Android:Actionbar nullpointexception setDisplayShowHomeEnabled

时间:2015-10-10 17:07:25

标签: android android-actionbar

我在“getSupportActionBar()。setDisplayShowHomeEnabled(true);”中遇到此错误第87行。

我已经检查了getSupportActionBar扩展名,Theme.AppCompat,并检查了getSupporActionBar的空点。这段代码有什么问题...... ??

错误:

java.lang.RuntimeException: Unable to start activity ComponentInfo{com.trik.gamepad/com.trik.gamepad.MainActivity}: java.lang.NullPointerException: Attempt to invoke virtual method 'void android.app.ActionBar.setDisplayShowHomeEnabled(boolean)' on a null object reference
        at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2325)
        at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2387)
        at android.app.ActivityThread.access$800(ActivityThread.java:151)
        at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1303)
        at android.os.Handler.dispatchMessage(Handler.java:102)
        at android.os.Looper.loop(Looper.java:135)
        at android.app.ActivityThread.main(ActivityThread.java:5254)
        at java.lang.reflect.Method.invoke(Native Method)
        at java.lang.reflect.Method.invoke(Method.java:372)
        at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:903)
        at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:698)
 Caused by: java.lang.NullPointerException: Attempt to invoke virtual method 'void android.app.ActionBar.setDisplayShowHomeEnabled(boolean)' on a null object reference
        at android.support.v7.app.ActionBarImplICS.setDisplayShowHomeEnabled(ActionBarImplICS.java:174)
        at android.support.v7.app.ActionBarImplJB.setDisplayShowHomeEnabled(ActionBarImplJB.java:20)
        at com.trik.gamepad.MainActivity.onCreate(MainActivity.java:86)
        at android.app.Activity.performCreate(Activity.java:5990)
        at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1106)
        at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2278)
        at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2387)
        at android.app.ActivityThread.access$800(ActivityThread.java:151)
        at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1303)
        at android.os.Handler.dispatchMessage(Handler.java:102)
        at android.os.Looper.loop(Looper.java:135)
        at android.app.ActivityThread.main(ActivityThread.java:5254)
        at java.lang.reflect.Method.invoke(Native Method)
        at java.lang.reflect.Method.invoke(Method.java:372)
        at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:903)
        at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:698)

这是我的主要活动

public class MainActivity extends ActionBarActivity implements SensorEventListener {
@Override
protected void onCreate(final Bundle savedInstanceState) {
    setRequestedOrientation(ActivityInfo.SCREEN_ORIENTATION_LANDSCAPE);
    requestWindowFeature(Window.FEATURE_NO_TITLE);
    getWindow().setFlags(WindowManager.LayoutParams.FLAG_FULLSCREEN, WindowManager.LayoutParams.FLAG_FULLSCREEN);
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_main);

    setSystemUiVisibility(false);

    if(getSupportActionBar()!=null){
        getSupportActionBar().setDisplayShowHomeEnabled(true);
        getSupportActionBar().setDisplayUseLogoEnabled(true);
        getSupportActionBar().setLogo(R.drawable.trik_icon);

        getSupportActionBar().setDisplayShowTitleEnabled(true);
        getSupportActionBar().setTitle("Preferences");
    }
}

这是styles.xml

<resources>

<!--
    Base application theme, dependent on API level. This theme is replaced
    by AppBaseTheme from res/values-vXX/styles.xml on newer devices.
-->
<style name="AppBaseTheme" parent="Theme.AppCompat">
    <!--
        Theme customizations available in newer API levels can go in
        res/values-vXX/styles.xml, while customizations related to
        backward-compatibility can go here.
    -->
</style>

<!-- Application theme. -->
<style name="AppTheme" parent="AppBaseTheme">
    <!-- Support library compatibility -->
    <item name="windowActionBarOverlay">true</item>
    <!-- All customizations that are NOT specific to a particular API-level can go here. -->
</style>

这是AndroidManifest.xml

<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
package="com.trik.gamepad"
android:versionCode="16"
android:versionName="1.9 dev" >
<uses-permission android:name="android.permission.INTERNET" />    
<!--  <uses-permission android:name="android.permission.VIBRATE" /> -->
<uses-sdk
    android:minSdkVersion="7"
    android:targetSdkVersion="21" />

<application
    android:allowBackup="false"
    android:icon="@drawable/trik_icon"
    android:label="TRIK Gamepad"
    android:theme="@style/AppTheme" >
   <!--  <service android:name=".SenderService" />  -->

    <activity
        android:name="com.trik.gamepad.MainActivity"
        android:label="TRIK Gamepad">
        <intent-filter>
            <action android:name="android.intent.action.MAIN" />

            <category android:name="android.intent.category.LAUNCHER" />
        </intent-filter>
    </activity>
    <activity
        android:name="com.trik.gamepad.SettingsActivity"
        android:label="@string/title_activity_settings" >
    </activity>        
</application>

2 个答案:

答案 0 :(得分:1)

activity_main.xml

中添加此内容
protected override void

如果您已添加此内容,请转到下一步:

MainActivity.java

中添加此内容
base.Start();

并在 onCreate()

<android.support.v7.widget.Toolbar 
android:id="@+id/tool_bar"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:theme="@style/ThemeOverlay.AppCompat.Dark">

所以现在你可以Toolbar toolbar; 和你想要的。

答案 1 :(得分:1)

您忘了设置操作栏。像这样初始化你的ActionBar /工具栏:

Toolbar toolbar = (Toolbar) findViewById(R.id.your_toolbar);
setSupportActionBar(toolbar);
if (getSupportActionBar() != null) 
    getSupportActionBar().setDisplayHomeAsUpEnabled(true);
}