我想创建非常简单的ListView
,但不幸的是每次应用程序都会崩溃。
这是我的代码:
import android.app.ListActivity;
import android.os.Bundle;
import android.util.Log;
import android.view.View;
import android.widget.AdapterView;
import android.widget.AdapterView.OnItemClickListener;
import android.widget.ArrayAdapter;
import android.widget.ListView;
public class MainActivity extends ListActivity implements OnItemClickListener {
public static String[] daysArray = {"Ponedeljek", "Torek", "Sreda", "Četrtek", "Petek", "Sobota", "Nedelja"};
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
ArrayAdapter<String> adapter = new ArrayAdapter<String>(this, android.R.layout.simple_list_item_1, daysArray);
setListAdapter(adapter);
ListView lv = getListView();
lv.setOnItemClickListener(this);
}
@Override
public void onItemClick(AdapterView<?> arg0, View arg1, int arg2, long arg3) {
// TODO Auto-generated method stub
Log.i("Info", "CLICKED");
}
}
布局main.xml:
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="fill_parent"
android:layout_height="fill_parent" >
<ListView android:id="@+id/listView"
android:layout_width="match_parent"
android:layout_height="match_parent" />
</RelativeLayout>
修改
02-18 14:56:51.549: D/dalvikvm(2618): GC_FOR_ALLOC freed 68K, 6% free 3096K/3280K, paused 17ms, total 17ms
02-18 14:56:51.561: I/dalvikvm-heap(2618): Grow heap (frag case) to 4.202MB for 1127532-byte allocation
02-18 14:56:51.565: D/dalvikvm(2618): GC_FOR_ALLOC freed 2K, 5% free 4195K/4384K, paused 4ms, total 4ms
02-18 14:56:51.581: D/AndroidRuntime(2618): Shutting down VM
02-18 14:56:51.585: W/dalvikvm(2618): threadid=1: thread exiting with uncaught exception (group=0xa4d89b20)
02-18 14:56:51.585: E/AndroidRuntime(2618): FATAL EXCEPTION: main
02-18 14:56:51.585: E/AndroidRuntime(2618): Process: com.example.listsstudy, PID: 2618
02-18 14:56:51.585: E/AndroidRuntime(2618): java.lang.RuntimeException: Unable to start activity ComponentInfo{com.example.listsstudy/com.example.listsstudy.MainActivity}: java.lang.RuntimeException: Your content must have a ListView whose id attribute is 'android.R.id.list'
02-18 14:56:51.585: E/AndroidRuntime(2618): at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2195)
02-18 14:56:51.585: E/AndroidRuntime(2618): at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2245)
02-18 14:56:51.585: E/AndroidRuntime(2618): at android.app.ActivityThread.access$800(ActivityThread.java:135)
02-18 14:56:51.585: E/AndroidRuntime(2618): at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1196)
02-18 14:56:51.585: E/AndroidRuntime(2618): at android.os.Handler.dispatchMessage(Handler.java:102)
02-18 14:56:51.585: E/AndroidRuntime(2618): at android.os.Looper.loop(Looper.java:136)
02-18 14:56:51.585: E/AndroidRuntime(2618): at android.app.ActivityThread.main(ActivityThread.java:5017)
02-18 14:56:51.585: E/AndroidRuntime(2618): at java.lang.reflect.Method.invokeNative(Native Method)
02-18 14:56:51.585: E/AndroidRuntime(2618): at java.lang.reflect.Method.invoke(Method.java:515)
02-18 14:56:51.585: E/AndroidRuntime(2618): at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:779)
02-18 14:56:51.585: E/AndroidRuntime(2618): at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:595)
02-18 14:56:51.585: E/AndroidRuntime(2618): at dalvik.system.NativeStart.main(Native Method)
02-18 14:56:51.585: E/AndroidRuntime(2618): Caused by: java.lang.RuntimeException: Your content must have a ListView whose id attribute is 'android.R.id.list'
02-18 14:56:51.585: E/AndroidRuntime(2618): at android.app.ListActivity.onContentChanged(ListActivity.java:243)
02-18 14:56:51.585: E/AndroidRuntime(2618): at com.android.internal.policy.impl.PhoneWindow.setContentView(PhoneWindow.java:293)
02-18 14:56:51.585: E/AndroidRuntime(2618): at android.app.Activity.setContentView(Activity.java:1929)
02-18 14:56:51.585: E/AndroidRuntime(2618): at com.example.listsstudy.MainActivity.onCreate(MainActivity.java:19)
02-18 14:56:51.585: E/AndroidRuntime(2618): at android.app.Activity.performCreate(Activity.java:5231)
02-18 14:56:51.585: E/AndroidRuntime(2618): at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1087)
02-18 14:56:51.585: E/AndroidRuntime(2618): at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2159)
02-18 14:56:51.585: E/AndroidRuntime(2618): ... 11 more
答案 0 :(得分:3)
您正在延长ListActivity
。您的ListView
必须
android:id="@android:id/list"
作为id。
答案 1 :(得分:2)
需要使用
android:id="@android:id/list"
而不是:
android:id="@+id/listView"
如果您使用ListActivity。或者只是尝试使用Activity,而不是ListActivity
编辑: 我的代码; 布局:
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:orientation="vertical"
android:layout_width="match_parent"
android:layout_height="match_parent">
<ListView android:id="@android:id/list"
android:layout_width="match_parent"
android:layout_height="match_parent"/>
...
在我班上:
public class AnyNameHere extends ListActivity {
@Override
protected void onCreate ( Bundle savedInstanceState ) {
super.onCreate ( savedInstanceState );
setContentView ( R.layout.listview );
// do what you want here;
}
它的工作:)
答案 2 :(得分:2)
你的ListView有错误的ID。来自documentation:
为此,您自己的视图必须包含带有id的ListView对象 “@android:id / list”(或列表,如果它在代码中)
答案 3 :(得分:0)
因为你的setContentView方法调用。
答案 4 :(得分:0)
当您希望entend ListActivity
创建活动时,MainActivity
,ListView
xml必须标识为android:id
list
,如下所示。 ..
<ListView
android:id="@+id/list"
android:layout_width="match_parent"
android:layout_height="match_parent" />