4

我無法弄清楚這一個。我自定義我的ActionBar(並且是我的應用程序是3.0+)代碼在設備上運行良好(使用摩托羅拉xoom,如果它很重要)。但是當我在模擬器上運行它會引發NPE.This是我的代碼爲ActionBarActionBar拋出NullPointerException

/** 
* Customizes Action bar sets background color and assigns a layout to it 
*/ 
private void customActionBar() { 
    Log.v(TAG, "customizing ActionBar Entry"); 
    ActionBar actionBar = getActionBar(); 
    Log.v(TAG, "customizing ActionBar : "+actionBar.toString()); 
      //This is where i get NPE 
    actionBar.setBackgroundDrawable(new ColorDrawable(Color 
      .parseColor(Constants.COLOR))); 
    Log.v(TAG, "customizing ActionBar -Background color : "); 

    LayoutInflater inflater = (LayoutInflater) getSystemService(LAYOUT_INFLATER_SERVICE); 
    View view = inflater.inflate(R.layout.custom_action, null); 
    // lay.setLayoutParams(new ActionBar.LayoutParams(
    // android.app.ActionBar.LayoutParams.MATCH_PARENT, 
    // android.app.ActionBar.LayoutParams.MATCH_PARENT)); 
    actionBar.setCustomView(view); 
    actionBar.setDisplayOptions(ActionBar.DISPLAY_SHOW_CUSTOM); 
    Log.v(TAG, "customizing ActionBar Exit"); 
} 

編輯:logcat的

V/>>> FullPdfViewerActivity(438): customizing ActionBar Entry 
D/AndroidRuntime(438): Shutting down VM 
W/dalvikvm(438): threadid=1: thread exiting with uncaught exception (group=0x40014760) 
E/AndroidRuntime(438): FATAL EXCEPTION: main 
E/AndroidRuntime(438): java.lang.RuntimeException: Unable to start activity         
ComponentInfo{com.test.android.tester/com.test.android.tester.core.AwesomePagerActivity}:   java.lang.NullPointerException 
E/AndroidRuntime(438): at  android.app.ActivityThread.performLaunchActivity(ActivityThread.java:1736) 
E/AndroidRuntime(438): at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:1752) 
E/AndroidRuntime(438): at android.app.ActivityThread.access$1500(ActivityThread.java:123) 
E/AndroidRuntime(438): at android.app.ActivityThread$H.handleMessage(ActivityThread.java:993) 
E/AndroidRuntime(438): at android.os.Handler.dispatchMessage(Handler.java:99) 
E/AndroidRuntime(438): at android.os.Looper.loop(Looper.java:126) 
E/AndroidRuntime(438): at android.app.ActivityThread.main(ActivityThread.java:3997) 
E/AndroidRuntime(438): at java.lang.reflect.Method.invokeNative(Native Method) 
E/AndroidRuntime(438): at java.lang.reflect.Method.invoke(Method.java:491) 
E/AndroidRuntime(438): at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:841) 
E/AndroidRuntime(438): at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:599) 
E/AndroidRuntime(438): at dalvik.system.NativeStart.main(Native Method) 
E/AndroidRuntime(438): Caused by: java.lang.NullPointerException 
E/AndroidRuntime(438): at com.test.android.tester.core.AwesomePagerActivity.customActionBar(AwesomePagerActivity.java:227) 
E/AndroidRuntime(438): at com.test.android.tester.core.AwesomePagerActivity.onCreate(AwesomePagerActivity.java:187) 
E/AndroidRuntime(438): at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1048) 
E/AndroidRuntime(438): at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:1700) 
E/AndroidRuntime(438): ... 11 more 

我把這個從我的onCreate()。我很擔心它是否會爲其他設備的工作嗎?

TIA

+0

你能否提供logcat? – 2011-12-14 03:32:09

+0

早些時候它被setBackgroundDrawable。但是當我用toString()它顯示NPE.Its那裏在評論 – 2011-12-14 03:33:04

回答

10

我認爲,getActionBar()返回null。我看到getActionBar()setContentView()之前被調用的行爲,但只在Android 3.0和3.1上,在Android 3.2上沒有關係。 嘗試撥打setContentView,然後再自定義操作欄

0

請記住,在某些情況下,您可能需要在onActivityCreated中執行任何呼叫,因爲視圖可能未準備就緒。

相關問題