我正在尝试开发一款Android应用,其中以ListView格式显示水果列表。但是,无论我如何尝试以及我如何密切关注教程,它总是在打开时崩溃。
这是我的主要活动
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
ListView listView = (ListView) findViewById(R.id.list);
String[] values = new String[]{"apples", "oranges", "grapes", "pomegrantes"};
ArrayAdapter<String> adapter = new ArrayAdapter<String>(this, R.layout.list_item, values);
listView.setAdapter(adapter);
}
这是我的主要活动xml
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:paddingBottom="@dimen/activity_vertical_margin"
android:paddingLeft="@dimen/activity_horizontal_margin"
android:paddingRight="@dimen/activity_horizontal_margin"
android:paddingTop="@dimen/activity_vertical_margin"
tools:context="com.example.test.MainActivity" >
<LinearLayout
android:id="@+id/linearLayout1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginTop="36dp"
android:orientation="vertical" >
</LinearLayout>
<ListView
android:id="@+id/list"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_alignLeft="@+id/linearLayout1"
android:layout_centerVertical="true" >
</ListView>
</RelativeLayout>
这是我的list_item.xml
<?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" >
<TextView
android:id="@+id/list_entry_title"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:textSize="20sp" />
</LinearLayout>
关于我做错的任何想法?
修改
这就是我的logcat所说的:
AndroidRuntime(19894):致命的例外:主要
AndroidRuntime(19894):java.lang.RuntimeException:无法启动活动ComponentInfo {com.example.test / com.example.test.MainActivity}:java.lang.RuntimeException:您的内容必须具有其id属性的ListView是&#39; android.R.id.list&#39;
AndroidRuntime(19894):在android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2211)
AndroidRuntime(19894):在android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2261)
AndroidRuntime(19894):在android.app.ActivityThread.access $ 600(ActivityThread.java:141)
AndroidRuntime(19894):在android.app.ActivityThread $ H.handleMessage(ActivityThread.java:1256)
AndroidRuntime(19894):在android.os.Handler.dispatchMessage(Handler.java:99)
AndroidRuntime(19894):在android.os.Looper.loop(Looper.java:137)
AndroidRuntime(19894):在android.app.ActivityThread.main(ActivityThread.java:5103)
AndroidRuntime(19894):at java.lang.reflect.Method.invokeNative(Native Method)
AndroidRuntime(19894):在java.lang.reflect.Method.invoke(Method.java:525)
AndroidRuntime(19894):at com.android.internal.os.ZygoteInit $ MethodAndArgsCaller.run(ZygoteInit.java:737)
AndroidRuntime(19894):at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:553)
AndroidRuntime(19894):at dalvik.system.NativeStart.main(Native Method)
AndroidRuntime(19894):引起:java.lang.RuntimeException:您的内容必须有一个ListView,其id属性为&#39; android.R.id.list&#39; AndroidRuntime(19894):在android.app.ListActivity.onContentChanged(ListActivity.java:243) AndroidRuntime(19894):在com.android.internal.policy.impl.PhoneWindow.setContentView(PhoneWindow.java:270)
AndroidRuntime(19894):在android.app.Activity.setContentView(Activity.java:1895)
AndroidRuntime(19894):at com.example.test.MainActivity.onCreate(MainActivity.java:15)
AndroidRuntime(19894):在android.app.Activity.performCreate(Activity.java:5133)
AndroidRuntime(19894):在android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1087)
AndroidRuntime(19894):在android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2175)
AndroidRuntime(19894):... 11更多
解决!!
感谢所有帮助过的人!最后,答案的组合起作用,我的最终代码如下
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
ListView listView = (ListView) findViewById(android.R.id.list);
String[] values = new String[]{"apples", "oranges", "grapes", "pomegrantes"};
ArrayAdapter<String> adapter = new ArrayAdapter<String>(this, R.layout.list_item, R.id.list_entry_title, values);
listView.setAdapter(adapter);
}
答案 0 :(得分:1)
java.lang.RuntimeException:您的内容必须有一个ListView,其id属性为&#39; android.R.id.list&#39;
<ListView android:id="@android:id/list"
答案 1 :(得分:0)
使用@android:id/list
您应该在使用ListActivity
时使用该ID并更改
ArrayAdapter<String> adapter = new ArrayAdapter<String>(this, R.layout.list_item, values);
带
ArrayAdapter<String> adapter = new ArrayAdapter<String>(this, R.layout.list_item, R.id.list_entry_title, values);
检查doc
3º参数指定TextView
适配器用于设置文本的
Array
的id