启动新活动时从alertdialog抛出NullPointerException

时间:2015-08-03 03:54:00

标签: java android android-alertdialog android-sdk-2.3

我正在为Android手机应用程序开发一个异常处理程序。我正在尝试链接错误框上的正面“提交报告”按钮,以将用户定向到ContactActivity ...联系支持页面。当用户按下提交按钮时,会从我认为的意图的上下文中抛出NullPointerException。我已经尝试了ExceptionHandler.this,getApplicationcontext(),getBaseContext(),似乎没有任何工作,我不想经历设置自定义配置文件的麻烦,因为我有点急,但我被困了关于这个问题几个小时。

public class ExceptionHandler extends BaseActivity {

private String phoneModel = android.os.Build.MODEL;
private String phoneDevice = android.os.Build.DEVICE;
private String phoneVersion = android.os.Build.VERSION.RELEASE;
private String errorTag;
private Context context;

public ExceptionHandler (Context context, String tag){
    this.context = context;
    errorTag = tag;
}

public void alert(Exception e){

    AlertDialog.Builder messageBox = new AlertDialog.Builder(this.context, AlertDialog.THEME_HOLO_DARK);
    messageBox.setTitle("Oops...");
    messageBox.setMessage("An error has occurred .\n\n" +
            "Error: " + e.toString() + "\n" +
            "Location: " + AtlasApplication.MenuTitle + "\n" +
            "Phone Model: " + phoneModel + "\n" +
            "Phone Device: " + phoneDevice + "\n" +
            "API Version: " + phoneVersion + "\n");
   messageBox.setPositiveButton("Send Report", new DialogInterface.OnClickListener() {
        public void onClick(DialogInterface dialog, int id) {
            try {
                startActivityWithAnim(new Intent(context, ContactActivity.class));
                //I have also tried (ContactActivity.getIntent(context))
            }
            catch(Exception e) {
                alert(e);
            }
        }
    });
    messageBox.setNegativeButton("Close", new DialogInterface.OnClickListener() {
        public void onClick(DialogInterface dialog, int id) {
            dialog.dismiss();
        }
    });

    messageBox.create();
    messageBox.show();
}

public void reportError(Exception e){

}

}

1 个答案:

答案 0 :(得分:2)

AFAIK,如果你想让ExceptionHandler显示警告,你就不需要扩展BaseActivity(你的自定义Activity)。例如,如果要从MainActivity调用ExceptionHandler,可以这样做

ExceptionHandler exHandler = new ExceptionHandler(getApplicationContext(),"MAIN_ACTIVITY"); 
exHandler.alert(new Exception()); //Put your exception in the parameter.

如果要为ExceptionHandler创建新的Activity,则不需要在参数中传递上下文。对于NullPointerException,请尝试

context.startActivityWithAnim(new Intent(context,ContactActivity.class));

希望这会有所帮助:)

相关问题