Android中的操作栏setDisplayShowHomeEnabled()中的nullpointerexception

时间:2015-07-29 10:56:19

标签: java android nullpointerexception

我创建自定义操作栏我真的不知道我的错误在哪里。我在创建自定义操作栏时遇到了一些问题。

并在行显示空指针异常= mActionBar.setDisplayShowHomeEnabled(false);

这是我的错误日志

07-29 16:19:20.180  32714-32714/com.example.tazeen.classnkk E/AndroidRuntime﹕ FATAL EXCEPTION: main
    java.lang.RuntimeException: Unable to start activity ComponentInfo{com.example.tazeen.classnkk/com.example.tazeen.classnkk.AllPosts_Page}: java.lang.NullPointerException
            at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:1956)
            at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:1981)
            at android.app.ActivityThread.access$600(ActivityThread.java:123)
            at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1147)
            at android.os.Handler.dispatchMessage(Handler.java:99)
            at android.os.Looper.loop(Looper.java:137)
            at android.app.ActivityThread.main(ActivityThread.java:4424)
            at java.lang.reflect.Method.invokeNative(Native Method)
            at java.lang.reflect.Method.invoke(Method.java:511)
            at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:784)
            at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:551)
            at dalvik.system.NativeStart.main(Native Method)
     Caused by: java.lang.NullPointerException
            at com.example.tazeen.classnkk.AllPosts_Page.CustomActionBar(AllPosts_Page.java:37)
            at com.example.tazeen.classnkk.AllPosts_Page.onCreate(AllPosts_Page.java:28)
            at android.app.Activity.performCreate(Activity.java:4466)
            at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1049)
            at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:1920)
            at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:1981)
            at android.app.ActivityThread.access$600(ActivityThread.java:123)
            at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1147)
            at android.os.Handler.dispatchMessage(Handler.java:99)
            at android.os.Looper.loop(Looper.java:137)
            at android.app.ActivityThread.main(ActivityThread.java:4424)
            at java.lang.reflect.Method.invokeNative(Native Method)
            at java.lang.reflect.Method.invoke(Method.java:511)
            at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:784)
            at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:551)
            at dalvik.system.NativeStart.main(Native Method)

我已经尝试了5到7个小时,但找不到任何合适的解决方案。请帮帮我。谢谢。

这是我的活动代码

public class AllPosts_Page extends Activity
{

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.all_post);
        CustomActionBar();
    }
public void CustomActionBar()
    {
        android.app.ActionBar mActionBar = getActionBar();
        mActionBar.setDisplayShowHomeEnabled(false);
        mActionBar.setDisplayShowTitleEnabled(false);

        LayoutInflater mInflater = LayoutInflater.from(this);

        View mCustomView = mInflater.inflate(R.layout.custom_action_bar, null);
        TextView mTitleTextView = (TextView) mCustomView.findViewById(R.id.txtTitle);
        mTitleTextView.setText("All Post");


        ImageView imageButton = (ImageView) mCustomView
                .findViewById(R.id.imgLeftMenu);
        imageButton.setOnClickListener(new View.OnClickListener() {

            @Override
            public void onClick(View view) {
                Toast.makeText(getApplicationContext(), "Refresh Clicked!",
                        Toast.LENGTH_LONG).show();
            }
        });


        mActionBar.setCustomView(mCustomView);
        mActionBar.setDisplayShowCustomEnabled(true);
    }

清单文件

 <application
        android:allowBackup="true"
        android:icon="@mipmap/ic_launcher"
        android:label="@string/app_name"
        android:theme="@android:style/Theme.NoTitleBar">
        <activity
            android:name=".Splash_Screen"
            android:label="@string/app_name" >
            <intent-filter>
                <action android:name="android.intent.action.MAIN" />

                <category android:name="android.intent.category.LAUNCHER" />
            </intent-filter>
        </activity>
        <activity android:name=".Login_Screen" />
        <activity
            android:name=".AllPosts_Page">
        </activity>
        <activity
            android:name=".Filter_Page"
            android:label="@string/title_activity_filter__page"
            android:theme="@style/ListFont">
        </activity>
    </application>

</manifest>

style.xml

<resources>

    <!-- Base application theme. -->
    <style name="AppTheme" parent="Theme.AppCompat.Light.DarkActionBar">
        <!-- Customize your theme here. -->
        <item name="android:height">5dp</item>
</style>
</resources>

1 个答案:

答案 0 :(得分:2)

您正在使用Theme.AppCompat.Light.DarkActionBar,因此您应该使用ActionBar getSupportActionBar()方法,而不是getActionBar()

如果您的扩展Activity来自Android系统,那么样式不正确(AppCompat),而Activity会在没有ActionBar的情况下获得一些默认样式,因此getActionBar()会返回null

否则,如果您想使用此AppCompat样式,则适用于AppCompatActivity(然后您应该使用getSupportActionBar()