0

我已经启动了一个带有应包含列表的 Intent 的 Activity,但是当我收集数据并将其绑定到我的 ListView 时,应用程序崩溃了。我在这一行得到错误:

resultMenu.setAdapter(new ArrayAdapter<String>(this, android.R.layout.simple_list_item_1, selected));

这是我的整个活动。提前致谢。

public class Result extends Activity {
    private ArrayList<String> menuList;

    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.result);

        Intent intent = this.getIntent();
        ArrayList<String> selected = intent.getStringArrayListExtra("menuList"); 

        // Configure the listview
        ListView resultMenu = (ListView)this.findViewById(R.id.menuResult);
        resultMenu.setAdapter(new ArrayAdapter<String>(this, 
                android.R.layout.simple_list_item_1, selected));
    }
}
4

1 回答 1

0

Since your force close is caused by this line:

resultMenu.setAdapter(new ArrayAdapter<String>(this, android.R.layout.simple_list_item_1, selected));

The only two possible errors that I can see are:

  1. selected is null, or
  2. resultMenu is null

Both of these cause a Null Pointer Exception. If your file result.xml has a ListView element with android:id="@+id/resultMenu then the error is in selected.

I am betting that selected is possibly a String[] or the Intent does not have a 'menuList' key.

How do you build the Intent used to start your Activity Result?

于 2012-06-07T04:12:04.557 回答