我正在学习如何构建一个Android应用程序并解决了这种情况:
当我尝试在模拟器中午餐时,按下AboutUs按钮它工作正常,也作为退出。但是当我按下偏好按钮时,我收到一个错误:
> 01-23 07:40:59.145: E/AndroidRuntime(314): FATAL EXCEPTION: main
01-23 07:40:59.145: E/AndroidRuntime(314): java.lang.RuntimeException: Unable to start activity ComponentInfo{com.jinibox.jinibox/com.jinibox.jinibox.Prefs}: java.lang.ClassCastException: java.lang.String
01-23 07:40:59.145: E/AndroidRuntime(314): at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2663)
01-23 07:40:59.145: E/AndroidRuntime(314): at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2679)
01-23 07:40:59.145: E/AndroidRuntime(314): at android.app.ActivityThread.access$2300(ActivityThread.java:125)
01-23 07:40:59.145: E/AndroidRuntime(314): at android.app.ActivityThread$H.handleMessage(ActivityThread.java:2033)
01-23 07:40:59.145: E/AndroidRuntime(314): at android.os.Handler.dispatchMessage(Handler.java:99)
01-23 07:40:59.145: E/AndroidRuntime(314): at android.os.Looper.loop(Looper.java:123)
01-23 07:40:59.145: E/AndroidRuntime(314): at android.app.ActivityThread.main(ActivityThread.java:4627)
01-23 07:40:59.145: E/AndroidRuntime(314): at java.lang.reflect.Method.invokeNative(Native Method)
01-23 07:40:59.145: E/AndroidRuntime(314): at java.lang.reflect.Method.invoke(Method.java:521)
01-23 07:40:59.145: E/AndroidRuntime(314): at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:868)
01-23 07:40:59.145: E/AndroidRuntime(314): at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:626)
01-23 07:40:59.145: E/AndroidRuntime(314): at dalvik.system.NativeStart.main(Native Method)
01-23 07:40:59.145: E/AndroidRuntime(314): Caused by: java.lang.ClassCastException: java.lang.String
01-23 07:40:59.145: E/AndroidRuntime(314): at android.app.ContextImpl$SharedPreferencesImpl.getBoolean(ContextImpl.java:2724)
01-23 07:40:59.145: E/AndroidRuntime(314): at android.preference.Preference.getPersistedBoolean(Preference.java:1425)
01-23 07:40:59.145: E/AndroidRuntime(314): at android.preference.CheckBoxPreference.onSetInitialValue(CheckBoxPreference.java:259)
01-23 07:40:59.145: E/AndroidRuntime(314): at android.preference.Preference.dispatchSetInitialValue(Preference.java:1172)
01-23 07:40:59.145: E/AndroidRuntime(314): at android.preference.Preference.onAttachedToHierarchy(Preference.java:984)
01-23 07:40:59.145: E/AndroidRuntime(314): at android.preference.PreferenceGroup.addPreference(PreferenceGroup.java:156)
01-23 07:40:59.145: E/AndroidRuntime(314): at android.preference.PreferenceGroup.addItemFromInflater(PreferenceGroup.java:97)
01-23 07:40:59.145: E/AndroidRuntime(314): at android.preference.PreferenceGroup.addItemFromInflater(PreferenceGroup.java:38)
01-23 07:40:59.145: E/AndroidRuntime(314): at android.preference.GenericInflater.rInflate(GenericInflater.java:488)
01-23 07:40:59.145: E/AndroidRuntime(314): at android.preference.GenericInflater.inflate(GenericInflater.java:326)
01-23 07:40:59.145: E/AndroidRuntime(314): at android.preference.GenericInflater.inflate(GenericInflater.java:263)
01-23 07:40:59.145: E/AndroidRuntime(314): at android.preference.PreferenceManager.inflateFromResource(PreferenceManager.java:251)
01-23 07:40:59.145: E/AndroidRuntime(314): at android.preference.PreferenceActivity.addPreferencesFromResource(PreferenceActivity.java:262)
01-23 07:40:59.145: E/AndroidRuntime(314): at com.jinibox.jinibox.Prefs.onCreate(Prefs.java:12)
01-23 07:40:59.145: E/AndroidRuntime(314): at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1047)
01-23 07:40:59.145: E/AndroidRuntime(314): at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2627)
01-23 07:40:59.145: E/AndroidRuntime(314): ... 11 more
有人能帮助我吗?
cool_menu.xml
<?xml version="1.0" encoding="utf-8"?>
<menu xmlns:android="http://schemas.android.com/apk/res/android" >
<item
android:id="@+id/aboutUs"
android:alphabeticShortcut="a"
android:numericShortcut="1"
android:title="Abou Us"/>
<item
android:id="@+id/preferences "
android:title="Preferences"/>
<item
android:id="@+id/exit "
android:title="Exit"/>
</menu>
menu.java
package com.jinibox.jinibox;
import android.app.ListActivity;
import android.content.Intent;
import android.os.Bundle;
import android.view.MenuInflater;
import android.view.MenuItem;
import android.view.View;
import android.widget.ArrayAdapter;
import android.widget.ListView;
public class Menu extends ListActivity {
String classes[] = { "JiniboxActivity", "TextPlay", "Email", "Camera",
"Data", };
@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 jiniboxmenu = classes[position];
try {
Class ourClass = Class
.forName("com.jinibox.jinibox." + jiniboxmenu);
Intent ourIntent = new Intent(Menu.this, ourClass);
startActivity(ourIntent);
} catch (ClassNotFoundException e) {
e.printStackTrace();
}
}
@Override
public boolean onCreateOptionsMenu(android.view.Menu menu) {
// TODO Auto-generated method stub
super.onCreateOptionsMenu(menu);
MenuInflater blowup = getMenuInflater();
blowup.inflate(R.menu.cool_menu, menu);
return true;
}
@Override
public boolean onOptionsItemSelected(MenuItem item) {
// TODO Auto-generated method stub
switch (item.getItemId()) {
case R.id.aboutUs:
Intent i = new Intent("com.jinibox.jinibox.ABOUTUS");
startActivity(i);
break;
case R.id.preferences:
Intent p = new Intent("com.jinibox.jinibox.PREFS");
startActivity(p);
break;
case R.id.exit:
finish();
break;
}
return false;
}
}
Prefs.java
package com.jinibox.jinibox;
import android.os.Bundle;
import android.preference.PreferenceActivity;
public class Prefs extends PreferenceActivity{
@Override
protected void onCreate(Bundle savedInstanceState){
//TODO Auto-generated method stub
super.onCreate(savedInstanceState);
addPreferencesFromResource(R.xml.prefs);
}
}
的prefs.xml
<?xml version="1.0" encoding="utf-8"?>
<PreferenceScreen xmlns:android="http://schemas.android.com/apk/res/android" >
<EditTextPreference
android:title="EditText"
android:key="name"
android:summary="Enter Your Name" >
</EditTextPreference>
<CheckBoxPreference
android:title="Music"
android:defaultValue="true"
android:key="chackbox"
android:summary="Chack This Box" />
<ListPreference
android:title="list"
android:key="list"
android:summary="This Is A List To Choos From"
android:entries="@array/list"
android:entryValues="@array/lvalues">
</ListPreference>
</PreferenceScreen>
**array.xml**
<?xml version="1.0" encoding="utf-8"?>
<resources>
<string-array name="list">
<item>option 1</item>
<item>option 2</item>
<item>option 3</item>
<item>option 4</item>
</string-array>
<string-array name="lvalues">
<item>1</item>
<item>2</item>
<item>3</item>
<item>4</item>
</string-array>
</resources>
清单
<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
package="com.jinibox.jinibox"
android:versionCode="1"
android:versionName="1.0" >
<uses-sdk android:minSdkVersion="8" />
<uses-permission android:name="android.permission.SET_WALLPAPER" />
<application
android:icon="@drawable/ic_launcher"
android:label="@string/app_name" >
<activity
android:name=".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=".JiniboxActivity"
android:label="@string/app_name" >
<intent-filter>
<action android:name="com.jinibox.jinibox.JINIBOX" />
<category android:name="android.intent.category.DEFAULT" />
</intent-filter>
</activity>
<activity
android:name=".Menu"
android:label="@string/app_name" >
<intent-filter>
<action android:name="com.jinibox.jinibox.MENU" />
<category android:name="android.intent.category.DEFAULT" />
</intent-filter>
</activity>
<activity
android:name=".TextPlay"
android:label="@string/app_name" >
</activity>
<activity
android:name=".Email"
android:label="@string/app_name" >
</activity>
<activity
android:name=".Camera"
android:label="Camera Application"
android:screenOrientation="portrait" >
</activity>
<activity
android:name=".Data"
android:label="Data" >
</activity>
<activity
android:name=".OpenedClass"
android:label="OpenedClass"
android:screenOrientation="portrait" >
</activity>
<activity
android:name=".Prefs"
android:label="@string/app_name" >
<intent-filter>
<action android:name="com.jinibox.jinibox.PREFS" />
<category android:name="android.intent.category.DEFAULT" />
</intent-filter>
</activity>
<activity
android:name=".AboutUs"
android:label="@string/app_name"
android:theme="@android:style/Theme.Dialog" >
<intent-filter>
<action android:name="com.jinibox.jinibox.ABOUTUS" />
<category android:name="android.intent.category.DEFAULT" />
</intent-filter>
</activity>
</application>
</manifest>
答案 0 :(得分:0)
您可能已使用SharedPreference
将一些非布尔值放入android:key="chackbox"
文件(在您的情况下是默认值),然后将此键指定为布尔值。只需尝试将prefs.xml
中的CheckBoxPreference键属性更改为其他值,例如android:key="checkboxMusic"
或使用Prefs.java onCreate中的setSharedPreferencesName(..)
方法更改SharedPreference
文件名(。 。)方法。