奇怪的力量关闭

时间:2013-05-11 13:10:23

标签: android

我的应用程序在我的Galaxy Nexus上完美运行,但是当我在模拟器上运行时(仅在某些设置上,一个是带有JB的平板电脑,另一个是带有android 2.1的ldpi)它会崩溃。

logcat看起来像这样

03-29 16:26:16.842: D/AndroidRuntime(526): Shutting down VM
03-29 16:26:16.842: W/dalvikvm(526): threadid=3: thread exiting with uncaught exception (group=0x4001b188)
03-29 16:26:16.842: E/AndroidRuntime(526): Uncaught handler: thread main exiting due to uncaught exception
03-29 16:26:16.862: E/AndroidRuntime(526): java.lang.RuntimeException: Unable to start activity ComponentInfo{com.doublep.wakey/com.doublep.wakey.Bulb}: java.lang.NullPointerException
03-29 16:26:16.862: E/AndroidRuntime(526):  at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2496)
03-29 16:26:16.862: E/AndroidRuntime(526):  at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2512)
03-29 16:26:16.862: E/AndroidRuntime(526):  at android.app.ActivityThread.access$2200(ActivityThread.java:119)
03-29 16:26:16.862: E/AndroidRuntime(526):  at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1863)
03-29 16:26:16.862: E/AndroidRuntime(526):  at android.os.Handler.dispatchMessage(Handler.java:99)
03-29 16:26:16.862: E/AndroidRuntime(526):  at android.os.Looper.loop(Looper.java:123)
03-29 16:26:16.862: E/AndroidRuntime(526):  at android.app.ActivityThread.main(ActivityThread.java:4363)
03-29 16:26:16.862: E/AndroidRuntime(526):  at java.lang.reflect.Method.invokeNative(Native Method)
03-29 16:26:16.862: E/AndroidRuntime(526):  at java.lang.reflect.Method.invoke(Method.java:521)
03-29 16:26:16.862: E/AndroidRuntime(526):  at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:860)
03-29 16:26:16.862: E/AndroidRuntime(526):  at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:618)
03-29 16:26:16.862: E/AndroidRuntime(526):  at dalvik.system.NativeStart.main(Native Method)
03-29 16:26:16.862: E/AndroidRuntime(526): Caused by: java.lang.NullPointerException
03-29 16:26:16.862: E/AndroidRuntime(526):  at com.doublep.wakey.Bulb.onCreate(Bulb.java:465)
03-29 16:26:16.862: E/AndroidRuntime(526):  at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1047)
03-29 16:26:16.862: E/AndroidRuntime(526):  at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2459)
03-29 16:26:16.862: E/AndroidRuntime(526):  ... 11 more
03-29 16:26:16.882: I/dalvikvm(526): threadid=7: reacting to signal 3
03-29 16:26:16.882: E/dalvikvm(526): Unable to open stack trace file '/data/anr/traces.txt': Permission denied
03-29 16:26:18.752: I/Process(526): Sending signal. PID: 526 SIG: 9

我发现,如果我注释掉一个按钮,问题就会消失(之前是一个imageButton,但是我把它转换为按钮,因为imageButton工作正常)

//This is a global variable declariation
    Button premiumFeaturesBtn; 


//this is inside the oncreate
    premiumFeaturesBtn = (Button) findViewById(R.id.btn_premium_features); 

//this is inside a thread
    premiumFeaturesBtn.setOnClickListener(new View.OnClickListener() {
                        @Override
                        public void onClick(View v) {
                            Intent premiumPurchase = new Intent(Bulb.this, PremiumPurchase.class);
                            startActivity(premiumPurchase);
                       }
                    });

//This is at the end of my onCreate
    Typeface bebas = Typeface.createFromAsset(getAssets(), "fonts/bebas_neue.otf");
            premiumFeaturesBtn.setTypeface(bebas);

在评论出上述内容后,一切正常 这是按钮的xml

<Button
                    android:id="@+id/btn_premium_features"
                    android:layout_width="wrap_content"
                    android:layout_height="wrap_content"
                    android:layout_centerHorizontal="true"
                    android:layout_marginTop="15dp"
                    android:background="@drawable/img_btn_premium"
                    android:padding="10dp"
                    android:text="@string/premium_features"
                    android:textSize="25sp" />

你能发现我做错了吗? 我认为问题出在这一行

premiumFeaturesBtn = (Button) findViewById(R.id.btn_premium_features); 

因为在我有时收到错误,说我无法在ImageButton上投射Button 但显然我不是

2 个答案:

答案 0 :(得分:0)

//this is inside a thread
premiumFeaturesBtn.setOnClickListener(new View.OnClickListener() {

它不应该在onCreate正在执行的主/ UI线程之外。

答案 1 :(得分:0)

各种设备上的不同行为可能会导致为屏幕尺寸,方向等定义不同的布局。

检查是否在所有适用的布局中定义了按钮标识符(android:id="@+id/btn_premium_features")。

这可以解释为什么它在某些设备上运行而在其他设备上运行。