有 Java 编程相关的问题?

你可以在下面搜索框中键入要查询的问题!

java Android错误:应用程序意外停止,请重试

我制作了一个应用程序,它运行得很好。它显示应用程序已启动的次数。代码如下:

import 安卓.app.Activity;
import 安卓.content.SharedPreferences;
import 安卓.os.Bundle;
import 安卓.preference.PreferenceManager;
import 安卓.widget.TextView;

public class PreferencesDemo extends Activity {
    /** Called when the activity is first created. */
    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.main);

        // Get the app's shared preferences
        SharedPreferences app_preferences = 
            PreferenceManager.getDefaultSharedPreferences(this);

        // Get the value for the run counter
        int counter = app_preferences.getInt("counter", 0);

        // Update the TextView
        TextView text = (TextView) findViewById(R.id.text);
        text.setText("This app has been started " + counter + " times.");

        // Increment the counter
        SharedPreferences.Editor editor = app_preferences.edit();
        editor.putInt("counter", ++counter);
        editor.commit(); // Very important
    }
}

如果我将int变量更改为string,它将抛出一个错误,提示:“应用程序意外停止..请稍后再试”。下面是引发错误的代码:

import 安卓.app.Activity;
import 安卓.content.SharedPreferences;
import 安卓.os.Bundle;
import 安卓.preference.PreferenceManager;
import 安卓.widget.TextView;

public class pref extends Activity {
    /** Called when the activity is first created. */
    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.main);

        SharedPreferences prefs = PreferenceManager.getDefaultSharedPreferences(this);
        String c = prefs.getString("c", "hello");
        TextView t = (TextView)findViewById(R.id.te);
        t.append(c);

        SharedPreferences.Editor edit = prefs.edit();
        edit.putString("c", "There");
        edit.commit();
    }

}

谁能告诉我哪里出了问题?提供:我已经正确地提到了主要的文本视图。xml文件

以下是LogCat的详细信息:

07-11 15:49:12.908: DEBUG/AndroidRuntime(521): >>>>>>>>>>>>>> AndroidRuntime START <<<<<<<<<<<<<<
07-11 15:49:12.918: DEBUG/AndroidRuntime(521): CheckJNI is ON
07-11 15:49:15.459: DEBUG/AndroidRuntime(521): --- registering native functions ---
07-11 15:49:19.049: DEBUG/AndroidRuntime(521): Shutting down VM
07-11 15:49:19.049: DEBUG/dalvikvm(521): Debugger has detached; object registry had 1 entries
07-11 15:49:19.079: INFO/AndroidRuntime(521): NOTE: attach of thread 'Binder Thread #4' failed
07-11 15:49:20.349: DEBUG/AndroidRuntime(530): >>>>>>>>>>>>>> AndroidRuntime START <<<<<<<<<<<<<<
07-11 15:49:20.359: DEBUG/AndroidRuntime(530): CheckJNI is ON
07-11 15:49:20.739: DEBUG/AndroidRuntime(530): --- registering native functions ---
07-11 15:49:22.492: INFO/ActivityManager(68): Starting activity: Intent { act=安卓.intent.action.MAIN cat=[安卓.intent.category.LAUNCHER] flg=0x10000000 cmp=com.webkul.pref/.pref }
07-11 15:49:26.996: WARN/ActivityManager(68): Activity pause timeout for HistoryRecord{43f1d590 com.安卓.launcher/com.安卓.launcher2.Launcher}
07-11 15:49:27.484: DEBUG/AndroidRuntime(530): Shutting down VM
07-11 15:49:27.619: DEBUG/dalvikvm(530): Debugger has detached; object registry had 1 entries
07-11 15:49:28.649: INFO/ActivityManager(68): Start proc com.webkul.pref for activity com.webkul.pref/.pref: pid=537 uid=10038 gids={}
07-11 15:49:31.339: DEBUG/AndroidRuntime(537): Shutting down VM
07-11 15:49:31.339: WARN/dalvikvm(537): threadid=1: thread exiting with uncaught exception (group=0x4001d800)
07-11 15:49:31.438: ERROR/AndroidRuntime(537): FATAL EXCEPTION: main
07-11 15:49:31.438: ERROR/AndroidRuntime(537): java.lang.RuntimeException: Unable to start activity ComponentInfo{com.webkul.pref/com.webkul.pref.pref}: java.lang.ClassCastException: java.lang.Integer
07-11 15:49:31.438: ERROR/AndroidRuntime(537):     at 安卓.app.ActivityThread.performLaunchActivity(ActivityThread.java:2663)
07-11 15:49:31.438: ERROR/AndroidRuntime(537):     at 安卓.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2679)
07-11 15:49:31.438: ERROR/AndroidRuntime(537):     at 安卓.app.ActivityThread.access$2300(ActivityThread.java:125)
07-11 15:49:31.438: ERROR/AndroidRuntime(537):     at 安卓.app.ActivityThread$H.handleMessage(ActivityThread.java:2033)
07-11 15:49:31.438: ERROR/AndroidRuntime(537):     at 安卓.os.Handler.dispatchMessage(Handler.java:99)
07-11 15:49:31.438: ERROR/AndroidRuntime(537):     at 安卓.os.Looper.loop(Looper.java:123)
07-11 15:49:31.438: ERROR/AndroidRuntime(537):     at 安卓.app.ActivityThread.main(ActivityThread.java:4627)
07-11 15:49:31.438: ERROR/AndroidRuntime(537):     at java.lang.reflect.Method.invokeNative(Native Method)
07-11 15:49:31.438: ERROR/AndroidRuntime(537):     at java.lang.reflect.Method.invoke(Method.java:521)
07-11 15:49:31.438: ERROR/AndroidRuntime(537):     at com.安卓.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:868)
07-11 15:49:31.438: ERROR/AndroidRuntime(537):     at com.安卓.internal.os.ZygoteInit.main(ZygoteInit.java:626)
07-11 15:49:31.438: ERROR/AndroidRuntime(537):     at dalvik.system.NativeStart.main(Native Method)
07-11 15:49:31.438: ERROR/AndroidRuntime(537): Caused by: java.lang.ClassCastException: java.lang.Integer
07-11 15:49:31.438: ERROR/AndroidRuntime(537):     at 安卓.app.ContextImpl$SharedPreferencesImpl.getString(ContextImpl.java:2699)
07-11 15:49:31.438: ERROR/AndroidRuntime(537):     at com.webkul.pref.pref.onCreate(pref.java:17)
07-11 15:49:31.438: ERROR/AndroidRuntime(537):     at 安卓.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1047)
07-11 15:49:31.438: ERROR/AndroidRuntime(537):     at 安卓.app.ActivityThread.performLaunchActivity(ActivityThread.java:2627)
07-11 15:49:31.438: ERROR/AndroidRuntime(537):     ... 11 more
07-11 15:49:31.646: WARN/ActivityManager(68):   Force finishing activity com.webkul.pref/.pref
07-11 15:49:34.053: DEBUG/dalvikvm(68): GC_FOR_MALLOC freed 13421 objects / 615120 bytes in 1300ms
07-11 15:49:34.129: WARN/ActivityManager(68): Activity pause timeout for HistoryRecord{43fd3ee8 com.webkul.pref/.pref}
07-11 15:49:34.739: WARN/ActivityManager(68): Launch timeout has expired, giving up wake lock!
07-11 15:49:37.479: INFO/Process(537): Sending signal. PID: 537 SIG: 9
07-11 15:49:37.559: WARN/InputManagerService(68): Window already focused, ignoring focus gain of: com.安卓.internal.view.IInputMethodClient$Stub$Proxy@43ebfa20
07-11 15:49:37.779: INFO/ActivityManager(68): Process com.webkul.pref (pid 537) has died.
07-11 15:49:37.889: DEBUG/SntpClient(68): request time failed: java.net.SocketException: Address family not supported by protocol
07-11 15:49:46.680: WARN/ActivityManager(68): Activity destroy timeout for HistoryRecord{43fd3ee8 com.webkul.pref/.pref}

共 (3) 个答案

  1. # 1 楼答案

    okk现在明白了。。添加了一个上下文并对其进行了引用

    import android.app.Activity;
    import android.content.Context;
    import android.content.SharedPreferences;
    import android.os.Bundle;
    //import android.preference.PreferenceManager;
    import android.widget.TextView;
    
    public class pref extends Activity {
        /** Called when the activity is first created. */
        @Override
        public void onCreate(Bundle savedInstanceState) {
            super.onCreate(savedInstanceState);
            setContentView(R.layout.main);
    
            Context mContext = this.getApplicationContext();
            SharedPreferences prefs = mContext.getSharedPreferences("pref", 0);
            String c = prefs.getString("c", "hello");
            TextView t = (TextView)findViewById(R.id.te);
            t.append(c);
    
            SharedPreferences.Editor edit = prefs.edit();
            edit.putString("c", "There");
            edit.commit();
        }
    }
    
  2. # 2 楼答案

    来自getString()的文档来自SharedPreferences

    Returns the preference value if it exists, or defValue. Throws ClassCastException if there is a preference with this name that is not a String.

    所以我猜你有一个“c”键,它的值和字符串不同。可能是你用putInt()错误地添加了它

    尝试用getInt()检索“c”键,看看是否仍然存在异常

  3. # 3 楼答案

    您需要学习如何在Eclipse中进行调试,以及如何使用ADBDDMS工具

    为了获得有关异常/强制关闭的更多详细信息,您需要在Eclipse中查找一个名为Logcat(您将在DDMS透视图中找到)的视图,在那里您将找到问题发生的时间/内容以及在哪一行的详细回溯

    为此,你应该阅读一篇关于Debugging in Android using Eclipse的完整文章

    alt text
    (来源:droidnova.com

    编辑

    它清楚地说明了Caused by: java.lang.ClassCastException: java.lang.Integer上的问题是什么getString