从另一个活动启动时,ListView无法正常工作

时间:2015-01-23 20:53:48

标签: android android-listview

当我运行应用程序并单击按钮启动时,它表示该应用程序已停止。对此有任何帮助将不胜感激 这是我的大部分代码:

public class MainActivity2 extends ListActivity {
ListView listView;

protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    ListView theListView = getListView();

    Intent calledActivity=getIntent();
    final List pe=calledActivity.getExtras().getStringArrayList("Caller1");
           ListAdapter theAdapter= new ArrayAdapter<String>(this, android.R.layout.simple_list_item_1, pe);

    theListView.setAdapter(theAdapter);
    theListView.setOnItemClickListener(new AdapterView.OnItemClickListener() {
        @Override
        public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
            String s = String.valueOf(parent.getItemAtPosition(position));
            pe.remove(s);
        }
    });    

XML:

<ListView
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:id="@+id/listView"
    android:layout_gravity="center_vertical"
    android:layout_weight="1" />

logcat的:

01-23 16:03:29.501    1147-1147/com.example.epiclapser.noprocrastinate W/EGL_emulation﹕ eglSurfaceAttrib not implemented
01-23 16:03:29.541    1147-1147/com.example.epiclapser.noprocrastinate D/OpenGLRenderer﹕ Enabling debug mode 0
01-23 16:04:24.422    1147-1147/com.example.epiclapser.noprocrastinate D/dalvikvm﹕ GC_FOR_ALLOC freed 273K, 16% free 2746K/3244K, paused 76ms, total 105ms
01-23 16:04:24.552    1147-1147/com.example.epiclapser.noprocrastinate D/dalvikvm﹕ GC_FOR_ALLOC freed 63K, 17% free 2792K/3344K, paused 69ms, total 80ms
01-23 16:04:24.572    1147-1147/com.example.epiclapser.noprocrastinate I/dalvikvm-heap﹕ Grow heap (frag case) to 3.550MB for 635812-byte allocation
01-23 16:04:24.651    1147-1156/com.example.epiclapser.noprocrastinate D/dalvikvm﹕ GC_FOR_ALLOC freed <1K, 15% free 3412K/3968K, paused 82ms, total 82ms
01-23 16:04:24.742    1147-1147/com.example.epiclapser.noprocrastinate D/dalvikvm﹕ GC_FOR_ALLOC freed <1K, 15% free 3412K/3968K, paused 54ms, total 55ms
01-23 16:04:24.751    1147-1147/com.example.epiclapser.noprocrastinate I/dalvikvm-heap﹕ Grow heap (frag case) to 4.027MB for 500416-byte allocation
01-23 16:04:24.832    1147-1156/com.example.epiclapser.noprocrastinate D/dalvikvm﹕ GC_FOR_ALLOC freed <1K, 13% free 3900K/4460K, paused 76ms, total 76ms
01-23 16:04:25.001    1147-1147/com.example.epiclapser.noprocrastinate D/AndroidRuntime﹕ Shutting down VM
01-23 16:04:25.001    1147-1147/com.example.epiclapser.noprocrastinate W/dalvikvm﹕ threadid=1: thread exiting with uncaught exception (group=0x41465700)
01-23 16:04:25.082    1147-1147/com.example.epiclapser.noprocrastinate E/AndroidRuntime﹕ FATAL EXCEPTION: main

希望logcat有帮助:)

3 个答案:

答案 0 :(得分:0)

我假设你得到的错误是NullPointerException,因为你没有初始化你的listview .. 如何添加:     theListView =(ListView)findViewById(R.id.listView);

然后尝试再次运行.. 如果它不起作用,请添加logcat并显示错误!

TY。

答案 1 :(得分:0)

ListView theListView = getListView();将此更改为

ListView theListView = (ListView) findViewById(R.id.listView);`

答案 2 :(得分:0)

基类被称为ListActivity的原因。它可以通过调用ListView来获取ID为android.R.id.list的{​​{1}}。

然而,在此之前,您需要致电getListView()并传递一个setContentView具有此ID的布局。

以下是一个例子:

MainActivity2.java:

ListView

activity_main.xml中:

public class MainActivity2 extends ListActivity {

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);

        ...
    }

}