0

我得到了一个 NPE,但是当我检查 logcat 时,它没有使用我的代码的点。

程序在尝试显示包含 RadioButtonGroup 的 AlertDialog 后崩溃。它以前有效,我记得的唯一变化是在对话之后。

现在我收到以下信息: 编辑:

06-19 17:15:19.430: E/AndroidRuntime(898): java.lang.NullPointerException
06-19 17:15:19.430: E/AndroidRuntime(898):  at android.widget.ArrayAdapter.createViewFromResource(ArrayAdapter.java:394)
06-19 17:15:19.430: E/AndroidRuntime(898):  at android.widget.ArrayAdapter.getView(ArrayAdapter.java:362)
06-19 17:15:19.430: E/AndroidRuntime(898):  at android.widget.AbsListView.obtainView(AbsListView.java:2143)
06-19 17:15:19.430: E/AndroidRuntime(898):  at android.widget.ListView.measureHeightOfChildren(ListView.java:1246)
06-19 17:15:19.430: E/AndroidRuntime(898):  at android.widget.ListView.onMeasure(ListView.java:1158)
06-19 17:15:19.430: E/AndroidRuntime(898):  at android.view.View.measure(View.java:15518)
06-19 17:15:19.430: E/AndroidRuntime(898):  at android.view.ViewGroup.measureChildWithMargins(ViewGroup.java:4825)
06-19 17:15:19.430: E/AndroidRuntime(898):  at android.widget.LinearLayout.measureChildBeforeLayout(LinearLayout.java:1404)
06-19 17:15:19.430: E/AndroidRuntime(898):  at android.widget.LinearLayout.measureVertical(LinearLayout.java:695)
06-19 17:15:19.430: E/AndroidRuntime(898):  at android.widget.LinearLayout.onMeasure(LinearLayout.java:588)
06-19 17:15:19.430: E/AndroidRuntime(898):  at android.view.View.measure(View.java:15518)
06-19 17:15:19.430: E/AndroidRuntime(898):  at android.view.ViewGroup.measureChildWithMargins(ViewGroup.java:4825)
06-19 17:15:19.430: E/AndroidRuntime(898):  at android.widget.LinearLayout.measureChildBeforeLayout(LinearLayout.java:1404)
06-19 17:15:19.430: E/AndroidRuntime(898):  at android.widget.LinearLayout.measureVertical(LinearLayout.java:695)
06-19 17:15:19.430: E/AndroidRuntime(898):  at android.widget.LinearLayout.onMeasure(LinearLayout.java:588)
06-19 17:15:19.430: E/AndroidRuntime(898):  at android.view.View.measure(View.java:15518)
06-19 17:15:19.430: E/AndroidRuntime(898):  at android.view.ViewGroup.measureChildWithMargins(ViewGroup.java:4825)
06-19 17:15:19.430: E/AndroidRuntime(898):  at android.widget.FrameLayout.onMeasure(FrameLayout.java:310)
06-19 17:15:19.430: E/AndroidRuntime(898):  at android.view.View.measure(View.java:15518)
06-19 17:15:19.430: E/AndroidRuntime(898):  at android.view.ViewGroup.measureChildWithMargins(ViewGroup.java:4825)
06-19 17:15:19.430: E/AndroidRuntime(898):  at android.widget.FrameLayout.onMeasure(FrameLayout.java:310)
06-19 17:15:19.430: E/AndroidRuntime(898):  at android.view.View.measure(View.java:15518)
06-19 17:15:19.430: E/AndroidRuntime(898):  at android.view.ViewGroup.measureChildWithMargins(ViewGroup.java:4825)
06-19 17:15:19.430: E/AndroidRuntime(898):  at android.widget.FrameLayout.onMeasure(FrameLayout.java:310)
06-19 17:15:19.430: E/AndroidRuntime(898):  at com.android.internal.policy.impl.PhoneWindow$DecorView.onMeasure(PhoneWindow.java:2176)
06-19 17:15:19.430: E/AndroidRuntime(898):  at android.view.View.measure(View.java:15518)
06-19 17:15:19.430: E/AndroidRuntime(898):  at android.view.ViewRootImpl.performMeasure(ViewRootImpl.java:1874)
06-19 17:15:19.430: E/AndroidRuntime(898):  at android.view.ViewRootImpl.measureHierarchy(ViewRootImpl.java:1089)
06-19 17:15:19.430: E/AndroidRuntime(898):  at android.view.ViewRootImpl.performTraversals(ViewRootImpl.java:1265)
06-19 17:15:19.430: E/AndroidRuntime(898):  at android.view.ViewRootImpl.doTraversal(ViewRootImpl.java:989)
06-19 17:15:19.430: E/AndroidRuntime(898):  at android.view.ViewRootImpl$TraversalRunnable.run(ViewRootImpl.java:4351)
06-19 17:15:19.430: E/AndroidRuntime(898):  at android.view.Choreographer$CallbackRecord.run(Choreographer.java:749)
06-19 17:15:19.430: E/AndroidRuntime(898):  at android.view.Choreographer.doCallbacks(Choreographer.java:562)
06-19 17:15:19.430: E/AndroidRuntime(898):  at android.view.Choreographer.doFrame(Choreographer.java:532)
06-19 17:15:19.430: E/AndroidRuntime(898):  at android.view.Choreographer$FrameDisplayEventReceiver.run(Choreographer.java:735)
06-19 17:15:19.430: E/AndroidRuntime(898):  at android.os.Handler.handleCallback(Handler.java:725)
06-19 17:15:19.430: E/AndroidRuntime(898):  at android.os.Handler.dispatchMessage(Handler.java:92)
06-19 17:15:19.430: E/AndroidRuntime(898):  at android.os.Looper.loop(Looper.java:137)
06-19 17:15:19.430: E/AndroidRuntime(898):  at android.app.ActivityThread.main(ActivityThread.java:5041)
06-19 17:15:19.430: E/AndroidRuntime(898):  at java.lang.reflect.Method.invokeNative(Native Method)
06-19 17:15:19.430: E/AndroidRuntime(898):  at java.lang.reflect.Method.invoke(Method.java:511)
06-19 17:15:19.430: E/AndroidRuntime(898):  at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:793)
06-19 17:15:19.430: E/AndroidRuntime(898):  at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:560)
06-19 17:15:19.430: E/AndroidRuntime(898):  at dalvik.system.NativeStart.main(Native Method)

当我单步执行时,运行的最后一个代码块是:

public void addTeam (View view) {
    //open select race dialog
    AlertDialog alertDialog;
    AlertDialog.Builder builder = new AlertDialog.Builder(MainActivity.this); 

    String[] items = new String[races.size()];
    int i = 0;
    for (Race race : races) {
        //add radio button for each race
        items[i] = race.raceName;
    }
    builder.setTitle(getString(R.string.select_race));
    builder.setSingleChoiceItems(items, 0, null);

    builder.setPositiveButton(R.string.done, new DialogInterface.OnClickListener() {
            public void onClick(DialogInterface dialogInterface, int whichButton) {
                dialogInterface.dismiss();
                Team team = new Team();
                team.raceIndex = ((AlertDialog) dialogInterface).getListView().getCheckedItemPosition();
                team.raceName = races.get(team.raceIndex).raceName;
                setContentView(R.layout.edit_team);
                ((TextView) findViewById(R.id.raceTV)).setText(team.raceName);
            }
    });

    alertDialog = builder.create();  
    alertDialog.show();
}

如果没有对我的代码的行引用,我什至不知道从哪里开始。帮助表示赞赏。

4

3 回答 3

2

这是您的代码中的错误:

int i = 0;
for (Race race : races) {
    //add radio button for each race
    items[i] = race.raceName;
}

您不会i在 for 循环中递增。可能是这导致崩溃。

于 2013-06-19T18:04:44.790 回答
1

你有两个dialog实例。1)AlertDialog dialog;和 2) DialogInterface dialog。所以很可能会AlertDialog dialog;抛出NPE,因为它从未被初始化过所以默认null

于 2013-06-19T17:36:42.703 回答
0

替换这个

((TextView) findViewById(R.id.raceTV)).setText(team.raceName);

((TextView) dialog.findViewById(R.id.raceTV)).setText(team.raceName);
于 2013-06-19T17:40:55.017 回答