根据this教程,我试图为我的应用构建导航抽屉。
我得到的是这一行的错误:
getActionBar().setDisplayHomeAsUpEnabled(true);
我的错误日志:
Caused by: java.lang.NullPointerException: Attempt to invoke virtual method 'void android.app.ActionBar.setDisplayHomeAsUpEnabled(boolean)' on a null object reference
12-11 18:36:50.670 6386-6386/com.example.mika.newspaper E/AndroidRuntime: at com.example.mika.newspaper.MainActivity.onCreate(MainActivity.java:56)
12-11 18:36:50.670 6386-6386/com.example.mika.newspaper E/AndroidRuntime: at android.app.Activity.performCreate(Activity.java:6237)
12-11 18:36:50.670 6386-6386/com.example.mika.newspaper E/AndroidRuntime: at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1107)
12-11 18:36:50.670 6386-6386/com.example.mika.newspaper E/AndroidRuntime: at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2369)
12-11 18:36:50.670 6386-6386/com.example.mika.newspaper E/AndroidRuntime: at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2476)
12-11 18:36:50.670 6386-6386/com.example.mika.newspaper E/AndroidRuntime: at android.app.ActivityThread.-wrap11(ActivityThread.java)
12-11 18:36:50.670 6386-6386/com.example.mika.newspaper E/AndroidRuntime: at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1344)
12-11 18:36:50.670 6386-6386/com.example.mika.newspaper E/AndroidRuntime: at android.os.Handler.dispatchMessage(Handler.java:102)
12-11 18:36:50.670 6386-6386/com.example.mika.newspaper E/AndroidRuntime: at android.os.Looper.loop(Looper.java:148)
12
我的活动:
package com.example.mika.newspaper;
import android.app.Activity;
import android.app.FragmentManager;
import android.app.Fragment;
import android.content.res.Configuration;
import android.support.v4.widget.DrawerLayout;
import android.os.Bundle;
import android.support.v7.app.ActionBarDrawerToggle;
import android.view.Menu;
import android.view.MenuItem;
import android.view.View;
import android.widget.AdapterView;
import android.widget.ArrayAdapter;
import android.widget.ListView;
public class MainActivity extends Activity {
private String[] mNewsTitles;
private DrawerLayout mDrawerLayout;
private ListView mDrawerList;
private ActionBarDrawerToggle mDrawerToggle;
private CharSequence mDrawerTitle;
private CharSequence mTitle;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
mNewsTitles = getResources().getStringArray(R.array.newsTypes);
mDrawerLayout = (DrawerLayout) findViewById(R.id.drawer_layout);
mDrawerList = (ListView) findViewById(R.id.left_drawer);
mDrawerList.setAdapter(new ArrayAdapter<String>(this, R.layout.drawer_list_item, mNewsTitles));
mDrawerList.setOnItemClickListener(new DrawerItemClickListener());
mTitle = mDrawerTitle = getTitle();
mDrawerToggle = new ActionBarDrawerToggle(this, mDrawerLayout,
R.string.drawer_open, R.string.drawer_close) {
public void onDrawerClosed(View view) {
super.onDrawerClosed(view);
getActionBar().setTitle(mTitle);
invalidateOptionsMenu();
}
public void onDrawerOpened(View drawerView) {
super.onDrawerOpened(drawerView);
getActionBar().setTitle(mDrawerTitle);
invalidateOptionsMenu();
}
};
mDrawerLayout.setDrawerListener(mDrawerToggle);
getActionBar().setDisplayHomeAsUpEnabled(true);
getActionBar().setHomeButtonEnabled(true);
}
@Override
protected void onPostCreate(Bundle savedInstanceState) {
super.onPostCreate(savedInstanceState);
mDrawerToggle.syncState();
}
@Override
public void onConfigurationChanged(Configuration newConfig) {
super.onConfigurationChanged(newConfig);
mDrawerToggle.onConfigurationChanged(newConfig);
}
@Override
public boolean onOptionsItemSelected(MenuItem item) {
if (mDrawerToggle.onOptionsItemSelected(item)) {
return true;
}
return super.onOptionsItemSelected(item);
}
@Override
public boolean onPrepareOptionsMenu(Menu menu) {
boolean drawerOpen = mDrawerLayout.isDrawerOpen(mDrawerList);
menu.findItem(R.id.action_websearch).setVisible(!drawerOpen);
return super.onPrepareOptionsMenu(menu);
}
private class DrawerItemClickListener implements ListView.OnItemClickListener {
@Override
public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
selectItem(position);
}
}
private void selectItem(int position){
Fragment fragment = new NewsFragment();
Bundle args = new Bundle();
args.putInt(NewsFragment.ARG_NEWS_NUMBER, position);
fragment.setArguments(args);
FragmentManager fragmentManager = getFragmentManager();
fragmentManager.beginTransaction().replace(R.id.content_frame, fragment).commit();
mDrawerList.setItemChecked(position, true);
setTitle(mNewsTitles[position]);
mDrawerLayout.closeDrawer(mDrawerList);
}
@Override
public void setTitle(CharSequence title) {
mTitle = title;
getActionBar().setTitle(mTitle);
}
}
答案 0 :(得分:0)
您必须将所有getActionBar()
替换为getSupportActionBar()