我是Android开发的新手,我在新的Android活动页面上遇到了Force Close的问题。
我的结构是这样的:
主页 - >菜单页面(工作正常) - >新游戏画面(强制关闭)
致电活动:
public void btnNewGame_Click(View view)
{
Intent i = new Intent(this, NewGameScreen.class);
startActivityForResult(i, ACTIVITY_CREATE);
}
清单
<activity android:name=".NewGameScreen"
android:label="@string/app_name">
<intent-filter>
<action android:name="android.intent.action.MAIN" />
<category android:name="android.intent.category.DEFAULT" />
</intent-filter>
</activity>
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"
android:orientation="vertical" >
<ExpandableListView
android:id="@+id/eLVGameList"
android:layout_width="match_parent"
android:layout_gravity="center_vertical"
android:layout_weight=".75"
android:background="@null" android:layout_height="0dip">
</ExpandableListView>
</LinearLayout>
NewGameScreenActivity
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.loadgame);
ExpandableListView epView = (ExpandableListView) findViewById(R.id.eLVGameList);
ExpandableListAdapter mAdapter = new GameListAdapter();
epView.setAdapter(mAdapter);
错误:
03-05 20:16:57.375: E/AndroidRuntime(887): FATAL EXCEPTION: main
03-05 20:16:57.375: E/AndroidRuntime(887): java.lang.RuntimeException: Unable to start activity ComponentInfo{com.example.helloandroid/com.example.helloandroid.NewGameScreen}: java.lang.RuntimeException: Your content must have a ExpandableListView whose id attribute is 'android.R.id.list'
03-05 20:16:57.375: E/AndroidRuntime(887): at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:1956)
03-05 20:16:57.375: E/AndroidRuntime(887): at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:1981)
03-05 20:16:57.375: E/AndroidRuntime(887): at android.app.ActivityThread.access$600(ActivityThread.java:123)
03-05 20:16:57.375: E/AndroidRuntime(887): at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1147)
03-05 20:16:57.375: E/AndroidRuntime(887): at android.os.Handler.dispatchMessage(Handler.java:99)
03-05 20:16:57.375: E/AndroidRuntime(887): at android.os.Looper.loop(Looper.java:137)
03-05 20:16:57.375: E/AndroidRuntime(887): at android.app.ActivityThread.main(ActivityThread.java:4424)
03-05 20:16:57.375: E/AndroidRuntime(887): at java.lang.reflect.Method.invokeNative(Native Method)
03-05 20:16:57.375: E/AndroidRuntime(887): at java.lang.reflect.Method.invoke(Method.java:511)
03-05 20:16:57.375: E/AndroidRuntime(887): at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:784)
03-05 20:16:57.375: E/AndroidRuntime(887): at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:551)
03-05 20:16:57.375: E/AndroidRuntime(887): at dalvik.system.NativeStart.main(Native Method)
03-05 20:16:57.375: E/AndroidRuntime(887): Caused by: java.lang.RuntimeException: Your content must have a ExpandableListView whose id attribute is 'android.R.id.list'
03-05 20:16:57.375: E/AndroidRuntime(887): at android.app.ExpandableListActivity.onContentChanged(ExpandableListActivity.java:222)
03-05 20:16:57.375: E/AndroidRuntime(887): at com.android.internal.policy.impl.PhoneWindow.setContentView(PhoneWindow.java:254)
03-05 20:16:57.375: E/AndroidRuntime(887): at android.app.Activity.setContentView(Activity.java:1835)
03-05 20:16:57.375: E/AndroidRuntime(887): at com.example.helloandroid.NewGameScreen.onCreate(NewGameScreen.java:22)
03-05 20:16:57.375: E/AndroidRuntime(887): at android.app.Activity.performCreate(Activity.java:4465)
03-05 20:16:57.375: E/AndroidRuntime(887): at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1049)
03-05 20:16:57.375: E/AndroidRuntime(887): at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:1920)
03-05 20:16:57.375: E/AndroidRuntime(887): ... 11 more
答案 0 :(得分:3)
使用此
更改您的ExpandableListView IDandroid:id="@android:id/list"
如果您的类扩展了Activity,那么将其更改为
public class className extends ListActivity
您可以通过调用方法
来获取ExpandableListViewExpandableListView listView = getListView();
答案 1 :(得分:0)
答案非常简单,您只是忘记在AndroidManiFest.xml中插入NewGameScreen Activity的条目在标记之前记下AndroidManifest.xml文件中的以下行。
<activity android:name=".NewGameScreen"></activity>
同样重要的是你的NewGameScreen课程必须extends
Activity
答案 2 :(得分:0)
如果要扩展LIstActivity,则需要将ListView的ID设为android:id="@android:id/list"
。然后你可以从getLIstView()获取你的ListView处理程序;
如果您正在扩展Activity,那么您可以将任何其他ID提供给ListView并使用findViewbyId()获取它
从LOgCat跟踪看起来,您使用的是ListActivity。