所以我正在编写一个名为摇滚,纸张,剪刀的游戏,每当我试图提示用户遇到困难时,我都会收到NullPointerException。我的目的是通过意图的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()方法。
答案 0 :(得分:0)
什么时候粉碎?在你的代码中,我没有看到你关闭对话框的行,所以如果你没有那行,并且在对话框显示后应用程序崩溃,并且在重定向到另一个活动之前尝试解除对话框
答案 1 :(得分:0)
我一直在试图弄清楚它,我终于明白错误是什么。将.setItems(context.getResources().getStringArray(R.array.difficulties) ...
更改为{{1}}就可以了。