试图在android中创建弹出窗口,但出现空对象引用错误

67up9zun  于 2021-06-29  发布在  Java
关注(0)|答案(0)|浏览(242)

首先,我检查了在android studio中对空对象引用调用虚拟方法“android.content.context.getapplicationinfo()”和在notify notification android时对空对象引用调用“android.content.pm.applicationinfo android.content.context.getapplicationinfo()”的尝试,但我认为两者都不直接适用于我。
如前所述,我试图在android上创建一个弹出窗口,但我得到的错误是

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)

相关代码如下:

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();
    }
}

错误发生在线路上 dialogBuilder = new AlertDialog.Builder(this); 我不知道为什么。一开始我以为是因为 thisnull 但是当我打印出来的时候 conversation.ConversationActivity@bfca1fa . 我想也许是因为 dialogBuilder 是一个私有类变量(我删除了private关键字),而不是 public 是个问题,但我还是犯了同样的错误。也许这是一件很琐碎的事,而我却看不到?

暂无答案!

目前还没有任何答案,快来回答吧!

相关问题