我正在使用ListActivity和String类,一切都很好,除了它们是不可点击的。我想将ListActivity
与不使用ListView
的字符串类一起使用。我想将activity.java绑定到activity。如何解决这个问题?
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>
答案 0 :(得分:1)
删除此行com.tutorial.android.
后的空格:
Class ourClass = Class.forName("com.tutorial.android. " + activity);
更改为
Class ourClass = Class.forName("com.tutorial.android." + activity);
此外,您必须先实施Activity
并在AndroidManifest.xml
中注册,否则当您点击列表项时,应用无法找到Activity
。