因此,我正在编写一个名为“石头,纸,剪刀”的游戏,每当我试图向用户提示困难时,我都会得到NullPointerException。我的意图是发起一个新的活动,该活动通过intent的putExtra方法传递难度值。此操作将通过自定义对话框执行。这是日志:
08-11 13:38:09.619 8725-8725/dserov.rockpaperscissors E/AndroidRuntime﹕ FATAL EXCEPTION: main
Process: dserov.rockpaperscissors, PID: 8725
java.lang.NullPointerException
at android.widget.ArrayAdapter.createViewFromResource(ArrayAdapter.java:394)
at android.widget.ArrayAdapter.getView(ArrayAdapter.java:362)
at android.widget.AbsListView.obtainView(AbsListView.java:2255)
at android.widget.ListView.measureHeightOfChildren(ListView.java:1263)
at android.widget.ListView.onMeasure(ListView.java:1175)
at android.view.View.measure(View.java:16497)
at android.view.ViewGroup.measureChildWithMargins(ViewGroup.java:5125)
at android.widget.LinearLayout.measureChildBeforeLayout(LinearLayout.java:1404)
at android.widget.LinearLayout.measureVertical(LinearLayout.java:695)
at android.widget.LinearLayout.onMeasure(LinearLayout.java:588)
at android.view.View.measure(View.java:16497)
at android.view.ViewGroup.measureChildWithMargins(ViewGroup.java:5125)
at android.widget.LinearLayout.measureChildBeforeLayout(LinearLayout.java:1404)
at android.widget.LinearLayout.measureVertical(LinearLayout.java:695)
at android.widget.LinearLayout.onMeasure(LinearLayout.java:588)
at android.view.View.measure(View.java:16497)
at android.view.ViewGroup.measureChildWithMargins(ViewGroup.java:5125)
at android.widget.FrameLayout.onMeasure(FrameLayout.java:310)
at android.view.View.measure(View.java:16497)
at android.view.ViewGroup.measureChildWithMargins(ViewGroup.java:5125)
at android.widget.FrameLayout.onMeasure(FrameLayout.java:310)
at android.view.View.measure(View.java:16497)
at android.view.ViewGroup.measureChildWithMargins(ViewGroup.java:5125)
at android.widget.FrameLayout.onMeasure(FrameLayout.java:310)
at com.android.internal.policy.impl.PhoneWindow$DecorView.onMeasure(PhoneWindow.java:2291)
at android.view.View.measure(View.java:16497)
at android.view.ViewRootImpl.performMeasure(ViewRootImpl.java:1912)
at android.view.ViewRootImpl.measureHierarchy(ViewRootImpl.java:1084)
at android.view.ViewRootImpl.performTraversals(ViewRootImpl.java:1291)
at android.view.ViewRootImpl.doTraversal(ViewRootImpl.java:996)
at android.view.ViewRootImpl$TraversalRunnable.run(ViewRootImpl.java:5600)
at android.view.Choreographer$CallbackRecord.run(Choreographer.java:761)
at android.view.Choreographer.doCallbacks(Choreographer.java:574)
at android.view.Choreographer.doFrame(Choreographer.java:544)
at android.view.Choreographer$FrameDisplayEventReceiver.run(Choreographer.java:747)
at android.os.Handler.handleCallback(Handler.java:733)
at android.os.Handler.dispatchMessage(Handler.java:95)
at android.os.Looper.loop(Looper.java:136)
at android.app.ActivityThread.main(ActivityThread.java:5001)
at java.lang.reflect.Method.invokeNative(Native Method)
at java.lang.reflect.Method.invoke(Method.java:515)
at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:785)
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:601)
at dalvik.system.NativeStart.main(Native Method)
这是MainActivity:
public class MainActivity extends AppCompatActivity {
@Bind(R.id.singleplayerButton) Button mSingleplayerButton;
@Bind(R.id.multiplayerButton) Button mMultiplayerButton;
@Bind(R.id.gameEmblem) ImageView mGameEmblem;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
ButterKnife.bind(this);
View.OnClickListener listener = new View.OnClickListener() {
@Override
public void onClick(View v) {
switch (v.getId()) {
case (R.id.singleplayerButton) : {
AlertDialogFragment dialog = new AlertDialogFragment();
dialog.show(getFragmentManager(), getString(R.string.difficulty_dialog));
break;
}
case (R.id.multiplayerButton) : {
break;
}
}
}
};
mSingleplayerButton.setOnClickListener(listener);
mMultiplayerButton.setOnClickListener(listener);
}
这是AlertDialogFragment:
public class AlertDialogFragment extends DialogFragment {
@Override
public Dialog onCreateDialog(Bundle savedInstanceState) {
final Context context = getActivity();
AlertDialog.Builder builder = new AlertDialog.Builder(context);
builder.setTitle(R.string.difficulty_prompt)
.setItems(R.array.difficulties, new DialogInterface.OnClickListener() {
@Override
public void onClick(DialogInterface dialog, int which) {
Intent intent = new Intent(context, PlayActivity.class);
switch (which) {
case 0 : {
intent.putExtra(context.getString(R.string.difficulty_label), 0);
break;
}
case 1 : {
intent.putExtra(context.getString(R.string.difficulty_label), 1);
break;
}
}
startActivity(intent);
}
});
AlertDialog dialog = builder.create();
return dialog;
}
}
Android Studio的bugtracker函数不会产生任何结果,每当按下Singleplayer按钮时都会通过某些系统函数引发异常。到目前为止,没有任何线索可以得到异常代码,但是我最好的猜测是AlertDialogFragment的show()方法。
最佳答案
什么时候会粉碎?在您的代码中,我没有看到您关闭对话框的行,因此,如果您没有该行,并且在对话框显示后且重定向到另一个活动之前,应用程序崩溃,请尝试关闭对话框