我在自己的Java类中有一个AlertDialog。其中一个选项是启动一个Activity,但此时似乎不可能,因为我总是以java.lang.NullPointerException
结束。
这是AlertDialog的代码,YallDoneDialog.java:
import android.app.Dialog;
import android.content.Context;
import android.support.v4.app.DialogFragment;
import android.content.DialogInterface;
import android.os.Bundle;
import android.support.v7.app.AlertDialog;
public class YallDoneDialog extends DialogFragment {
private Context mContext;
public YallDoneDialog() {
}
public static YallDoneDialog newInstance(String title) {
YallDoneDialog frag = new YallDoneDialog();
Bundle args = new Bundle();
args.putString("title", title);
frag.setArguments(args);
return frag;
}
@Override
public Dialog onCreateDialog(Bundle savedInstanceState) {
AlertDialog.Builder builder = new AlertDialog.Builder(getActivity());
builder.setMessage(R.string.done)
.setTitle(R.string.done_title)
.setPositiveButton(R.string.y, new DialogInterface.OnClickListener() {
public void onClick(DialogInterface dialog, int id) {
MenuActivity m = new MenuActivity();
String resName = m.getResName();
LaunchBill l = new LaunchBill(mContext, getActivity());
l.LaunchTheDuckingBill(resName);
}
})
.setNegativeButton(R.string.n, new DialogInterface.OnClickListener() {
public void onClick(DialogInterface dialog, int id) {
dismiss();
}
});
return builder.create();
}
}
您将看到来自LaunchBill.java的名为LaunchTheDuckingBill的方法被称为肯定按钮的操作。这是代码:
import android.app.Activity;
import android.content.Context;
import android.content.Intent;
public class LaunchBill {
private Context mContext;
private Activity mActivity;
public LaunchBill(Context mContext, Activity mActivity){
this.mContext = mContext;
this.mActivity = mActivity;
}
public void LaunchTheDuckingBill(String resName){
Intent intent = new Intent(mContext, BillActivity.class);
intent.putExtra("resName",resName);
mContext.startActivity(intent);
}
}
这是崩溃的整个堆栈跟踪:
05-13 20:45:10.836 21191-21191/com.whatamidoingwithmylife.splitbill E/AndroidRuntime: FATAL EXCEPTION: main
Process: com.whatamidoingwithmylife.splitbill, PID: 21191
java.lang.NullPointerException: Attempt to invoke virtual method 'java.lang.String android.content.Context.getPackageName()' on a null object reference
at android.content.ComponentName.<init>(ComponentName.java:128)
at android.content.Intent.<init>(Intent.java:4916)
at com.whatamidoingwithmylife.splitbill.LaunchBill.LaunchTheDuckingBill(LaunchBill.java:18)
at com.whatamidoingwithmylife.splitbill.YallDoneDialog$2.onClick(YallDoneDialog.java:74)
at android.support.v7.app.AlertController$ButtonHandler.handleMessage(AlertController.java:162)
at android.os.Handler.dispatchMessage(Handler.java:102)
at android.os.Looper.loop(Looper.java:154)
at android.app.ActivityThread.main(ActivityThread.java:6186)
at java.lang.reflect.Method.invoke(Native Method)
at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:889)
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:779)
你能告诉我出错的地方吗?我尝试过多种解决方案,但都会产生同样的错误。
答案 0 :(得分:0)
第1步:从private Context mContext;
删除YallDoneDialog
。
第2步:将LaunchBill l = new LaunchBill(mContext, getActivity());
替换为LaunchBill l = new LaunchBill(getActivity(), getActivity());