java代码如下:布局似乎很好。当我点击左上角的导航栏图标时,似乎会出现唯一的问题。
package com.example.chirag.carparlour;
import android.app.ActionBar;
import android.content.Intent;
import android.graphics.Color;
import android.graphics.drawable.ColorDrawable;
import android.support.v4.widget.DrawerLayout;
import android.support.v7.app.ActionBarDrawerToggle;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.view.MenuItem;
import android.view.View;
import android.widget.ArrayAdapter;
import android.widget.Button;
import android.view.Menu;
public class MainActivity extends AppCompatActivity {
private DrawerLayout dl;
private ActionBarDrawerToggle abdt;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
Button book = (Button)findViewById(R.id.book);
Button sub = (Button)findViewById(R.id.sub);
Button offers = (Button)findViewById(R.id.offers);
Button support = (Button)findViewById(R.id.support);
Button comp = (Button)findViewById(R.id.comp);
DrawerLayout dl = (DrawerLayout)findViewById(R.id.drawerlayout);
ActionBarDrawerToggle abdt = new ActionBarDrawerToggle(this, dl , R.string.Open, R.string.Close);
dl.addDrawerListener(abdt);
abdt.syncState();
getSupportActionBar().setDisplayHomeAsUpEnabled(true);
book.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view) {
Intent intent = new Intent(MainActivity.this, bookawash.class);
startActivity(intent);
}
});
sub.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view) {
Intent intent = new Intent(MainActivity.this, subscriptions.class);
startActivity(intent);
}
});
offers.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view) {
}
});
support.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view) {
}
});
comp.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view) {
}
});
}
@Override
public boolean onOptionsItemSelected(MenuItem item) {
if(abdt.onOptionsItemSelected(item))
{return true;}
return super.onOptionsItemSelected(item);
}}
并且日志中的错误如下:
FATAL EXCEPTION: main
Process: com.example.chirag.carparlour, PID: 4879
java.lang.NullPointerException: Attempt to invoke virtual method 'boolean android.support.v7.app.ActionBarDrawerToggle.onOptionsItemSelected(android.view.MenuItem)' on a null object reference
at com.example.chirag.carparlour.MainActivity.onOptionsItemSelected(MainActivity.java:86)
at android.app.Activity.onMenuItemSelected(Activity.java:2914)
at android.support.v4.app.FragmentActivity.onMenuItemSelected(FragmentActivity.java:408)
at android.support.v7.app.AppCompatActivity.onMenuItemSelected(AppCompatActivity.java:198)
at android.support.v7.view.WindowCallbackWrapper.onMenuItemSelected(WindowCallbackWrapper.java:113)
at android.support.v7.widget.ToolbarWidgetWrapper$1.onClick(ToolbarWidgetWrapper.java:187)
at android.view.View.performClick(View.java:5205)
at android.view.View$PerformClick.run(View.java:21166)
at android.os.Handler.handleCallback(Handler.java:739)
at android.os.Handler.dispatchMessage(Handler.java:95)
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 :(得分:2)
我认为问题是:(在onCreate中):
ActionBarDrawerToggle abdt = new ActionBarDrawerToggle(this, dl , R.string.Open, R.string.Close);
切换到:
abdt = new ActionBarDrawerToggle(this, dl , R.string.Open, R.string.Close);