2013-05-11 62 views
0

我有一個應用程序,它完美地在我的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); 

註釋掉上述行後一切工作正常 這是按鈕

<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" /> 
01的XML

你能發現我做錯了嗎? 我認爲這個問題是在這條線

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

因爲之前我有時得到的錯誤說我不能投上的ImageButton 按鈕,但顯然我不是

+0

當你在按鈕定義中擺脫了android:background屬性時會發生什麼? – MarsAtomic 2013-05-11 13:12:48

+1

在嘗試通過findViewById()方法初始化小部件之前,請確保調用了'setContentView(R.layout.layoutName)'方法。 – Sajmon 2013-05-11 13:14:16

+0

刪除背景後,它仍然崩潰。 我在onCreate的beginnig中調用setContentView,所有事情都在它後面完成 – DoubleP90 2013-05-11 13:16:25

回答

0

各種設備上的不同行爲可能會導致爲屏幕大小,方向等使用不同的佈局。

檢查是否在所有適用的佈局中定義了按鈕標識符(android:id="@+id/btn_premium_features")。

這可以解釋爲什麼它在某些設備上運行,而不在其他設備上運行。

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

它不該」除了正在執行onCreate的主/ UI線程之外,其他內容都不在其中。

+1

如果我將它評論出來或將它移動到主線程,它崩潰 – DoubleP90 2013-05-11 13:20:10

+0

@ DoubleP90做Pragnani建議。 – 2013-05-11 13:21:40