从onCreate外部的方法访问UI

时间:2017-10-18 03:48:46

标签: android methods oncreate

我尝试使用以下内容创建一个类并从onCreate外部的函数运行它但是我尝试执行它我总是得到一个空指针异常错误。我用Google搜索并试了一整天没有结果。有人请帮助我理解如何从onCreate之外的方法执行以下类吗?如果我在Create上调用它,它运行得很好。感谢。

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



    }
}

1 个答案:

答案 0 :(得分:0)

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

this是您的Runnable实例。用正确的上下文实例替换thisYourActivity.this例如。