我的android应用崩溃了。每当我尝试运行该应用程序时,都会弹出一条消息,提示“不幸的是,碎饼和比萨饼”已停止。
下面是我的Logcat:
07-27 19:12:34.672 11698-11698/com.hfad.bitsandpizzas E/AndroidRuntime: FATAL EXCEPTION: main
Process: com.hfad.bitsandpizzas, PID: 11698
java.lang.RuntimeException: Unable to start activity ComponentInfo{com.hfad.bitsandpizzas/com.hfad.bitsandpizzas.MainActivity}: java.lang.NullPointerException: Attempt to invoke virtual method 'void android.support.v7.app.ActionBarDrawerToggle.syncState()' on a null object reference
at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2444)
at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2504)
at android.app.ActivityThread.access$900(ActivityThread.java:165)
at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1368)
at android.os.Handler.dispatchMessage(Handler.java:102)
at android.os.Looper.loop(Looper.java:150)
at android.app.ActivityThread.main(ActivityThread.java:5546)
at java.lang.reflect.Method.invoke(Native Method)
at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:794)
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:684)
Caused by: java.lang.NullPointerException: Attempt to invoke virtual method 'void android.support.v7.app.ActionBarDrawerToggle.syncState()' on a null object reference
at com.hfad.bitsandpizzas.MainActivity.onPostCreate(MainActivity.java:166)
at android.app.Instrumentation.callActivityOnPostCreate(Instrumentation.java:1191)
at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2426)
at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2504)
at android.app.ActivityThread.access$900(ActivityThread.java:165)
at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1368)
at android.os.Handler.dispatchMessage(Handler.java:102)
at android.os.Looper.loop(Looper.java:150)
at android.app.ActivityThread.main(ActivityThread.java:5546)
at java.lang.reflect.Method.invoke(Native Method)
at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:794)
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:684)
下面是我的应用程序Java代码:
package com.hfad.bitsandpizzas;
import android.content.res.Configuration;
import android.support.v4.view.MenuItemCompat;
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.Menu;
import android.view.MenuItem;
import android.content.Intent;
import android.support.v7.widget.ShareActionProvider;
import android.widget.ListView;
import android.widget.ArrayAdapter;
import android.view.View;
import android.widget.AdapterView;
import android.app.Fragment;
import android.app.FragmentTransaction;
import android.widget.Toolbar;
public class MainActivity extends AppCompatActivity {
private ShareActionProvider shareActionProvider;
private String[] titles;
private ListView drawerList;
private DrawerLayout drawerLayout;
private ActionBarDrawerToggle drawerToggle;
private class DrawerItemClickListener implements ListView.OnItemClickListener{
@Override
public void onItemClick(AdapterView<?> parent, View view, int position, long id){
selectItem(position); // Call the selectItem() method wen an item gets clicked.
}
}
private void selectItem(int position){ // Check the position of the item that was clicked.
Fragment fragment;
switch (position){
case 1:
fragment = new PizzaFragment(); // Use the position to create the right type of fragment. The "Pizzas" option is at position 1, for instance, so in this case create a PizzaFragment.
break;
case 2:
fragment = new PastaFragment();
break;
case 3:
fragment = new StoresFragment();
break;
default:
fragment = new TopFragment();
}
FragmentTransaction ft = getFragmentManager().beginTransaction();
ft.replace(R.id.content_frame, fragment);
ft.addToBackStack(null);
ft.setTransition(FragmentTransaction.TRANSIT_FRAGMENT_FADE);
ft.commit();
//Set Action Bar Title
setActionBarTitle(position);
//Close the drawer
drawerLayout.closeDrawer(drawerList); // drawerList is the DrawerLayout's drawer. This tells the DrawerLayout to close its ListView drawer.
}
private void setActionBarTitle(int position){
String title;
if(position==0){
title = getResources().getString(R.string.app_name); // If the user clicks on the "Home" option, use the app name for the title.
}
else{
title = titles[position]; // Otherwise, get the String from the titles array for the position that was clicked and use that.
}
getSupportActionBar().setTitle(title); // Display the title on the action bar.
}
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
titles = getResources().getStringArray(R.array.titles);
drawerList = (ListView)findViewById(R.id.drawer);
drawerLayout = (DrawerLayout) findViewById(R.id.drawer_layout); // Get a reference to the DrawerLayout
//Populate the List View
drawerList.setAdapter(new ArrayAdapter<String>(this, // Use an ArrayAdapter to populate the ListView
android.R.layout.simple_list_item_activated_1, titles)); // Using simple_list_item_activated_1 means that the item the user clicks on is highlighted.
drawerList.setOnItemClickListener(new DrawerItemClickListener()); // Add a new instance of our OnItemClickListener to the drawer's ListView
if(savedInstanceState == null){
selectItem(0); // If the MainActivity is newly created, use the selectItem() method to display TopFragment
}
//Create the ActionBarDrawerToggle
ActionBarDrawerToggle drawerToggle = new ActionBarDrawerToggle(this, drawerLayout,
R.string.open_drawer, R.string.close_drawer){
//Called when a drawer has settled in a completely closed state
@Override
public void onDrawerClosed(View view){ // This method gets called when the drawer is closed.
super.onDrawerClosed(view);
invalidateOptionsMenu(); // The invalidateOptionsMenu() method tells Android to re-create the menu items. We want to change the visibility of the Share action if the drawer is opened or closed, so we call it in the onDrawerOpened() and onDrawerClosed() methods.
}
//Called when a drawer has settled in a completely open state
@Override
public void onDrawerOpened(View drawerView){
super.onDrawerOpened(drawerView);
invalidateOptionsMenu();
}
};
drawerLayout.addDrawerListener(drawerToggle); // Set the DrawerLayout's drawer listener as the ActionBarDrawerToggle.
if(getSupportActionBar()!=null) {
getSupportActionBar().setDisplayHomeAsUpEnabled(true); // Enables the Up icon so it can be used by the ActionBarDrawer Toggle.
getSupportActionBar().setHomeButtonEnabled(true);
}
}
//Called whenever we call invalidateOptionsMenu()
@Override
public boolean onPrepareOptionsMenu(Menu menu){
//If the drawer is open, hide action items related to the content view
boolean drawerOpen = drawerLayout.isDrawerOpen(drawerList);
menu.findItem(R.id.action_share).setVisible(!drawerOpen);
return super.onPrepareOptionsMenu(menu);
}
@Override
public boolean onCreateOptionsMenu(Menu menu){
//Inflate the menu; This adds items to the action bar present in the menu resource file.
getMenuInflater().inflate(R.menu.menu_main, menu); // This takes menu items in the menu_main.xml menu resource file, and add them to the action bar Menu object.
MenuItem menuItem = menu.findItem(R.id.action_share);
shareActionProvider = (ShareActionProvider) MenuItemCompat.getActionProvider(menuItem); // Get a reference to the share action provider and assign it to the private variable. Then call the setIntent() method.
setIntent("This is an example text");
return super.onCreateOptionsMenu(menu);
}
private void setIntent(String text){
Intent intent = new Intent(Intent.ACTION_SEND); // We created the setIntent() method.It creates an intent, and passes it to the share action provider using setShareIntent() method.
intent.setType("text/plain");
intent.putExtra(Intent.EXTRA_TEXT,text);
shareActionProvider.setShareIntent(intent);
}
@Override
public boolean onOptionsItemSelected(MenuItem item){ // The MenuItem object is the item on the action bar that was clicked.
if (drawerToggle.onOptionsItemSelected(item)){ // This line is used so that the ActionBarDrawer Toggle can handle being clicked. It returns true if the ActionBarDrawer Toggle has handled being clicked. If it returns false, this means that another action item in the action bar has been clicked.
return true;
}
switch (item.getItemId()){
case R.id.action_create_order:
Intent intent = new Intent(this, OrderActivity.class);
startActivity(intent);
return true;
case R.id.action_settings:
return true;
default:
return super.onOptionsItemSelected(item);
}
}
@Override
protected void onPostCreate(Bundle savedInstanceState){
super.onPostCreate(savedInstanceState);
//Sync the toggle state after onRestoreInstanceState has occurred.
drawerToggle.syncState(); // Syncing the state means that the drawer icon appears one way when the drawer is closed, and another way when the drawer is open.
}
@Override
public void onConfigurationChanged(Configuration newConfig){ // You need to add this method to your activity so that any configuration changes get passed to the ActionBarDrawer Toggle.
super.onConfigurationChanged(newConfig);
drawerToggle.onConfigurationChanged(newConfig);
}
}
我已经尝试使用getSupportActionBar()代替getActionBar(),因为我已经阅读了其他答案,仍然没有帮助。