![](/img/trans.png)
[英]getSupportActionBar().setDisplayHomeAsUpEnabled(true); throws NullPointerException
[英]getsupportActionBar().setDisplayHomeAsUpEnabled(true).setDisplayHomeAsUpEnabled(true) throws NullPointerException
我的应用每次都崩溃,并出现java.lang.RuntimeException: Unable to start activity ComponentInfo{sidssol.com.bit_taxi_b2c/sidssol.com.bit_taxi_b2c.Home}: java.lang.NullPointerException
错误java.lang.RuntimeException: Unable to start activity ComponentInfo{sidssol.com.bit_taxi_b2c/sidssol.com.bit_taxi_b2c.Home}: java.lang.NullPointerException
getActionBar()。setDisplayHomeAsUpEnabled(true)的java.lang.RuntimeException: Unable to start activity ComponentInfo{sidssol.com.bit_taxi_b2c/sidssol.com.bit_taxi_b2c.Home}: java.lang.NullPointerException
,粘贴了所有必需的代码。任何人都可以检查此代码
public class Home extends ActionBarActivity {
adapter = new NavDrawerListAdapter(getApplicationContext(),
navDrawerItems);
mDrawerList.setAdapter(adapter);
// enabling action bar app icon and behaving it as toggle button
getSupportActionBar().setDisplayHomeAsUpEnabled(true);
getSupportActionBar().setHomeButtonEnabled(true);
mDrawerToggle = new ActionBarDrawerToggle(this, mDrawerLayout,
R.drawable.ic_drawer, //nav menu toggle icon
R.string.app_name, // nav drawer open - description for accessibility
R.string.app_name // nav drawer close - description for accessibility
)
}
Style.xml
<resources>
<!-- Base application theme. -->
<style name="AppTheme" parent="Theme.AppCompat.Light.NoActionBar" >
<item name="android:windowTranslucentStatus">true</item>
</style>
<style name="Theme.Test" parent="@style/Theme.AppCompat.Light">
<!-- customize the color palette -->
<item name="colorPrimary">@color/colorPrimary</item>
<item name="colorPrimaryDark">@color/colorPrimaryDark</item>
<item name="colorAccent">@color/colorAccent</item>
<item name="windowActionBar">false</item>
<item name="windowNoTitle">true</item>
<item name="drawerArrowStyle">@style/Theme.Test.DrawerArrowStyle</item>
</style>
<style name="Theme.Test.DrawerArrowStyle" parent="@style/Widget.AppCompat.DrawerArrowToggle">
<item name="spinBars">true</item>
<item name="color">@android:color/white</item>
</style>
<style name="Divider">
<item name="android:layout_width">match_parent</item>
<item name="android:layout_height">1dp</item>
<item name="android:background">?android:attr/listDivider</item>
</style>
</resources>
Androidmainfest.xml
<application
android:allowBackup="true"
android:icon="@mipmap/ic_launcher"
android:label="@string/app_name"
android:theme="@style/AppTheme" >
<activity
android:name=".loginForm"
android:label="@string/app_name" >
<intent-filter>
<action android:name="android.intent.action.MAIN" />
<category android:name="android.intent.category.LAUNCHER" />
</intent-filter>
</activity>
如您所见,主题状态没有动作栏
Theme.AppCompat.Light.NoActionBar
如果您正在使用该主题,则需要使用Toolbar
或将主题更改为使用操作栏
更改这些行
<!-- Base application theme. -->
<style name="AppTheme" parent="Theme.AppCompat.Light.NoActionBar" >
<item name="android:windowTranslucentStatus">true</item>
</style>
跟着
<!-- Base application theme. -->
<style name="AppTheme" parent="Theme.AppCompat.Light" >
<item name="android:windowTranslucentStatus">true</item>
</style>
或将android:theme="@style/AppTheme"
更改为android:theme="@style/Theme.Test"
另外,不建议使用ActionBarActivity,应使用AppCompatActivity。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.