Android的错误:应用程序已意外停止,请重试 [英] Android error: The application has stopped unexpectedly please try again

查看:103
本文介绍了Android的错误:应用程序已意外停止,请重试的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我做了一个应用程序,它只是正常工作。它显示次应用的数目已经启动。这里的code:

 进口android.app.Activity;
进口android.content.Shared preferences;
进口android.os.Bundle;
。进口的Andr​​oid preference preferenceManager。
进口android.widget.TextView;公共类preferencesDemo延伸活动{
    / **当第一次创建活动调用。 * /
    @覆盖
    公共无效的onCreate(捆绑savedInstanceState){
        super.onCreate(savedInstanceState);
        的setContentView(R.layout.main);        //获取应用程序的共享preferences
        共享preferences APP_ preferences =
            preferenceManager.getDefaultShared preferences(本);        //获取运行计数器的值
        INT计数器= APP_ preferences.getInt(反,0);        //更新的TextView
        TextView的文本=(TextView的)findViewById(R.id.text);
        text.setText(这个程序已经启动+专柜+时代。);        //增加计数器
        共享preferences.Editor编辑= APP_ preferences.edit();
        editor.putInt(专柜,专柜++);
        editor.commit(); // 很重要
    }
}

如果我改变字符串,它抛出一个错误,说int变量:应用程序已意外停止。请稍后再试。这里的code抛出错误:

 进口android.app.Activity;
进口android.content.Shared preferences;
进口android.os.Bundle;
。进口的Andr​​oid preference preferenceManager。
进口android.widget.TextView;公共类preF延伸活动{
    / **当第一次创建活动调用。 * /
    @覆盖
    公共无效的onCreate(捆绑savedInstanceState){
        super.onCreate(savedInstanceState);
        的setContentView(R.layout.main);        共享preferences preFS = preferenceManager.getDefaultShared preferences(本);
        字符串C = prefs.getString(C,你好);
        TextView的T =(的TextView)findViewById(R.id.te);
        t.append(C);        共享preferences.Editor编辑= prefs.edit();
        edit.putString(C,有);
        edit.commit();
    }

}

谁能告诉我其中m我的问题呢?提供:我已经正确main.xml中文件中提及的TextView的

下面是LogCat中的细节:

  07-11 15:49:12.908:DEBUG / AndroidRuntime(521):>>>>>>>>>>> >>> AndroidRuntime START<<<<<<<<<<<<<<
07-11 15:49:12.918:DEBUG / AndroidRuntime(521):CheckJNI为ON
07-11 15:49:15.459:DEBUG / AndroidRuntime(521):---注册本地函数---
07-11 15:49:19.049:DEBUG / AndroidRuntime(521):关闭VM
07-11 15:49:19.049:DEBUG / dalvikvm(521):调试器分离;对象注册了1项
07-11 15:49:19.079:信息/ AndroidRuntime(521):注:连接线的捆扎线#4失败
07-11 15:49:20.349:调试/ AndroidRuntime(530):>>>>>>>>>>>>>> AndroidRuntime START<<<<<<<<<<<<<<
07-11 15:49:20.359:调试/ AndroidRuntime(530):CheckJNI为ON
07-11 15:49:20.739:DEBUG / AndroidRuntime(530):---注册本地函数---
07-11 15:49:22.492:信息/ ActivityManager(68):启动活动:意向{行动= android.intent.action.MAIN猫= [android.intent.category.LAUNCHER] FLG = 0x10000000的CMP = com.webkul。 preF /.$ p $} PF
07-11 15:49:26.996:WARN / ActivityManager(68):为HistoryRecord活动暂停超时{43f1d590 com.android.launcher / com.android.launcher2.Launcher}
07-11 15:49:27.484:DEBUG / AndroidRuntime(530):关闭VM
07-11 15:49:27.619:DEBUG / dalvikvm(530):调试器分离;对象注册了1项
07-11 15:49:28.649:信息/ ActivityManager(68):启动PROC com.webkul $ P $粉煤为活动com.webkul preF /.preF:PID = 537 UID = 10038导报= {}
07-11 15:49:31.339:DEBUG / AndroidRuntime(537):关闭VM
07-11 15:49:31.339:WARN / dalvikvm(537):主题ID = 1:螺纹未捕获的异常退出(组= 0x4001d800)
07-11 15:49:31.438:ERROR / AndroidRuntime(537):致命异常:主要
07-11 15:49:31.438:ERROR / AndroidRuntime(537):了java.lang.RuntimeException:无法启动活动ComponentInfo {com.webkul preF / com.webkul preF preF。 }:java.lang.ClassCastException:java.lang.Integer中
07-11 15:49:31.438:ERROR / AndroidRuntime(537):在android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2663)
07-11 15:49:31.438:ERROR / AndroidRuntime(537):在android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2679)
07-11 15:49:31.438:ERROR / AndroidRuntime(537):在android.app.ActivityThread.access $ 2300(ActivityThread.java:125)
07-11 15:49:31.438:ERROR / AndroidRuntime(537):在android.app.ActivityThread $ H.handleMessage(ActivityThread.java:2033)
07-11 15:49:31.438:ERROR / AndroidRuntime(537):在android.os.Handler.dispatchMessage(Handler.java:99)
07-11 15:49:31.438:ERROR / AndroidRuntime(537):在android.os.Looper.loop(Looper.java:123)
07-11 15:49:31.438:ERROR / AndroidRuntime(537):在android.app.ActivityThread.main(ActivityThread.java:4627)
07-11 15:49:31.438:ERROR / AndroidRuntime(537):在java.lang.reflect.Method.invokeNative(本机方法)
07-11 15:49:31.438:ERROR / AndroidRuntime(537):在java.lang.reflect.Method.invoke(Method.java:521)
07-11 15:49:31.438:ERROR / AndroidRuntime(537):在com.android.internal.os.ZygoteInit $ MethodAndArgsCaller.run(ZygoteInit.java:868)
07-11 15:49:31.438:ERROR / AndroidRuntime(537):在com.android.internal.os.ZygoteInit.main(ZygoteInit.java:626)
07-11 15:49:31.438:ERROR / AndroidRuntime(537):在dalvik.system.NativeStart.main(本机方法)
07-11 15:49:31.438:ERROR / AndroidRuntime(537):java.lang.ClassCastException:产生的原因为java.lang.Integer
07-11 15:49:31.438:ERROR / AndroidRuntime(537):在android.app.ContextImpl $共享preferencesImpl.getString(ContextImpl.java:2699)
07-11 15:49:31.438:ERROR / AndroidRuntime(537):在com.webkul preF pref.onCreate(pref.java:17)。
07-11 15:49:31.438:ERROR / AndroidRuntime(537):在android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1047)
07-11 15:49:31.438:ERROR / AndroidRuntime(537):在android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2627)
07-11 15:49:31.438:ERROR / AndroidRuntime(537):11 ...更多
07-11 15:49:31.646:WARN / ActivityManager(68):强制完成活动com.webkul preF /.preF
07-11 15:49:34.053:DEBUG / dalvikvm(68):GC_FOR_MALLOC释放13421对象/在1300ms 615120字节
07-11 15:49:34.129:WARN / ActivityManager(68):{。43fd3ee8 com.webkul preF /.$ p $} PF为HistoryRecord活动暂停超时
07-11 15:49:34.739:WARN / ActivityManager(68):启动超时已过期,放弃唤醒锁!
07-11 15:49:37.479:信息/流程(537):发送信号。 PID:537 SIG:9
07-11 15:49:37.559:WARN / InputManagerService(68):窗口已经集中,忽视聚焦增益:com.android.internal.view.IInputMethodClient$Stub$Proxy@43ebfa20
07-11 15:49:37.779:信息/ ActivityManager(68):流程com.webkul preF(PID 537)已经死亡。
07-11 15:49:37.889:DEBUG / SntpClient(68):请求时失败:java.net.SocketException异常:地​​址协议不支持
07-11 15:49:46.680:WARN / ActivityManager(68):活动破坏超时HistoryRecord {43fd3ee8 com.webkul preF /.$ p $} PF


解决方案

OKK​​了,现在..添加上下文和refrenced它通过它..

 进口android.app.Activity;
进口android.content.Context;
进口android.content.Shared preferences;
进口android.os.Bundle;
。//进口的Andr​​oid preference preferenceManager;
进口android.widget.TextView;公共类preF延伸活动{
    / **当第一次创建活动调用。 * /
    @覆盖
    公共无效的onCreate(捆绑savedInstanceState){
        super.onCreate(savedInstanceState);
        的setContentView(R.layout.main);        上下文mContext = this.getApplicationContext();
        共享preferences preFS = mContext.getShared preferences(preF,0);
        字符串C = prefs.getString(C,你好);
        TextView的T =(的TextView)findViewById(R.id.te);
        t.append(C);        共享preferences.Editor编辑= prefs.edit();
        edit.putString(C,有);
        edit.commit();
    }
}

I made an app which is just working fine. It displays the number of times app has been started. Here's the code:

import android.app.Activity;
import android.content.SharedPreferences;
import android.os.Bundle;
import android.preference.PreferenceManager;
import android.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
    }
}

If I change the int variable to string it throws an error saying: "app has stopped unexpectedly.. please try again later". Here's the code that throws error:

import android.app.Activity;
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);

        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();
    }

}

can anyone tell me where m I going wrong? Provided: I have correctly mentioned the textview in main.xml file.

Here are the LogCat details:

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=android.intent.action.MAIN cat=[android.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.android.launcher/com.android.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 android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2663)
07-11 15:49:31.438: ERROR/AndroidRuntime(537):     at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2679)
07-11 15:49:31.438: ERROR/AndroidRuntime(537):     at android.app.ActivityThread.access$2300(ActivityThread.java:125)
07-11 15:49:31.438: ERROR/AndroidRuntime(537):     at android.app.ActivityThread$H.handleMessage(ActivityThread.java:2033)
07-11 15:49:31.438: ERROR/AndroidRuntime(537):     at android.os.Handler.dispatchMessage(Handler.java:99)
07-11 15:49:31.438: ERROR/AndroidRuntime(537):     at android.os.Looper.loop(Looper.java:123)
07-11 15:49:31.438: ERROR/AndroidRuntime(537):     at android.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.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:868)
07-11 15:49:31.438: ERROR/AndroidRuntime(537):     at com.android.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 android.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 android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1047)
07-11 15:49:31.438: ERROR/AndroidRuntime(537):     at android.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.android.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}

解决方案

okk got that now.. added a Context and refrenced it through it..

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();
    }
}

这篇关于Android的错误:应用程序已意外停止,请重试的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆