我创建了一个名为 Menu 的类。当用户按下菜单按钮时,他们会看到 3 个选项,即。关于,偏好和退出。当他们单击首选项时,应该会打开首选项活动。但是,该应用程序崩溃并且无法打开首选项活动。我已经发布了 ListActivity,我试图在清单和日志文件中打开的活动的活动标签。请帮忙。
public class Menu extends ListActivity {
String[] list = { "StartView", "FunWithText", "Email", "Camera",
"ActivityStarter" };
@Override
protected void onListItemClick(ListView l, View v, int position, long id) {
super.onListItemClick(l, v, position, id);
try {
Class myClass = Class.forName("com.thenewboston.terry."
+ list[position]);
Intent myIntent = new Intent(Menu.this, myClass);
startActivity(myIntent);
} catch (ClassNotFoundException e) {
e.printStackTrace();
}
}
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setListAdapter(new ArrayAdapter<String>(Menu.this,
android.R.layout.simple_list_item_1, list));
}
@Override
public boolean onCreateOptionsMenu(android.view.Menu menu) {
super.onCreateOptionsMenu(menu);
MenuInflater balloon = getMenuInflater();
balloon.inflate(R.menu.menu, menu);
return true;
}
@Override
public boolean onOptionsItemSelected(MenuItem item) {
switch (item.getItemId()) {
case (R.id.about):
Intent myIntent = new Intent(Menu.this, Dialogue.class);
startActivity(myIntent);
break;
case (R.id.preferences):
Intent prefs = new Intent(Menu.this, Preferences.class);
startActivity(prefs);
break;
case (R.id.exit):
finish();
break;
}
return false;
}
}
清单.xml:
<activity
android:name=".Preferences"
android:label="Start Activity"
android:theme="@android:style/Theme.Dialog" >
<intent-filter>
<action android:name="com.thenewboston.terry.PREFS" />
<category android:name="android.intent.category.DEFAULT" />
</intent-filter>
</activity>
错误日志:
07-18 00:39:07.133: E/AndroidRuntime(738): FATAL EXCEPTION: main
07-18 00:39:07.133: E/AndroidRuntime(738): java.lang.RuntimeException: Unable to start activity ComponentInfo{com.thenewboston.terry/com.thenewboston.terry.Preferences}: android.view.InflateException: Binary XML file line #14: Error inflating class listPreference
07-18 00:39:07.133: E/AndroidRuntime(738): at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2663)
07-18 00:39:07.133: E/AndroidRuntime(738): at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2679)
07-18 00:39:07.133: E/AndroidRuntime(738): at android.app.ActivityThread.access$2300(ActivityThread.java:125)
07-18 00:39:07.133: E/AndroidRuntime(738): at android.app.ActivityThread$H.handleMessage(ActivityThread.java:2033)
07-18 00:39:07.133: E/AndroidRuntime(738): at android.os.Handler.dispatchMessage(Handler.java:99)
07-18 00:39:07.133: E/AndroidRuntime(738): at android.os.Looper.loop(Looper.java:123)
07-18 00:39:07.133: E/AndroidRuntime(738): at android.app.ActivityThread.main(ActivityThread.java:4627)
07-18 00:39:07.133: E/AndroidRuntime(738): at java.lang.reflect.Method.invokeNative(Native Method)
07-18 00:39:07.133: E/AndroidRuntime(738): at java.lang.reflect.Method.invoke(Method.java:521)
07-18 00:39:07.133: E/AndroidRuntime(738): at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:868)
07-18 00:39:07.133: E/AndroidRuntime(738): at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:626)
07-18 00:39:07.133: E/AndroidRuntime(738): at dalvik.system.NativeStart.main(Native Method)
07-18 00:39:07.133: E/AndroidRuntime(738): Caused by: android.view.InflateException: Binary XML file line #14: Error inflating class listPreference
07-18 00:39:07.133: E/AndroidRuntime(738): at android.preference.GenericInflater.createItemFromTag(GenericInflater.java:441)
07-18 00:39:07.133: E/AndroidRuntime(738): at android.preference.GenericInflater.rInflate(GenericInflater.java:481)
07-18 00:39:07.133: E/AndroidRuntime(738): at android.preference.GenericInflater.inflate(GenericInflater.java:326)
07-18 00:39:07.133: E/AndroidRuntime(738): at android.preference.GenericInflater.inflate(GenericInflater.java:263)
07-18 00:39:07.133: E/AndroidRuntime(738): at android.preference.PreferenceManager.inflateFromResource(PreferenceManager.java:251)
07-18 00:39:07.133: E/AndroidRuntime(738): at android.preference.PreferenceActivity.addPreferencesFromResource(PreferenceActivity.java:262)
07-18 00:39:07.133: E/AndroidRuntime(738): at com.thenewboston.terry.Preferences.onCreate(Preferences.java:12)
07-18 00:39:07.133: E/AndroidRuntime(738): at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1047)
07-18 00:39:07.133: E/AndroidRuntime(738): at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2627)
07-18 00:39:07.133: E/AndroidRuntime(738): ... 11 more
07-18 00:39:07.133: E/AndroidRuntime(738): Caused by: java.lang.ClassNotFoundException: android.preference.listPreference in loader dalvik.system.PathClassLoader[/data/app/com.thenewboston.terry-1.apk]
07-18 00:39:07.133: E/AndroidRuntime(738): at dalvik.system.PathClassLoader.findClass(PathClassLoader.java:243)
07-18 00:39:07.133: E/AndroidRuntime(738): at java.lang.ClassLoader.loadClass(ClassLoader.java:573)
07-18 00:39:07.133: E/AndroidRuntime(738): at java.lang.ClassLoader.loadClass(ClassLoader.java:532)
07-18 00:39:07.133: E/AndroidRuntime(738): at android.preference.GenericInflater.createItem(GenericInflater.java:375)
07-18 00:39:07.133: E/AndroidRuntime(738): at android.preference.GenericInflater.onCreateItem(GenericInflater.java:417)
07-18 00:39:07.133: E/AndroidRuntime(738): at android.preference.GenericInflater.createItemFromTag(GenericInflater.java:428)
07-18 00:39:07.133: E/AndroidRuntime(738): ... 19 more
**Preferences xml:** <?xml version="1.0" encoding="utf-8"?> <PreferenceScreen xmlns:android="http://schemas.android.com/apk/res/android"> <PreferenceCategory android:title="Label"> <EditTextPreference android:title="EditText" android:key="name" android:summary="Enter your name"/> <CheckBoxPreference android:title="checkBox" android:key="music" android:defaultValue="true" android:summary="music on?" /> <listPreference android:title="list" android:key="list" android:summary="Choose somethign" android:entries="@array/list" android:entryValues="@array/values" /> </PreferenceCategory> </PreferenceScreen>
public class Preferences extends PreferenceActivity {
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
addPreferencesFromResource(R.xml.prefs);
}
}