gpt4 book ai didi

android - ListView 崩溃

转载 作者:太空宇宙 更新时间:2023-11-03 12:22:18 24 4
gpt4 key购买 nike

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

Unfortunately,… has stopped.

我正在尝试这样做 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>

主.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

最佳答案

改变:

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);

关于android - ListView 崩溃,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/11866719/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com