0

我正在将 ListActivity 与 String 类一起使用,除了它们不可点击之外,一切都很好。我想使用ListActivity字符串类而不是ListView. 我想将我的 activity.java 绑定到活动。如何解决这个问题?

package com.tutorial.android;
import android.app.ListActivity;
import android.content.Intent;
import android.os.Bundle;
import android.view.View;
import android.widget.ArrayAdapter;
import android.widget.ListView;

public class Menu extends ListActivity{ 
String classes [] =   
{"example0", "example1", "example2", "example3", "example4", "example5", "example6"};


@Override
protected void onCreate(Bundle savedInstanceState) {
    // TODO Auto-generated method stub
    super.onCreate(savedInstanceState);
    setListAdapter(new ArrayAdapter<String>(Menu.this, android.R.layout.simple_list_item_1, classes));
}

@Override
protected void onListItemClick(ListView l, View v, int position, long id) {
    // TODO Auto-generated method stub
    super.onListItemClick(l, v, position, id);
    String activity = classes[position];
    try{
    Class ourClass = Class.forName("com.tutorial.android. " + activity);
    startActivity(new Intent (Menu.this, ourClass));
    }catch(ClassNotFoundException e){
        e.printStackTrace();
    }
}

}

这很明显

 <?xml version="1.0" encoding="utf-8"?>
 <manifest xmlns:android="http://schemas.android.com/apk/res/android"
 package="com.tutorial.android"
 android:versionCode="1"
 android:versionName="1.0" >

<uses-sdk
    android:minSdkVersion="7"
    android:targetSdkVersion="8" />

<application
    android:allowBackup="true"
    android:icon="@drawable/ic_launcher"
    android:label="@string/app_name"
    android:theme="@style/AppTheme" >
    <activity
        android:name="com.tutorial.android.Splash"
        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="com.tutorial.android.Example0"
        android:label="@string/app_name" >
        <intent-filter>
            <action android:name="com.tutorial.android.EXAMPLE0" />

            <category android:name="android.intent.category.DEFAULT" />
        </intent-filter>
    </activity>
    <activity
        android:name="com.tutorial.android.Menu"
        android:label="@string/app_name" >
        <intent-filter>
            <action android:name="com.tutorial.android.MENU" />

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

1 回答 1

1

删除com.tutorial.android.此行后面的空格:

Class ourClass = Class.forName("com.tutorial.android. " + activity);

改成

Class ourClass = Class.forName("com.tutorial.android." + activity);

另外,您必须先实现Activitys 并将它们注册到 中,否则当您单击列表项时AndroidManifest.xml应用程序找不到。Activity

于 2013-11-03T04:00:07.863 回答