我的问题是开始新的活动。
我有ListView
,其中的项目是通过以下代码从字符串数组中定义的:
listBanksUA = getResources().getStringArray(R.array.list_banks_ua);
setListAdapter(new ArrayAdapter<String(this,android.R.layout.simple_list_item_1,listBanksUA));
列表在布局中定义为:
<ListView android:id="@+id/list"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:drawSelectorOnTop="false"/>
所以,我想在点击ListView
的项目后开始新的活动。当然,我已经创建了新的XML布局:
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout
xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="fill_parent"
android:layout_height="fill_parent">
<ListView
android:id="@+id/listRegions"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:drawSelectorOnTop="false"/>
</LinearLayout>
和它的Java类:ListRegionsActivity.class
。
在我的第一个Activity
中,我使用了以下代码来启动新的Activity
:
@Override
protected void onListItemClick(ListView l, View v, int position, long id)
{
startActivity(new Intent(MainActivity.this,ListRegionsActivity.class));
}
并且在点击启动应用程序后我收到错误,我的应用程序已经关闭,建议再试一次。悲伤...
所以,我真的很高兴为我提供任何帮助。 顺便说一句:当然,我在清单文件中添加了新活动。
UPD:带有错误标志的LogCAT
08-15 13:46:45.458: ERROR/AndroidRuntime(516): Uncaught handler: thread main exiting due to uncaught exception
08-15 13:46:45.488: ERROR/AndroidRuntime(516): java.lang.RuntimeException: Unable to start activity ComponentInfo{ua.donetsk.jeston.android/ua.donetsk.jeston.android.ListRegionsActivity}: java.lang.RuntimeException: Your content must have a ListView whose id attribute is 'android.R.id.list'
08-15 13:46:45.488: ERROR/AndroidRuntime(516): at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2401)
08-15 13:46:45.488: ERROR/AndroidRuntime(516): at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2417)
08-15 13:46:45.488: ERROR/AndroidRuntime(516): at android.app.ActivityThread.access$2100(ActivityThread.java:116)
08-15 13:46:45.488: ERROR/AndroidRuntime(516): at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1794)
08-15 13:46:45.488: ERROR/AndroidRuntime(516): at android.os.Handler.dispatchMessage(Handler.java:99)
08-15 13:46:45.488: ERROR/AndroidRuntime(516): at android.os.Looper.loop(Looper.java:123)
08-15 13:46:45.488: ERROR/AndroidRuntime(516): at android.app.ActivityThread.main(ActivityThread.java:4203)
08-15 13:46:45.488: ERROR/AndroidRuntime(516): at java.lang.reflect.Method.invokeNative(Native Method)
08-15 13:46:45.488: ERROR/AndroidRuntime(516): at java.lang.reflect.Method.invoke(Method.java:521)
08-15 13:46:45.488: ERROR/AndroidRuntime(516): at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:791)
08-15 13:46:45.488: ERROR/AndroidRuntime(516): at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:549)
08-15 13:46:45.488: ERROR/AndroidRuntime(516): at dalvik.system.NativeStart.main(Native Method)
08-15 13:46:45.488: ERROR/AndroidRuntime(516): Caused by: java.lang.RuntimeException: Your content must have a ListView whose id attribute is 'android.R.id.list'
08-15 13:46:45.488: ERROR/AndroidRuntime(516): at android.app.ListActivity.onContentChanged(ListActivity.java:236)
08-15 13:46:45.488: ERROR/AndroidRuntime(516): at com.android.internal.policy.impl.PhoneWindow.setContentView(PhoneWindow.java:316)
08-15 13:46:45.488: ERROR/AndroidRuntime(516): at android.app.Activity.setContentView(Activity.java:1620)
08-15 13:46:45.488: ERROR/AndroidRuntime(516): at ua.donetsk.jeston.android.ListRegionsActivity.onCreate(ListRegionsActivity.java:15)
08-15 13:46:45.488: ERROR/AndroidRuntime(516): at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1123)
08-15 13:46:45.488: ERROR/AndroidRuntime(516): at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2364)
08-15 13:46:45.488: ERROR/AndroidRuntime(516): ... 11 more
UPD:问题解决了。原因是打开avtivity时出现错误错误 - 应该删除setContentView。
答案 0 :(得分:0)
由于您没有向我们展示LogCat跟踪,我可以做猜测。
在ListView
点击监听器
MainActivity.this.startActivity(new Intent(MainActivity.this,ListRegionsActivity.class))
并且不要忘记在清单中添加新活动。
答案 1 :(得分:0)
检查您是不是在应用中导入android.R而不是your.package.R