列表视图崩溃

时间:2012-08-08 14:27:54

标签: android

我正在尝试创建ListView并用数据填充它,但是当我启动应用程序崩溃并向我显示消息时

  

不幸的是,......已经停止了。

我正在尝试这样做tutorial


布局

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:orientation="vertical" >

    <ListView
        android:id="@+id/mylist"
        android:layout_width="match_parent"
        android:layout_height="wrap_content" >
    </ListView>

</LinearLayout> 

main.java

public class MainActivity extends Activity {




    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        String[] actions = new String[] { "Android", "iPhone", "WindowsMobile",
            "Blackberry", "WebOS", "Ubuntu", "Windows7", "Max OS X",
            "Linux", "OS/2" };

        ArrayAdapter<String> value = new ArrayAdapter<String>(this,android.R.layout.simple_list_item_1,
                R.id.mylist, actions);


        ListView  v = (ListView)findViewById(R.id.mylist);
        v.setTextFilterEnabled(true);
        v.setAdapter(value);
    }

    @Override
    public boolean onCreateOptionsMenu(Menu menu) {
        getMenuInflater().inflate(R.menu.activity_main, menu);
        return true;
    }


}

日志

08-08 15:19:47.364: E/Trace(604): error opening trace file: No such file or directory (2)
08-08 15:19:47.945: I/Choreographer(604): Skipped 40 frames!  The application may be doing too much work on its main thread.
08-08 15:19:47.965: D/AndroidRuntime(604): Shutting down VM
08-08 15:19:47.995: W/dalvikvm(604): threadid=1: thread exiting with uncaught exception (group=0x40a13300)
08-08 15:19:48.015: E/AndroidRuntime(604): FATAL EXCEPTION: main
08-08 15:19:48.015: E/AndroidRuntime(604): java.lang.NullPointerException
08-08 15:19:48.015: E/AndroidRuntime(604):  at android.widget.ArrayAdapter.createViewFromResource(ArrayAdapter.java:392)
08-08 15:19:48.015: E/AndroidRuntime(604):  at android.widget.ArrayAdapter.getView(ArrayAdapter.java:362)
08-08 15:19:48.015: E/AndroidRuntime(604):  at android.widget.AbsListView.obtainView(AbsListView.java:2267)
08-08 15:19:48.015: E/AndroidRuntime(604):  at android.widget.ListView.measureHeightOfChildren(ListView.java:1244)
08-08 15:19:48.015: E/AndroidRuntime(604):  at android.widget.ListView.onMeasure(ListView.java:1156)
08-08 15:19:48.015: E/AndroidRuntime(604):  at android.view.View.measure(View.java:15172)
08-08 15:19:48.015: E/AndroidRuntime(604):  at android.view.ViewGroup.measureChildWithMargins(ViewGroup.java:4814)
08-08 15:19:48.015: E/AndroidRuntime(604):  at android.widget.LinearLayout.measureChildBeforeLayout(LinearLayout.java:1390)
08-08 15:19:48.015: E/AndroidRuntime(604):  at android.widget.LinearLayout.measureVertical(LinearLayout.java:681)
08-08 15:19:48.015: E/AndroidRuntime(604):  at android.widget.LinearLayout.onMeasure(LinearLayout.java:574)
08-08 15:19:48.015: E/AndroidRuntime(604):  at android.view.View.measure(View.java:15172)
08-08 15:19:48.015: E/AndroidRuntime(604):  at android.view.ViewGroup.measureChildWithMargins(ViewGroup.java:4814)
08-08 15:19:48.015: E/AndroidRuntime(604):  at android.widget.FrameLayout.onMeasure(FrameLayout.java:310)
08-08 15:19:48.015: E/AndroidRuntime(604):  at android.view.View.measure(View.java:15172)
08-08 15:19:48.015: E/AndroidRuntime(604):  at android.widget.LinearLayout.measureVertical(LinearLayout.java:833)
08-08 15:19:48.015: E/AndroidRuntime(604):  at android.widget.LinearLayout.onMeasure(LinearLayout.java:574)
08-08 15:19:48.015: E/AndroidRuntime(604):  at android.view.View.measure(View.java:15172)
08-08 15:19:48.015: E/AndroidRuntime(604):  at android.view.ViewGroup.measureChildWithMargins(ViewGroup.java:4814)
08-08 15:19:48.015: E/AndroidRuntime(604):  at android.widget.FrameLayout.onMeasure(FrameLayout.java:310)
08-08 15:19:48.015: E/AndroidRuntime(604):  at com.android.internal.policy.impl.PhoneWindow$DecorView.onMeasure(PhoneWindow.java:2148)
08-08 15:19:48.015: E/AndroidRuntime(604):  at android.view.View.measure(View.java:15172)
08-08 15:19:48.015: E/AndroidRuntime(604):  at android.view.ViewRootImpl.performMeasure(ViewRootImpl.java:1848)
08-08 15:19:48.015: E/AndroidRuntime(604):  at android.view.ViewRootImpl.measureHierarchy(ViewRootImpl.java:1100)
08-08 15:19:48.015: E/AndroidRuntime(604):  at android.view.ViewRootImpl.performTraversals(ViewRootImpl.java:1273)
08-08 15:19:48.015: E/AndroidRuntime(604):  at android.view.ViewRootImpl.doTraversal(ViewRootImpl.java:998)
08-08 15:19:48.015: E/AndroidRuntime(604):  at android.view.ViewRootImpl$TraversalRunnable.run(ViewRootImpl.java:4212)
08-08 15:19:48.015: E/AndroidRuntime(604):  at android.view.Choreographer$CallbackRecord.run(Choreographer.java:725)
08-08 15:19:48.015: E/AndroidRuntime(604):  at android.view.Choreographer.doCallbacks(Choreographer.java:555)
08-08 15:19:48.015: E/AndroidRuntime(604):  at android.view.Choreographer.doFrame(Choreographer.java:525)
08-08 15:19:48.015: E/AndroidRuntime(604):  at android.view.Choreographer$FrameDisplayEventReceiver.run(Choreographer.java:711)
08-08 15:19:48.015: E/AndroidRuntime(604):  at android.os.Handler.handleCallback(Handler.java:615)
08-08 15:19:48.015: E/AndroidRuntime(604):  at android.os.Handler.dispatchMessage(Handler.java:92)
08-08 15:19:48.015: E/AndroidRuntime(604):  at android.os.Looper.loop(Looper.java:137)
08-08 15:19:48.015: E/AndroidRuntime(604):  at android.app.ActivityThread.main(ActivityThread.java:4745)
08-08 15:19:48.015: E/AndroidRuntime(604):  at java.lang.reflect.Method.invokeNative(Native Method)
08-08 15:19:48.015: E/AndroidRuntime(604):  at java.lang.reflect.Method.invoke(Method.java:511)
08-08 15:19:48.015: E/AndroidRuntime(604):  at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:786)
08-08 15:19:48.015: E/AndroidRuntime(604):  at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:553)
08-08 15:19:48.015: E/AndroidRuntime(604):  at dalvik.system.NativeStart.main(Native Method)
08-08 15:19:51.895: I/Process(604): Sending signal. PID: 604 SIG: 9

5 个答案:

答案 0 :(得分:7)

变化:

ArrayAdapter<String> value = new ArrayAdapter<String>(this,
       android.R.layout.simple_list_item_1, R.id.mylist, actions);

对此:

ArrayAdapter<String> value = new ArrayAdapter<String>(this,
       android.R.layout.simple_list_item_1, android.R.id.text1, actions);

//OR

ArrayAdapter<String> value = new ArrayAdapter<String>(this,
       android.R.id.text1, actions);

答案 1 :(得分:1)

由于您的ArrayAdapter构造函数,您将获得NullPointerException,第三个参数应该是布局中的TextView(第二个参数),将其更改为android.R.id.text1然后重试。

答案 2 :(得分:0)

似乎你误用了它的构造函数。

试试这个:

ArrayAdapter<String> value = new ArrayAdapter<String>(this,android.R.layout.simple_list_item_1, android.R.id.text1, actions);

ArrayAdapter Documentaion中,第三个参数应为TextView

的ID

答案 3 :(得分:0)

arrayadapter的构造函数不正确。

ArrayAdapter(此,android.R.layout.simple_list_item_1,                 R.id.mylist,actions);

我相信R.id.mylist是listview的标识符?使用此构造函数,它实际上期望textview的标识符是布局android.R.layout.simple_list_item_1的一部分

答案 4 :(得分:-1)

您的布局似乎超出了代码范围,因此无法加载。检查日志时,您可以看到错误“NullPointerException”,表示您正在尝试访问不存在的对象。

可能是行

ListView  v = (ListView)findViewById(R.id.mylist);

没有像你期望的那样创建对象。如果你在这一行上设置一个断点,你会看到它是null并且步进到下一行

v.setTextFilterEnabled(true);

您将看到此处发生异常。

请检查您的引用以及如何调用布局以获得正确的对象加载。