ListActivity不起作用

时间:2014-11-16 05:36:17

标签: android

我创建了一个菜单,其中所有活动都将出现在列表中,每当我点击该列表中的某个活动时,它都应该启动该活动。

这是我的menu.java

public class Menu extends ListActivity
{
    String classnames[] = {"MainActivity","example1","example2","example3",
    "example4","example5","example6","example7","example8","example9","example10"};
    protected void onCreate(Bundle savedInstanceState)
    {
        super.onCreate(savedInstanceState);
        setListAdapter(new ArrayAdapter<String>(Menu.this,android.R.layout.simple_list_item_1,classnames));
    }
    protected void onListItemClick(ListView l, View v, int position, long id)
    {
        super.onListItemClick(l, v, position, id);
        String classClicked = classnames[position];
        try
        {
            Class ourclass = Class.forName("android.intent.action."+classClicked);
            Intent intent = new Intent(Menu.this,ourclass);
            startActivity(intent);
        }
        catch(ClassNotFoundException e)
        {
            e.printStackTrace();
        }
    }

}

这是我的清单:

<application
        android:allowBackup="true"
        android:icon="@drawable/ic_launcher"
        android:label="@string/app_name"
        android:theme="@style/AppTheme" >
        <activity
            android:name=".Menu"
            android:label="@string/app_name" >
            <intent-filter>
                <action android:name="android.intent.action.MAIN" />

                <category android:name="android.intent.category.LAUNCHER" />
            </intent-filter>
        </activity>
        <activity
            android:name=".MainActivity"
            android:label="@string/app_name" >
            <intent-filter>
                <action android:name="android.intent.action.MAINACTIVITY" />

                <category android:name="android.intent.category.DEFAULT" />
            </intent-filter>
        </activity>
</application>

它没有任何错误,但即使我点击它,我的MainActivity也无法启动。

这就是它的样子:

enter image description here

3 个答案:

答案 0 :(得分:2)

使用getListView().setOnItemClickListener代替onListItemClick。为什么?说实话我也不知道,但它解决了我的问题。

答案 1 :(得分:1)

您必须在AndroidManifest.xml中声明所有活动,否则将无效。您更新的清单文件应如下所示:

<application
    android:allowBackup="true"
    android:icon="@drawable/ic_launcher"
    android:label="@string/app_name"
    android:theme="@style/AppTheme" >
    <activity
        android:name=".Menu"
        android:label="@string/app_name" >
        <intent-filter>
            <action android:name="android.intent.action.MAIN" />

            <category android:name="android.intent.category.LAUNCHER" />
        </intent-filter>
    </activity>
    <activity
        android:name=".MainActivity"
        android:label="@string/app_name" >
        <intent-filter>
            <action android:name="android.intent.action.MAINACTIVITY" />

            <category android:name="android.intent.category.DEFAULT" />
        </intent-filter>
    </activity>

    <activity
        android:name=".example1"
        android:label="@string/app_name"
        android:parentActivityName=".MainActivity" >
        <meta-data
            android:name="android.support.PARENT_ACTIVITY"
            android:value=".MainActivity" />
    </activity>

    <activity
        android:name=".example2"
        android:label="@string/app_name"
        android:parentActivityName=".MainActivity" >
        <meta-data
            android:name="android.support.PARENT_ACTIVITY"
            android:value=".MainActivity" />
    </activity>

    <activity
        android:name=".example3"
        android:label="@string/app_name"
        android:parentActivityName=".MainActivity" >
        <meta-data
            android:name="android.support.PARENT_ACTIVITY"
            android:value=".MainActivity" />
    </activity>

    <activity
        android:name=".example4"
        android:label="@string/app_name"
        android:parentActivityName=".MainActivity" >
        <meta-data
            android:name="android.support.PARENT_ACTIVITY"
            android:value=".MainActivity" />
    </activity>

    <activity
        android:name=".example5"
        android:label="@string/app_name"
        android:parentActivityName=".MainActivity" >
        <meta-data
            android:name="android.support.PARENT_ACTIVITY"
            android:value=".MainActivity" />
    </activity>

    <activity
        android:name=".example6"
        android:label="@string/app_name"
        android:parentActivityName=".MainActivity" >
        <meta-data
            android:name="android.support.PARENT_ACTIVITY"
            android:value=".MainActivity" />
    </activity>

    <activity
        android:name=".example7"
        android:label="@string/app_name"
        android:parentActivityName=".MainActivity" >
        <meta-data
            android:name="android.support.PARENT_ACTIVITY"
            android:value=".MainActivity" />
    </activity>

    <activity
        android:name=".example8"
        android:label="@string/app_name"
        android:parentActivityName=".MainActivity" >
        <meta-data
            android:name="android.support.PARENT_ACTIVITY"
            android:value=".MainActivity" />
    </activity>

    <activity
        android:name=".example9"
        android:label="@string/app_name"
        android:parentActivityName=".MainActivity" >
        <meta-data
            android:name="android.support.PARENT_ACTIVITY"
            android:value=".MainActivity" />
    </activity>

    <activity
        android:name=".example10"
        android:label="@string/app_name"
        android:parentActivityName=".MainActivity" >
        <meta-data
            android:name="android.support.PARENT_ACTIVITY"
            android:value=".MainActivity" />
    </activity>
</application>

Menu.java

public class Menu extends ListActivity
{
String classnames[] = {"MainActivity","example1","example2","example3",
"example4","example5","example6","example7","example8","example9","example10"};
protected void onCreate(Bundle savedInstanceState)
{
    super.onCreate(savedInstanceState);
    setListAdapter(new ArrayAdapter<String>(Menu.this,android.R.layout.simple_list_item_1,classnames));

getListView().setOnItemClickListener(new AdapterView.OnItemClickListener() {
        @Override
        public void onItemClick(AdapterView<?> adapterView, View view, final int position, long l) {

            String classClicked = classnames[position];

            try {
                    Class ourclass = Class.forName("android.intent.action."+classClicked);
                    Intent intent = new Intent(getApplicationContext(),ourclass);
                    startActivity(intent);
                }
                catch(ClassNotFoundException e)
                {
                    e.printStackTrace();
                }

        }
    });
}
protected void onListItemClick(ListView l, View v, int position, long id)
{
    /*super.onListItemClick(l, v, position, id);
    String classClicked = classnames[position];
    try
    {
        Class ourclass = Class.forName("android.intent.action."+classClicked);
        Intent intent = new Intent(Menu.this,ourclass);
        startActivity(intent);
    }
    catch(ClassNotFoundException e)
    {
        e.printStackTrace();
    }*/
}

}

现在一切都应该有效。

答案 2 :(得分:0)

您必须在清单中注册其他活动,就像主要活动一样。否则Android不会启动它们。