uglyant
uglyant
采纳率100%
2017-04-04 12:16 阅读 1.2k
已采纳

ActionBar时候出现.ActionBar.show()' on a null object

接下来请看代码:

 package com.example.laoban.newtext;

import android.app.ActionBar;
import android.app.Activity;
import android.os.Bundle;
import android.util.Log;
import android.view.View;

public class Main2Activity extends Activity {
    ActionBar actionBar;
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main2);
        actionBar = getActionBar();

    }
    public void show(View source)
    {
        actionBar.show();

    }
    public void hide(View source)
    {
        actionBar.hide();
    }
}

下面是报错信息

04-04 08:06:32.435 10789-10789/com.example.laoban.newtext D/AndroidRuntime: Shutting down VM


                                                                            --------- beginning of crash
04-04 08:06:32.437 10789-10789/com.example.laoban.newtext E/AndroidRuntime: FATAL EXCEPTION: main
                                                                            Process: com.example.laoban.newtext, PID: 10789
                                                                            java.lang.IllegalStateException: Could not execute method for android:onClick
                                                                                at android.view.View$DeclaredOnClickListener.onClick(View.java:4697)
                                                                                at android.view.View.performClick(View.java:5609)
                                                                                at android.view.View$PerformClick.run(View.java:22259)
                                                                                at android.os.Handler.handleCallback(Handler.java:751)
                                                                                at android.os.Handler.dispatchMessage(Handler.java:95)
                                                                                at android.os.Looper.loop(Looper.java:154)
                                                                                at android.app.ActivityThread.main(ActivityThread.java:6077)
                                                                                at java.lang.reflect.Method.invoke(Native Method)
                                                                                at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:865)
                                                                                at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:755)
                                                                             Caused by: java.lang.reflect.InvocationTargetException
                                                                                at java.lang.reflect.Method.invoke(Native Method)
                                                                                at android.view.View$DeclaredOnClickListener.onClick(View.java:4692)
                                                                                at android.view.View.performClick(View.java:5609) 
                                                                                at android.view.View$PerformClick.run(View.java:22259) 
                                                                                at android.os.Handler.handleCallback(Handler.java:751) 
                                                                                at android.os.Handler.dispatchMessage(Handler.java:95) 
                                                                                at android.os.Looper.loop(Looper.java:154) 
                                                                                at android.app.ActivityThread.main(ActivityThread.java:6077) 
                                                                                at java.lang.reflect.Method.invoke(Native Method) 
                                                                                at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:865) 
                                                                                at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:755) 
                                                                             Caused by: java.lang.NullPointerException: Attempt to invoke virtual method 'void android.app.ActionBar.show()' on a null object reference
                                                                                at com.example.laoban.newtext.Main2Activity.show(Main2Activity.java:20)
                                                                                at java.lang.reflect.Method.invoke(Native Method) 
                                                                                at android.view.View$DeclaredOnClickListener.onClick(View.java:4692) 
                                                                                at android.view.View.performClick(View.java:5609) 
                                                                                at android.view.View$PerformClick.run(View.java:22259) 
                                                                                at android.os.Handler.handleCallback(Handler.java:751) 
                                                                                at android.os.Handler.dispatchMessage(Handler.java:95) 
                                                                                at android.os.Looper.loop(Looper.java:154) 
                                                                                at android.app.ActivityThread.main(ActivityThread.java:6077) 
                                                                                at java.lang.reflect.Method.invoke(Native Method) 
                                                                                at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:865) 
                                                                                at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:755) 


                                                                            --------- beginning of system

下面是AndroidMainifest.xml文件

 <?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
    package="com.example.laoban.newtext">

    <application
        android:allowBackup="true"
        android:icon="@mipmap/ic_launcher"
        android:label="@string/app_name"
        android:supportsRtl="true"
        android:theme="@style/AppTheme">
        <activity android:name=".MainActivity">

        </activity>
        <activity android:name=".Main2Activity">
            <intent-filter>
                <action android:name="android.intent.action.MAIN" />

                <category android:name="android.intent.category.LAUNCHER" />
            </intent-filter>
        </activity>
    </application>

</manifest>

其中我自己建立了一个Main2Activity.java文件
用来测试ActionBar但是每当点击隐藏或者显示ActionBar时候模拟器stopped,出现报错,最后我把AndroidMainfest.xml文件中的主题android:theme="@style/AppTheme"修改之后就可以了;
请看下面的主题文件

 <resources>

    <!-- Base application theme. -->
    <style name="AppTheme" parent="Theme.AppCompat.Light.DarkActionBar">
        <!-- Customize your theme here. -->
        <item name="colorPrimary">@color/colorPrimary</item>
        <item name="colorPrimaryDark">@color/colorPrimaryDark</item>
        <item name="colorAccent">@color/colorAccent</item>
    </style>

</resources>

其中的parent="Theme.AppCompat.Light.DarkActionBar"去掉之后ActionBar才能够正常使用, 请问大家这是什么原因请给出答案谢谢

  • 点赞
  • 写回答
  • 关注问题
  • 收藏
  • 复制链接分享

1条回答 默认 最新

  • 已采纳
    ljheee ljheee 2017-04-04 14:26

    这个问题很多人都遇到的,解决办法有一个
    public class Main2Activity extends ActionBarActivity

    点赞 评论 复制链接分享

相关推荐