自定义Android对话框的setMessage()中的空指针异常

时间:2014-09-28 15:46:23

标签: android dialog progressdialog

我正在尝试扩展ProgressDialog类以创建我的自定义对话框。当我尝试执行以下实现时。它在setmessage()调用

中给出Null Pointer Exception

有人可以解释一下如何解决这个问题吗?

private class SubProgressDialog extends ProgressDialog {

    Textview tvdialogtext;
    public SubProgressDialog(Context context) {

        super(context);
    }


    @Override
    public void setMessage(CharSequence message) {
        // TODO Auto-generated method stub

        tvdialogtext.setText(message);
    }

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        // TODO Auto-generated method stub
        super.onCreate(savedInstanceState);

        setContentView(R.layout.loadingdialog);
        tvdialogtext = (TextView) dialog.findViewById(R.id.tvloading);
        Typeface ttf = Typeface.createFromAsset(this.getContext()
                .getAssets(), "calibriitalic.ttf");
        ImageView ivloading = (ImageView) dialog
                .findViewById(R.id.ivloading);
        ivloading.setBackgroundResource(R.animator.loading);
        AnimationDrawable anim = (AnimationDrawable) ivloading
                .getBackground();
        anim.start();
        anim.setVisible(true,true );

        tvdialogtext.setTypeface(ttf);

        tvdialogtext.setTextColor(Color.parseColor("#ffffff"));
        tvdialogtext.setShadowLayer(25f, 0, 0, Color.WHITE);

    }

    @Override
    public void onBackPressed() {
        /** dismiss the progress bar and clean up here **/
        if (dialog != null) {
            dialog.dismiss();
        }
        running = false;
        finish();
        help.comeinfromleft();
    }
}

Logcat

 09-28 21:17:48.154: E/AndroidRuntime(27745): Caused by: java.lang.NullPointerException
 09-28 21:17:48.154: E/AndroidRuntime(27745):   at           m   sfs  com.anshul.guessthegaana.OnePlayerArena$SubProgressDialog.setMessage(OnePlayerArena.java:1552 )
 09-28 21:17:48.154: E/AndroidRuntime(27745):   at       com.anshul.guessthegaana.OnePlayerArena$GetSongsFromServer.onPreExecute(OnePlayerArena.java:1     191)
 09-28 21:17:48.154: E/AndroidRuntime(27745):   at           android.os.AsyncTask.executeOnExecutor(AsyncTask.java:586)
 09-28 21:17:48.154: E/AndroidRuntime(27745):   at android.os.AsyncTask.execute(AsyncTask.java:534)
 09-28 21:17:48.154: E/AndroidRuntime(27745):   at com.anshul.guessthegaana.OnePlayerArena.onCreate(OnePlayerArena.java:247)
 09-28 21:17:48.154: E/AndroidRuntime(27745):   at android.app.Activity.performCreate(Activity.java:5165)
 09-28 21:17:48.154: E/AndroidRuntime(27745):   at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1103)
 09-28 21:17:48.154: E/AndroidRuntime(27745):   at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2419)

初始化通话

        dialog = new SubProgressDialog(this);
        dialog.setMessage("Preparing the Game");

        dialog.setIndeterminate(false);

        dialog.show();

3 个答案:

答案 0 :(得分:0)

我认为你的问题在这里:

tvdialogtext = (TextView) dialog.findViewById(R.id.tvloading);

如果您的R.layout.loadingdialog中定义了tvDialogText,请使用:

tvdialogtext = (TextView) findViewById(R.id.tvloading);

如果没有,请告诉我们您在哪里初始化dialog

希望有所帮助

答案 1 :(得分:0)

终于想出来了

问题是调用Dialog的AsyncTask是在活动的ONCREATE上调用的,因此对话框没有实例化。

所以解决方法

在从Oncreate of activity调用的Async任务调用对话框时,在对话框中设置默认消息

setmessage应该适用于以后的实例

工作代码

private class SubProgressDialog extends ProgressDialog {

    TextView tvdialogtext;

    public SubProgressDialog(Context context) {

        super(context);
    }

    @Override
    public void setMessage(CharSequence message) {
        // TODO Auto-generated method stub
        // super.setMessage(message);

        if (tvdialogtext != null) {

            tvdialogtext.setText(message);
        }
    }

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        // TODO Auto-generated method stub
        super.onCreate(savedInstanceState);

        setContentView(R.layout.loadingdialog);


        tvdialogtext.setTypeface(ttf);

         // Your Default Text
        tvdialogtext.setText("Preparing The Game");


    }

    @Override
    public void onBackPressed() {
        /** dismiss the progress bar and clean up here **/
        if (dialog != null) {
            dialog.dismiss();
        }
        running = false;
        finish();
        help.comeinfromleft();
    }
}

答案 2 :(得分:0)

最简单解决问题的方法是在你的onPreExecute()方法中创建ProgressDialog并在全局声明它。

像这样:

progressDialog = new ProgressDialog(MyActivity.this);