我有一个可以在我的 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 但显然我不是