简体   繁体   English

Android:getSupportActionBar()始终在ActionBarSherlock库中返回null

[英]Android: getSupportActionBar() always returns null in ActionBarSherlock library

I'm trying to use the ActionBarSherlock library to provide backwards compatible ActionBar support with tabs in my Android app, so I downloaded the latest build, built the demo, and ran it. 我正在尝试使用ActionBarSherlock库来为我的Android应用程序中的选项卡提供向后兼容的ActionBar支持,因此我下载了最新版本,构建了演示并运行它。

If you go to Action Bar, then select Tab Navigation it crashes every time. 如果你去Action Bar,然后选择Tab Navigation,它每次都会崩溃。 Here's the stack trace: 这是堆栈跟踪:

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  

I can't move forward with my app until this is fixed. 在修复此问题之前,我无法继续使用我的应用。 I wrote a bunch of code, set up the action bar in my app, and tried to run it, and it crashes with an NPE because of the null return value on the getSupportActionBar() call. 我写了一堆代码,在我的应用程序中设置了操作栏,并尝试运行它,并且由于getSupportActionBar()调用的null返回值而导致它与NPE崩溃。

The relevant code is actually in the demo for the library: 相关代码实际上是在库的演示中:

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) {
    }
}

You should add the Sherlock theme to your application 您应该将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. 我在Android ICS 4.0.4上遇到了同样的问题。 I was using requestWindowFeature(Window.FEATURE_NO_TITLE); 我正在使用requestWindowFeature(Window.FEATURE_NO_TITLE); on the FragmentActivity, but this was hiding the ActionBar on ICS+ devices that caused the getSupportActionBar() to be null. 在FragmentActivity上,但这隐藏了导致getSupportActionBar()为null的ICS +设备上的ActionBar。

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. 在Honeycomb +设备上发生这种情况的另一个原因是因为windowNoTitle属性是在您的样式中设置的。 Get rid of that as ActionBarSherlock will automatically remove it in pre-Honeycomb devices for you. 摆脱它,因为ActionBarSherlock会自动将它移除到预蜂窝设备中。

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. 您可能从getSupportActionBar()获取null的另一个原因是尝试在setContentView(R.layout.main)之前或在您添加片段的示例中调用它。

I refactored oncreate and mistakenly put getSupportActionBar().setNavigationMode(ActionBar.NAVIGATION_MODE_TABS); 我重构了oncreate并错误地把getSupportActionBar().setNavigationMode(ActionBar.NAVIGATION_MODE_TABS); after super.onCreate(savedInstanceState); super.onCreate(savedInstanceState);

这是一个有趣的:不要将主题设置为 -

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

您可能从getSupportActionBar()获取null的另一个原因是在Honeycomb +上的TabHost中使用该活动。

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 我只是改变了Manifest

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

to: 至:

  android:theme="@style/AppTheme"

and the error gone 并且错误消失了

whenever we set customview using sherlock library. 每当我们使用sherlock库设置customview时。 just remove this requestWindowFeature(Window.FEATURE_NO_TITLE); 只需删除此requestWindowFeature(Window.FEATURE_NO_TITLE); like this we make customview using sherlock bar library.. 像这样我们使用sherlock bar库制作customview ..

            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");

在( super.onCreate() )忘记调用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 您被声明为Theme.Sherlock或Theme.Sherlock.Light作为清单中的活动或应用程序主题或使用继承自这两者之一的自定义主题

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 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. 解决方法是查看库并删除任何样式名称“AppTheme”,如果您在清单中使用相同的主题名称。 There wasn't a conflict on my Galaxy S4, Jelly Bean, while there was a conflict on my Galaxy Tab. 我的Galaxy S4,Jelly Bean没有冲突,而我的Galaxy Tab上有冲突。

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. 我在Android Manifest文件中添加了android:theme="@android:style/Theme.Dialog" ,试图将其作为对话活动。 This too will remove the action bar hence a null pointer. 这也将删除操作栏因此空指针。 Remove it or don't call getSupportActioBar 删除它或不要调用getSupportActioBar

I make this mistake because i add the toobar in the fragment xml file. 我犯了这个错误,因为我在片段xml文件中添加了toobar。 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(). 在片段中找到工具栏的代码是:getActivity()。findViewByid(id ...),但是我的工具栏在片段xml文件中,因此没有找到工具栏,也没有工具栏being setSipprotActionBar()也没有得到任何东西当getSupportActionBar()时。 So remember : Do not put the toolbar in your fragment`s xml file. 所以请记住: 不要将工具栏放在片段的xml文件中。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM