从onCreate以外的方法访问用户界面

问题描述:

我试着用下面的方法创建一个类,并从onCreate之外的函数运行它,但是我试图执行它,我总是得到一个空指针异常错误。我搜索了一整天而没有结果。有人请帮我理解如何从onCreate之外的方法执行以下类吗?如果我在Create中调用它,它会运行得很好。谢谢。从onCreate以外的方法访问用户界面

runOnUiThread(new Runnable() { 
       @Override 
       public void run() { 
     Dialog x = new Dialog(); 
     x.showDialog(this, act0, "restart0"); 
} 
}); 


//Dialog class 
package com.calmchess.game1; 

import android.app.Activity; 
import android.content.Context; 
import android.util.Log; 
import android.view.View; 
import android.widget.Button; 

/** 
* Created by bobsmithzero on 10/16/17. 
*/ 

public class Dialog extends Activity{ 


    public void showDialog(final Context context, final Activity act00, final String dialogId0) { 

     switch (dialogId0) { 


      case ("pause0"): 
       Button paBtn0 = (Button) act00.findViewById(R.id.pabtn0); 
       paBtn0.setOnClickListener(new View.OnClickListener() { 


        public void onClick(View v) { 


         //set up pause dialog 
         final android.app.Dialog dialog0 = new android.app.Dialog(context); 
         dialog0.setContentView(R.layout.activity_controls); 
         dialog0.setCancelable(true); 

         Globals.pause0 = true; 


         Button button = (Button) dialog0.findViewById(R.id.pasbtn0); 
         button.setOnClickListener(new View.OnClickListener() { 
          @Override 
          public void onClick(View v) { 


           dialog0.cancel(); 
           Globals.pause0 = false; 
          } 
         }); 

         Button reBtn0 = (Button) dialog0.findViewById(R.id.rebtn0); 
         reBtn0.setOnClickListener(new View.OnClickListener() { 
          @Override 
          public void onClick(View v) { 

           Restart restart0 = new Restart(); 
           restart0.doRestart(context); 
          } 
         }); 


         dialog0.show(); 
        } 

       }); 

       break; 


      case ("restart0"): 
       Log.e("error","this thAT"); 
       //set up pause dialog 
       runOnUiThread(new Runnable() { 
        @Override 
        public void run() { 
         //set up pause dialog 
         android.app.Dialog dialog1 = new android.app.Dialog(context); 


         dialog1.setContentView(R.layout.activity_restart); 
         dialog1.setCancelable(true); 

         Globals.pause0 = true; 


         Button button = (Button) dialog1.findViewById(R.id.restart0); 
         button.setOnClickListener(new View.OnClickListener() { 
          @Override 
          public void onClick(View v) { 


           Restart restart0 = new Restart(); 
           restart0.doRestart(context); 
          } 
         }); 


         dialog1.show(); 

        } 
       }); 
       break; 
     } 



    } 
} 
+0

如果Dialog类没有被用作Activity,那么它不应该扩展Activity。您也可以考虑重命名,以防止与Android的“Dialog”类混淆。 –

+0

你真的会从使用更合适的名称命名你的东西并使用有效的日志中受益。 – JoxTraex

x.showDialog(this, act0, "restart0"); 

this是您的Runnable的实例。用正确的上下文实例替换this。例如,以YourActivity.this为例。

+0

我试过,但我仍然得到空指针异常x.showDialog(Main0.this,act0000,“restart0”);在类android.app.Dialog中的这一行dialog1 = new android.app.Dialog(context); – calmchess

+0

在'dialog1 = new android.app.Dialog(context);'只有'context'可以为null,没有其他的可能。检查它并找出'context'变量中null的来源。 –