以下是java代码:
public class Displayprofile extends AppCompatActivity {
private DrawerLayout mDrawerLayout;
private ActionBarDrawerToggle mToggle;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.displayprofile);
mDrawerLayout = (DrawerLayout)findViewById(R.id.drawerlayout) ;
mToggle = new ActionBarDrawerToggle(this, mDrawerLayout, R.string.open, R.string.close);
getSupportActionBar().setDisplayHomeAsUpEnabled(true);
mDrawerLayout.addDrawerListener(mToggle);
mToggle.syncState();
}
@Override
public boolean onOptionsItemSelected(MenuItem item) {
if (mToggle.onOptionsItemSelected(item))
{
return true;
}
return super.onOptionsItemSelected(item);
}
}
导航图标未显示。否则,导航菜单工作正常。只要添加getSupportActionBar().setDisplayHomeAsUpEnabled(true);
,应用就会崩溃。只要省略此语句,应用程序就不会崩溃。
这项活动的主题如下:
<style name="mainthemered" parent="Theme.AppCompat.Light.DarkActionBar">
<item name="colorAccent">@color/colorLogoRed</item>
</style>
错误如下:
12-06 21:23:11.256 22417-22417/com.example.krsnv.kooc E/AndroidRuntime: FATAL EXCEPTION: main
Process: com.example.krsnv.kooc, PID: 22417
java.lang.RuntimeException: Unable to start activity ComponentInfo{com.example.krsnv.kooc/com.example.krsnv.kooc.Displayprofile}: java.lang.NullPointerException: Attempt to invoke virtual method 'void android.support.v7.app.ActionBar.setDisplayHomeAsUpEnabled(boolean)' on a null object reference
at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2416)
at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2476)
at android.app.ActivityThread.-wrap11(ActivityThread.java)
at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1344)
at android.os.Handler.dispatchMessage(Handler.java:102)
at android.os.Looper.loop(Looper.java:148)
at android.app.ActivityThread.main(ActivityThread.java:5422)
at java.lang.reflect.Method.invoke(Native Method)
at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:726)
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:616)
Caused by: java.lang.NullPointerException: Attempt to invoke virtual method 'void android.support.v7.app.ActionBar.setDisplayHomeAsUpEnabled(boolean)' on a null object reference
at com.example.krsnv.kooc.Displayprofile.onCreate(Displayprofile.java:47)
at android.app.Activity.performCreate(Activity.java:6251)
at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1108)
at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2369)
at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2476)
at android.app.ActivityThread.-wrap11(ActivityThread.java)
at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1344)
at android.os.Handler.dispatchMessage(Handler.java:102)
at android.os.Looper.loop(Looper.java:148)
at android.app.ActivityThread.main(ActivityThread.java:5422)
at java.lang.reflect.Method.invoke(Native Method)
at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:726)
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:616)
答案 0 :(得分:0)
设置支持操作栏?
Toolbar myToolbar = (Toolbar) findViewById(R.id.your_toolbar);
setSupportActionBar(myToolbar);
getSupportActionBar().setTitle("Your title");
getSupportActionBar().setDisplayHomeAsUpEnabled(true);