简体   繁体   English

Android App Force在开始新活动时关闭

[英]Android App ForceCloses on Starting of a new activity

I am new to android development and I have been having issues with Force Close on new Android Activity Pages. 我是Android开发的新手,并且在新的Android活动页面上强制关闭存在问题。

My Structure is like this: 我的结构是这样的:

Main Page -> Menu Page (Works Fine) -> New Game Screen(force closes) 主页->菜单页(正常工作)->新游戏屏幕(强制关闭)

Call to the Activity: 致电活动:

    public void btnNewGame_Click(View view)
{
    Intent i = new Intent(this, NewGameScreen.class);
    startActivityForResult(i, ACTIVITY_CREATE);
}

Manifest 表现

        <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格式

<?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 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);

Errors: 错误:

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

Change your ExpandableListView id with this 以此更改您的ExpandableListView ID

android:id="@android:id/list"

and if your class extends Activity then change it to 如果您的班级扩展了Activity,则将其更改为

public class className extends ListActivity

and you can get your ExpandableListView by calling the method 您可以通过调用方法来获取ExpandableListView

ExpandableListView listView = getListView();

The answer is very simple, you just forgot to insert the NewGameScreen Activity's Entry in the AndroidManiFest.xml Write down following line in your AndroidManifest.xml file before the tag. 答案很简单,您只是忘了在AndroidManiFest.xml中插入NewGameScreen活动的条目,请在AndroidManifest.xml文件中的标记之前写下以下行。

<activity android:name=".NewGameScreen"></activity>

And also important thing that your NewGameScreen class must extends Activity 同样重要的是,您的NewGameScreen类必须extends Activity

You need to give the id of your ListView as android:id="@android:id/list" , if you are extending LIstActivity. 如果要扩展LIstActivity,则需要将ListView的ID指定为android:id="@android:id/list" Then you can get your ListView handler from getLIstView(); 然后,您可以从getLIstView()获取ListView处理程序。 If you are extending Activity, then you can give any other id to your ListView and get it using findViewbyId() From the LOgCat trace, it looks like you are using ListActivity. 如果要扩展Activity,则可以将其他任何ID赋予ListView,并使用findViewbyId()从LOgCat跟踪中获取它,就好像您正在使用ListActivity。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM