Welcome to WuJiGu Developer Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
296 views
in Technique[技术] by (71.8m points)

java - Attempting to create a popup window in Android but getting a null object reference error

First thing is I've checked Attempt to invoke virtual method 'android.content.Context.getApplicationInfo()' on a null object reference in Android Studio and 'android.content.pm.ApplicationInfo android.content.Context.getApplicationInfo()' on a null object reference when notify notification Android but I don't think either applies directly for me.

As mentioned I'm trying to create a popup window on android but the error I'm getting is

E/AndroidRuntime: FATAL EXCEPTION: main
    Process: ca.used, PID: 6240
    java.lang.NullPointerException: Attempt to invoke virtual method 'android.content.pm.ApplicationInfo android.content.Context.getApplicationInfo()' on a null object reference
        at android.content.ContextWrapper.getApplicationInfo(ContextWrapper.java:163)
        at android.view.ContextThemeWrapper.getTheme(ContextThemeWrapper.java:174)
        at androidx.appcompat.app.AlertDialog.resolveDialogTheme(AlertDialog.java:115)
        at androidx.appcompat.app.AlertDialog$Builder.<init>(AlertDialog.java:313)
        at conversation.ConversationActivity.showDownloadPopUpTest(ConversationActivity.java:451)
        at conversation.ConversationActivity.downloadFile(ConversationActivity.java:336)
        at adapters.ConversationAdapter$1.onClick(ConversationAdapter.java:211)
        at android.view.View.performClick(View.java:7161)
        at android.view.View.performClickInternal(View.java:7133)
        at android.view.View.access$3500(View.java:804)
        at android.view.View$PerformClick.run(View.java:27416)
        at android.os.Handler.handleCallback(Handler.java:883)
        at android.os.Handler.dispatchMessage(Handler.java:100)
        at android.os.Looper.loop(Looper.java:241)
        at android.app.ActivityThread.main(ActivityThread.java:7604)
        at java.lang.reflect.Method.invoke(Native Method)
        at com.android.internal.os.RuntimeInit$MethodAndArgsCaller.run(RuntimeInit.java:492)
        at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:941)

The relevant code is below:

public class ConversationActivity extends AppCompatActivity implements ConversationContract.View {
   AlertDialog.Builder dialogBuilder;
   private AlertDialog dialog;
   private Button viewDownloadBtn;

   @Override
   protected void onCreate(Bundle savedInstanceState) {
       super.onCreate(savedInstanceState);
       setTheme(R.style.AppTheme);
       setContentView(R.layout.activity_conversation);
   }

   public void downloadFile(Message msg, String filesPath) {
        showDownloadPopUpTest();
   }

   public void showDownloadPopUpTest() {
        System.out.println("Showing Popup2");
        System.out.println("THIS: "+this);
        System.out.println("this: " + ConversationActivity.this);
        dialogBuilder = new AlertDialog.Builder(this); //ERROR HAPPENS HERE
        System.out.println("A1");
        final View popupView = getLayoutInflater().inflate(R.layout.activity_download_popup, null);
        System.out.println("A2");
        viewDownloadBtn = (Button) popupView.findViewById(R.id.closePopupBtn);
        System.out.println("A3");
        dialogBuilder.setView(popupView);
        System.out.println("A4");
        dialog = dialogBuilder.create();
        dialog.show();
    }
}

The error occurs at the line dialogBuilder = new AlertDialog.Builder(this); and I'm not sure why. At first I thought it was because this was null but when I printed it out I get conversation.ConversationActivity@bfca1fa. I then thought maybe because dialogBuilder was a private class variable (I have removed the private keyword) instead of being public was the issue but I'm still getting the same error. Maybe it's something really trivial and I'm just not seeing it?


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Answer

0 votes
by (71.8m points)
等待大神答复

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome to WuJiGu Developer Q&A Community for programmer and developer-Open, Learning and Share
...