当用户尝试打开PreferenceActivity时,应用程序崩溃

时间:2012-11-16 12:20:40

标签: android

我只有一个窗口,您可以按菜单 - >偏好并启动意图

@Override
public boolean onMenuItemSelected(int featureId, MenuItem item) {
    switch (item.getItemId()) {
        case R.id.menu_settings:
            startActivity(new Intent(this, com.example.app.PreferenciasGenerales.class));
            return true;
        default:
            return false;
    }
}

自动表示必须关闭该应用程序。

  

未捕获的处理程序:由于未捕获的异常而导致主线程退出       java.lang.RuntimeException:无法启动活动ComponentInfo {com.example.app/com.example.app.PreferenciasGenerales}:
      java.lang.ClassCastException:java.lang.Integer
          在android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2496)
          在android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2512)
          在android.app.ActivityThread.access $ 2200(ActivityThread.java:119)
          在android.app.ActivityThread $ H.handleMessage(ActivityThread.java:1863)
          在android.os.Handler.dispatchMessage(Handler.java:99)
          在android.os.Looper.loop(Looper.java:123)
          在android.app.ActivityThread.main(ActivityThread.java:4363)
          at java.lang.reflect.Method.invokeNative(Native Method)
          在java.lang.reflect.Method.invoke(Method.java:521)
          在com.android.internal.os.ZygoteInit $ MethodAndArgsCaller.run(ZygoteInit.java:860)
          在com.android.internal.os.ZygoteInit.main(ZygoteInit.java:618)
          at dalvik.system.NativeStart.main(Native Method)
      引起:java.lang.ClassCastException:java.lang.Integer           在android.app.ApplicationContext $ SharedPreferencesImpl.getString(ApplicationContext.java:2572)           在android.preference.Preference.getPersistedString(Preference.java:1250)           在android.preference.ListPreference.onSetInitialValue(ListPreference.java:232)           在android.preference.Preference.dispatchSetInitialValue(Preference.java:1173)           在android.preference.Preference.onAttachedToHierarchy(Preference.java:985)           在android.preference.PreferenceGroup.addPreference(PreferenceGroup.java:156)           在android.preference.PreferenceGroup.addItemFromInflater(PreferenceGroup.java:97)           在android.preference.PreferenceGroup.addItemFromInflater(PreferenceGroup.java:38)           在android.preference.GenericInflater.rInflate(GenericInflater.java:488)           在android.preference.GenericInflater.rInflate(GenericInflater.java:493)           在android.preference.GenericInflater.inflate(GenericInflater.java:326)           在android.preference.GenericInflater.inflate(GenericInflater.java:263)           在android.preference.PreferenceManager.inflateFromResource(PreferenceManager.java:254)           在android.preference.PreferenceActivity.addPreferencesFromResource(PreferenceActivity.java:253)         - >在com.example.app.PreferenciasGenerales.onCreate(PreferenciasGenerales.java:34)           在android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1047)           在android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2459)           ......还有11个

我想推出的PreferenceActivity是:

public class PreferenciasGenerales extends PreferenceActivity {
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
    /*->*/addPreferencesFromResource(R.xml.preferencias_general);/*<- Here is the exception at StackTrace */
    }

    @Override
    protected void onStop() {
        final ProgressDialog waitDialog =
            ProgressDialog.show( this, "Wait", "i'm testing that those things you write are correct...", true);
        waitDialog.setCancelable(false);
        /* Cannot have integers array on ListPreference
         * http://code.google.com/p/android/issues/detail?id=2096 */
        String old_debug_level = PreferenceManager.getDefaultSharedPreferences(this).getString("debug_level", "3");
        PreferenceManager.getDefaultSharedPreferences(this).edit()
        .remove("debug_level")
        .putInt("debug_level", Integer.parseInt(old_debug_level))
        .commit();
    old_debug_level = null;

    String api_server = PreferenceManager.getDefaultSharedPreferences(PreferenciasGenerales.this).getString("api_server", "");
    String account_id = PreferenceManager.getDefaultSharedPreferences(PreferenciasGenerales.this).getString("account_id", "");
    String account_pass = PreferenceManager.getDefaultSharedPreferences(PreferenciasGenerales.this).getString("account_pass", "");
    String account_key = PreferenceManager.getDefaultSharedPreferences(PreferenciasGenerales.this).getString("account_key", "");
    boolean debug_enabled= PreferenceManager.getDefaultSharedPreferences(PreferenciasGenerales.this).getBoolean("debug_enabled", false);
    int debug_level = PreferenceManager.getDefaultSharedPreferences(PreferenciasGenerales.this).getInt("debug_level", 3);

    try {
                 // Connect to API to test credentials...

您需要的与PreferenceActivity相关的更多内容...我删除了所有strings.xml值,因为它们太多而且是西班牙语。所有字符串,如title,hint,dialogTitle都存在并且是正确的。他们从事其他活动

/res/xml/preferencias_general.xml

<PreferenceScreen xmlns:android="http://schemas.android.com/apk/res/android" >
    <PreferenceCategory>
        <EditTextPreference android:key="api_server"/>
    </PreferenceCategory>
    <PreferenceCategory android:title="@string/preferencias_cuenta_categoria_title">
        <EditTextPreference android:key="account_id"  />
        <EditTextPreference android:key="account_pass" />
        <EditTextPreference android:key="account_key"  />
    </PreferenceCategory>
        <PreferenceCategory android:key="debug_enabled" />
        <ListPreference android:key="debug_level" android:entryValues="@array/debug_level_entryValues" android:entries="@array/debug_level_entries" />
</PreferenceCategory>

/res/values/preferencias_general.xml

<?xml version="1.0" encoding="utf-8"?>
<resources>
    <string-array name="debug_level_entries">
        <item>VERBOSE</item>
        <item>DEBUG</item>
        <item>INFO</item>
        <item>WARN</item>
        <item>ERROR</item>
    </string-array>
    <string-array name="debug_level_entryValues">
        <item>1</item>
        <item>2</item>
        <item>3</item>
        <item>4</item>
        <item>5</item>
   </string-array>

-

更新1 回答@CommonsWere我把数据擦掉了,它显示它不完整:

ListPreference when app settings are wiped out

在此之前,我尝试退出并引发新的异常:

  

java.lang.RuntimeException:无法停止活动{com.merinosa.kimera / com.merinosa.kimera.PreferenciasGenerales}:       java.lang.IllegalStateException:目标主机不能为null,或者在参数中设置。           在android.app.ActivityThread.performDestroyActivity(ActivityThread.java:3421)           在android.app.ActivityThread.handleDestroyActivity(ActivityThread.java:3487)           在android.app.ActivityThread.access $ 2800(ActivityThread.java:119)           在android.app.ActivityThread $ H.handleMessage(ActivityThread.java:1896)           在android.os.Handler.dispatchMessage(Handler.java:99)           在android.os.Looper.loop(Looper.java:123)           在android.app.ActivityThread.main(ActivityThread.java:4363)           at java.lang.reflect.Method.invokeNative(Native Method)           在java.lang.reflect.Method.invoke(Method.java:521)           在com.android.internal.os.ZygoteInit $ MethodAndArgsCaller.run(ZygoteInit.java:860)           在com.android.internal.os.ZygoteInit.main(ZygoteInit.java:618)           at dalvik.system.NativeStart.main(Native Method)       引发者:java.lang.IllegalStateException:目标主机不能为null,或者在参数中设置。           at org.apache.http.impl.client.DefaultRequestDirector.determineRoute(DefaultRequestDirector.java:561)           在org.apache.http.impl.client.DefaultRequestDirector.execute(DefaultRequestDirector.java:292)           在org.apache.http.impl.client.AbstractHttpClient.execute(AbstractHttpClient.java:555)           在org.apache.http.impl.client.AbstractHttpClient.execute(AbstractHttpClient.java:487)           在org.apache.http.impl.client.AbstractHttpClient.execute(AbstractHttpClient.java:465)           at com.example.app.PreferenciasGenerales.onStop(PreferenciasGenerales.java:78)           在android.app.Instrumentation.callActivityOnStop(Instrumentation.java:1169)           在android.app.Activity.performStop(Activity.java:3797)           在android.app.ActivityThread.performDestroyActivity(ActivityThread.java:3416)           ......还有11个

我应该在哪里测试配置是否正确呢? 如果我再次运行app,它会再次出现第一个错误。

-

更新2

我的新错误与原始问题无关,因此可以设置为已回答。 错误重新出现的原因是因为我修改了CommonsWare在接受的答案上说的设置 当我从设置中退出时,它执行onStop()并将SharedPreferences修改为整数,使其成为&#34; unasteable&#34;。

必须正确调查新错误,并在需要时再次询问。

1 个答案:

答案 0 :(得分:3)

此应用在您用于测试的任何设备或模拟器上已保存SharedPreferences,其中保存的值为Integer,您现在正尝试将其加载到{{ 1}}期望Preference。尝试通过“设置”清除应用程序的数据,或者卸载并重新安装应用程序,以解决此问题。

另请注意,在您当前的偏好XML中,String将保存为debug_level,但您尝试通过String检索它。