Development Tip

Android : getSupportActionBar ()는 ActionBarSherlock 라이브러리에서 항상 null을 반환합니다.

yourdevel 2020. 11. 12. 20:24
반응형

Android : getSupportActionBar ()는 ActionBarSherlock 라이브러리에서 항상 null을 반환합니다.


ActionBarSherlock 라이브러리 를 사용하여 Android 앱의 탭과 함께 하위 호환 ActionBar 지원을 제공 하려고 하므로 최신 빌드를 다운로드하고 데모를 빌드 한 후 실행했습니다.

작업 표시 줄로 이동 한 다음 탭 탐색을 선택하면 매번 충돌합니다. 다음은 스택 추적입니다.

09-03 02:34:47.940: ERROR/AndroidRuntime(3078): FATAL EXCEPTION: main  
09-03 02:34:47.940: ERROR/AndroidRuntime(3078): java.lang.RuntimeException: Unable to start activity ComponentInfo{com.actionbarsherlock.sample.demos/com.actionbarsherlock.sample.demos.app.ActionBarTabNavigation}: java.lang.NullPointerException  
09-03 02:34:47.940: ERROR/AndroidRuntime(3078): at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:1748)  
09-03 02:34:47.940: ERROR/AndroidRuntime(3078): at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:1764)  
09-03 02:34:47.940: ERROR/AndroidRuntime(3078): at android.app.ActivityThread.access$1500(ActivityThread.java:122)  
09-03 02:34:47.940: ERROR/AndroidRuntime(3078): at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1002)  
09-03 02:34:47.940: ERROR/AndroidRuntime(3078): at android.os.Handler.dispatchMessage(Handler.java:99)  
09-03 02:34:47.940: ERROR/AndroidRuntime(3078): at android.os.Looper.loop(Looper.java:132)  
09-03 02:34:47.940: ERROR/AndroidRuntime(3078): at android.app.ActivityThread.main(ActivityThread.java:4025)  
09-03 02:34:47.940: ERROR/AndroidRuntime(3078): at java.lang.reflect.Method.invokeNative(Native Method)  
09-03 02:34:47.940: ERROR/AndroidRuntime(3078): at java.lang.reflect.Method.invoke(Method.java:491)  
09-03 02:34:47.940: ERROR/AndroidRuntime(3078): at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:841)  
09-03 02:34:47.940: ERROR/AndroidRuntime(3078): at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:599)  
09-03 02:34:47.940: ERROR/AndroidRuntime(3078): at dalvik.system.NativeStart.main(Native Method)  
09-03 02:34:47.940: ERROR/AndroidRuntime(3078): Caused by: java.lang.NullPointerException  
09-03 02:34:47.940: ERROR/AndroidRuntime(3078): at com.actionbarsherlock.sample.demos.app.ActionBarTabNavigation.onCreate(ActionBarTabNavigation.java:19)  
09-03 02:34:47.940: ERROR/AndroidRuntime(3078): at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1048)  
09-03 02:34:47.940: ERROR/AndroidRuntime(3078): at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:1712)  
09-03 02:34:47.940: ERROR/AndroidRuntime(3078): ... 11 more  

이 문제가 해결 될 때까지 내 앱을 진행할 수 없습니다. 많은 코드를 작성하고 내 앱에 작업 표시 줄을 설정하고 실행하려고했지만 getSupportActionBar()호출시 null 반환 값 때문에 NPE와 충돌 합니다.

관련 코드는 실제로 라이브러리의 데모에 있습니다.

public class ActionBarTabNavigation extends FragmentActivity implements ActionBar.TabListener {
    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);

        getSupportFragmentManager()
            .beginTransaction()
            .add(android.R.id.content, FragmentStackSupport.CountingFragment.newInstance(0))
            .commit();

        getSupportActionBar().setNavigationMode(ActionBar.NAVIGATION_MODE_TABS);
        for (int i = 0; i < 3; i++) {
            ActionBar.Tab tab = getSupportActionBar().newTab();
            tab.setText("Tab " + i);
            tab.setTabListener(this);
            getSupportActionBar().addTab(tab);
        }
    }

    @Override
    public void onTabReselected(Tab tab, FragmentTransaction ft) {
    }

    @Override
    public void onTabSelected(Tab tab, FragmentTransaction ft) {
        getSupportFragmentManager()
            .beginTransaction()
            .replace(android.R.id.content, FragmentStackSupport.CountingFragment.newInstance(tab.getPosition()))
            .commit();
    }

    @Override
    public void onTabUnselected(Tab tab, FragmentTransaction ft) {
    }
}

응용 프로그램에 Sherlock 테마를 추가해야합니다.

<application android:icon="@drawable/icon" android:label="@string/app_name"
        android:debuggable="false" android:theme="@style/Theme.Sherlock">

I had the same problem on the Android ICS 4.0.4. I was using requestWindowFeature(Window.FEATURE_NO_TITLE); on the FragmentActivity, but this was hiding the ActionBar on ICS+ devices that caused the getSupportActionBar() to be null.

Simply removed the:
requestWindowFeature(Window.FEATURE_NO_TITLE);

And it worked like a charm.

Hope it helps someone.


Another reason this will happen on Honeycomb+ devices is because the windowNoTitle attribute is set in your style. Get rid of that as ActionBarSherlock will automatically remove it in pre-Honeycomb devices for you.


Another reason you might get null from getSupportActionBar() is trying to call it before setContentView(R.layout.main) or in your example adding a fragment.

I refactored oncreate and mistakenly put getSupportActionBar().setNavigationMode(ActionBar.NAVIGATION_MODE_TABS); after super.onCreate(savedInstanceState);


Heres a funny one: don't set the theme to -

    android:theme="@style/Theme.NoActionbar"

Another reason you might get null from getSupportActionBar() is when the activity is used in a TabHost on Honeycomb+.


Depending how your write out code. ensure that you did set the toolbar first before calling it.

  mToolbar = (Toolbar) findViewById(R.id.toolbar);
        setSupportActionBar(mToolbar);
        getSupportActionBar().setDisplayShowHomeEnabled(true);

I just changed in Manifest

android:theme="@style/AppTheme.NoActionBar"

to:

  android:theme="@style/AppTheme"

and the error gone


whenever we set customview using sherlock library. just remove this requestWindowFeature(Window.FEATURE_NO_TITLE); like this we make customview using sherlock bar library..

            getSupportActionBar().setDisplayHomeAsUpEnabled(true);
    getSupportActionBar().setDisplayOptions(ActionBar.DISPLAY_SHOW_CUSTOM); 
    getSupportActionBar().setCustomView(R.layout.header_sherlock_xmllayout);
    header_tvleft = (TextView) findViewById(R.id.header_tvleft);
    header_tvleft.setText("Back");

I had this problem after (sillily) forgetting to call the super.onCreate()


you are declared Theme.Sherlock or Theme.Sherlock.Light as your activity or application theme in the manifest Or using a custom theme that inherits from one of these two

Example:-

<application
android:allowBackup="true"
android:icon="@drawable/ic_launcher"
android:label="@string/app_name"
android:theme="@style/Theme.Sherlock.Light" >

also u can use dark theme:-

android:theme="@style/Theme.Sherlock"

I added android:theme="@android:style/Theme.Dialog" to my activity in the Android Manifest file in an attempt to make it a dialogue activity. This too will remove the action bar hence a null pointer. Remove it or don't call getSupportActioBar


I ran into this after adding a library to my project. The remedy was to look in the library and remove any styles name "AppTheme" if you are using this same theme name in your manifest. There wasn't a conflict on my Galaxy S4, Jelly Bean, while there was a conflict on my Galaxy Tab.


I make this mistake because i add the toobar in the fragment xml file. The code to find the toolbar in fragment is this:getActivity().findViewByid(id...),but my toolbar is in fragment xml file,so that there was no toolbar found and no toolbar beeing setSipprotActionBar() and also nothing get when getSupportActionBar(). So remember :Do not put the toolbar in your fragment`s xml file.

참고URL : https://stackoverflow.com/questions/7294797/android-getsupportactionbar-always-returns-null-in-actionbarsherlock-library

반응형