应用程序在2.2版中崩溃

时间:2012-08-16 05:06:25

标签: android

我创建了一个Android应用程序,它可以在Android 2.3+所有设备中完美运行。但当我试图在2.2中运行但它崩溃并在日志中给出错误,

08-16 10:22:41.323: E/dalvikvm(442): Could not find class       'android.os.StrictMode$ThreadPolicy$Builder', referenced from method com.uks.android.mssga.activity.LoginActivity.onCreate
08-16 10:22:41.333: W/dalvikvm(442): VFY: unable to resolve new-instance 39 (Landroid/os/StrictMode$ThreadPolicy$Builder;) in Lcom/uks/android/mssga/activity/LoginActivity;
08-16 10:22:41.333: D/dalvikvm(442): VFY: replacing opcode 0x22 at 0x000a
08-16 10:22:41.333: D/dalvikvm(442): VFY: dead code 0x000c-009a in Lcom/uks/android/mssga/activity/LoginActivity;.onCreate (Landroid/os/Bundle;)V
08-16 10:22:41.423: D/dalvikvm(442): GC_EXTERNAL_ALLOC freed 744 objects / 59064 bytes in 68ms
08-16 10:22:41.483: D/AndroidRuntime(442): Shutting down VM
08-16 10:22:41.483: W/dalvikvm(442): threadid=1: thread exiting with uncaught exception (group=0x4001d800)
08-16 10:22:41.483: E/AndroidRuntime(442): FATAL EXCEPTION: main
08-16 10:22:41.483: E/AndroidRuntime(442): java.lang.NoClassDefFoundError: android.os.StrictMode$ThreadPolicy$Builder
08-16 10:22:41.483: E/AndroidRuntime(442):  at com.uks.android.mssga.activity.LoginActivity.onCreate(LoginActivity.java:58)
08-16 10:22:41.483: E/AndroidRuntime(442):  at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1047)
08-16 10:22:41.483: E/AndroidRuntime(442):  at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2627)
08-16 10:22:41.483: E/AndroidRuntime(442):  at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2679)
08-16 10:22:41.483: E/AndroidRuntime(442):  at android.app.ActivityThread.access$2300(ActivityThread.java:125)
08-16 10:22:41.483: E/AndroidRuntime(442):  at android.app.ActivityThread$H.handleMessage(ActivityThread.java:2033)
08-16 10:22:41.483: E/AndroidRuntime(442):  at android.os.Handler.dispatchMessage(Handler.java:99)
08-16 10:22:41.483: E/AndroidRuntime(442):  at android.os.Looper.loop(Looper.java:123)
08-16 10:22:41.483: E/AndroidRuntime(442):  at android.app.ActivityThread.main(ActivityThread.java:4627)
08-16 10:22:41.483: E/AndroidRuntime(442):  at java.lang.reflect.Method.invokeNative(Native Method)
08-16 10:22:41.483: E/AndroidRuntime(442):  at java.lang.reflect.Method.invoke(Method.java:521)
08-16 10:22:41.483: E/AndroidRuntime(442):  at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:868)
08-16 10:22:41.483: E/AndroidRuntime(442):  at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:626)
08-16 10:22:41.483: E/AndroidRuntime(442):  at dalvik.system.NativeStart.main(Native Method)
08-16 10:22:44.503: I/Process(442): Sending signal. PID: 442 SIG: 9

LoginActivity代码是,

public void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.login);

    StrictMode.ThreadPolicy policy = new StrictMode.ThreadPolicy.Builder()
            .permitAll().build();
    StrictMode.setThreadPolicy(policy);

            // Other stuff
 }

1 个答案:

答案 0 :(得分:3)

Android 2.3(API级别9)中引入了

StrictMode.ThreadPolicy.Builder,因此尝试在早于此版本的版本中调用它将失败。

要阻止它在2.2及更早版本上崩溃,您可以选择: *删除ThreadPolicy代码(因为它是一个开发人员调试工具,提供您正在做的坏事的警告) 或
*在使用它之前使用反射来确定类是否存在