3

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

不幸已经停止了。

我正在尝试做这个教程


布局

<?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
4

5 回答 5

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);
于 2012-08-08T14:36:31.710 回答
1

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

于 2012-08-08T14:36:18.720 回答
0

您的 arrayadapter 构造函数不正确。

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

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

于 2012-08-08T14:44:46.953 回答
0

看来您滥用了它的构造函数。

尝试这个:

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

ArrayAdapter Documentaion中,第三个参数应该是一个 IDTextView

于 2012-08-08T14:37:59.733 回答
-1

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

大概就是这条线

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

这并没有像您期望的那样创建对象。如果你在这一行上放一个断点,你会看到它是空的,然后跳到下一行

v.setTextFilterEnabled(true);

你会看到这里发生了异常。

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

于 2012-08-08T14:42:28.810 回答