您好我正在使用Android中的數組列表填充ListView,點擊一個按鈕從第一個屏幕移動到第二個。但在點擊第一個屏幕後,出現以下錯誤。任何幫助真的很感激。從一個屏幕移動到另一個屏幕時android listview錯誤
second_activity.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" >
<ListView
android:id="@+id/listDisplayData"
android:layout_width="match_parent"
android:layout_height="wrap_content" >
</ListView>
SecondActivity.java
包com.example.listexample;
import java.util.ArrayList;
import android.app.Activity;
import android.content.Intent;
import android.os.Bundle;
import android.widget.ArrayAdapter;
import android.widget.ListView;
public class SecondActivity extends Activity {
ListView listDisplayData;
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.second_activity);
//reference to listview
ListView listDisplayData = (ListView)findViewById(R.id.listDisplayData);
final ArrayList<String> arr = new ArrayList<String>();
arr.add("name");
arr.add("address");
final ArrayAdapter<String> adapter = new ArrayAdapter<String>(this,
android.R.layout.list_content, arr);
listDisplayData.setAdapter(adapter);
};
}
錯誤:
01-13 16:11:37.028: E/AndroidRuntime(1188): FATAL EXCEPTION: main
01-13 16:11:37.028: E/AndroidRuntime(1188): java.lang.RuntimeException: Unable to start activity ComponentInfo{com.example.listexample/com.example.listexample.DisplayData}: java.lang.RuntimeException: Your content must have a ListView whose id attribute is 'android.R.id.list'
01-13 16:11:37.028: E/AndroidRuntime(1188): at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2180)
01-13 16:11:37.028: E/AndroidRuntime(1188): at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2230)
01-13 16:11:37.028: E/AndroidRuntime(1188): at android.app.ActivityThread.access$600(ActivityThread.java:141)
01-13 16:11:37.028: E/AndroidRuntime(1188): at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1234)
01-13 16:11:37.028: E/AndroidRuntime(1188): at android.os.Handler.dispatchMessage(Handler.java:99)
01-13 16:11:37.028: E/AndroidRuntime(1188): at android.os.Looper.loop(Looper.java:137)
01-13 16:11:37.028: E/AndroidRuntime(1188): at android.app.ActivityThread.main(ActivityThread.java:5039)
01-13 16:11:37.028: E/AndroidRuntime(1188): at java.lang.reflect.Method.invokeNative(Native Method)
01-13 16:11:37.028: E/AndroidRuntime(1188): at java.lang.reflect.Method.invoke(Method.java:511)
01-13 16:11:37.028: E/AndroidRuntime(1188): at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:793)
01-13 16:11:37.028: E/AndroidRuntime(1188): at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:560)
01-13 16:11:37.028: E/AndroidRuntime(1188): at dalvik.system.NativeStart.main(Native Method)
01-13 16:11:37.028: E/AndroidRuntime(1188): Caused by: java.lang.RuntimeException: Your content must have a ListView whose id attribute is 'android.R.id.list'
01-13 16:11:37.028: E/AndroidRuntime(1188): at android.app.ListActivity.onContentChanged(ListActivity.java:243)
01-13 16:11:37.028: E/AndroidRuntime(1188): at com.android.internal.policy.impl.PhoneWindow.setContentView(PhoneWindow.java:273)
01-13 16:11:37.028: E/AndroidRuntime(1188): at android.app.Activity.setContentView(Activity.java:1881)
01-13 16:11:37.028: E/AndroidRuntime(1188): at com.example.listexample.DisplayData.onCreate(DisplayData.java:26)
01-13 16:11:37.028: E/AndroidRuntime(1188): at android.app.Activity.performCreate(Activity.java:5104)
01-13 16:11:37.028: E/AndroidRuntime(1188): at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1080)
01-13 16:11:37.028: E/AndroidRuntime(1188): at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2144)
01-13 16:11:37.028: E/AndroidRuntime(1188): ... 11 more
如果任何更多的細節要求,那麼請讓我知道。
你不明白什麼? '你的內容必須有一個ListView,它的id屬性是'android.R.id.list'' – njzk2
也請閱讀你的錯誤。它是在DisplayData上,而不是在SecondActivity上... – njzk2
你有另外一個擴展ListView的活動,但沒有android.R.id.list作爲id!你的第二個活動很好 –