在我的 android 项目中,我有一个带有 2 个选项卡的操作栏的 Activity,我还添加了一个抽屉菜单,但是,当我尝试将应用程序图标设置为一个切换按钮以打开抽屉时,我得到一个 Nullpointexception。这是我的代码:
public class PaginaPrincipalActivity extends Activity {
private DrawerLayout mDrawerLayout;
private ListView mDrawerList;
private ActionBarDrawerToggle mDrawerToggle;
// nav drawer title
private CharSequence mDrawerTitle;
// used to store app title
private CharSequence mTitle;
// slide menu items
private String[] navMenuTitles;
private TypedArray navMenuIcons;
private ArrayList<TelaPrincipalDrawerItens> navDrawerItems;
private DrawerPrincipalAdapter adapter;
ActionBar.Tab tab1, tab2;
Fragment fragmentTab1 = new TabFragmentPrincipal();
Fragment fragmentTab2 = new TabEmTransporte();
@TargetApi(Build.VERSION_CODES.ICE_CREAM_SANDWICH)
@SuppressWarnings("unchecked")
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.tab_principal_activity);
navDrawerItems = (ArrayList<TelaPrincipalDrawerItens>) createDrawerItens();
//setando drawerLayout
mDrawerLayout = (DrawerLayout) findViewById(R.layout.telaprincipal_drawer);
mDrawerList = (ListView) findViewById(R.id.left_drawer);
adapter = new DrawerPrincipalAdapter(this, R.layout.telaprincipal_item_drawer, navDrawerItems);
mDrawerList.setAdapter(adapter);
// Handle your other action bar items...
getActionBar().setNavigationMode(ActionBar.NAVIGATION_MODE_TABS);
tab1 = getActionBar().newTab().setText("Principal");
tab2 = getActionBar().newTab().setText("Em Transporte");
tab1.setTabListener(new MyTabListener(fragmentTab1));
tab2.setTabListener(new MyTabListener(fragmentTab2));
getActionBar().addTab(tab1);
getActionBar().addTab(tab2);
//Try to add drawertoggle
mDrawerToggle = new ActionBarDrawerToggle(
this, /* host Activity */
mDrawerLayout, /* DrawerLayout object */
R.drawable.ic_drawer, /* nav drawer icon to replace 'Up' caret */
R.string.drawer_open, /* "open drawer" description */
R.string.drawer_close /* "close drawer" description */
){
/** Called when a drawer has settled in a completely closed state. */
public void onDrawerClosed(View view) {
super.onDrawerClosed(view);
getActionBar().setTitle(mTitle);
}
/** Called when a drawer has settled in a completely open state. */
public void onDrawerOpened(View drawerView) {
super.onDrawerOpened(drawerView);
getActionBar().setTitle(mDrawerTitle);
}
};
// Set the drawer toggle as the DrawerListener
mDrawerLayout.setDrawerListener(mDrawerToggle);
getActionBar().setDisplayHomeAsUpEnabled(true);
getActionBar().setHomeButtonEnabled(true);
if (savedInstanceState == null) {
// on first time display view for first nav item
// displayView(0);
}
}
@Override
protected void onPostCreate(Bundle savedInstanceState) {
super.onPostCreate(savedInstanceState);
// Sync the toggle state after onRestoreInstanceState has occurred.
mDrawerToggle.syncState();
}
@Override
public void onConfigurationChanged(Configuration newConfig) {
super.onConfigurationChanged(newConfig);
mDrawerToggle.onConfigurationChanged(newConfig);
}
@Override
public boolean onOptionsItemSelected(MenuItem item) {
// Pass the event to ActionBarDrawerToggle, if it returns
// true, then it has handled the app icon touch event
if (mDrawerToggle.onOptionsItemSelected(item)) {
return true;
}
// Handle your other action bar items...
return super.onOptionsItemSelected(item);
}
@Override
public boolean onCreateOptionsMenu(Menu menu) {
// Inflate the menu; this adds items to the action bar if it is present.
getMenuInflater().inflate(R.menu.receber_os_menu, menu);
return true;
}
public List createDrawerItens(){
List l = new ArrayList();
l.add( new TelaPrincipalDrawerItens("Senor Abravanel","001.415.333-987", R.drawable.ic_maoe, false));
l.add( new TelaPrincipalDrawerItens("Configuações",R.drawable.ic_action_settings, false));
l.add( new TelaPrincipalDrawerItens("Upload de Dados",R.drawable.ic_action_upload, false));
l.add( new TelaPrincipalDrawerItens("Download de Dados",R.drawable.ic_action_download, false));
l.add( new TelaPrincipalDrawerItens("Sair",R.drawable.ic_sair, false));
return l;
}
}
这是我的 logCat 输出:
04-25 13:35:30.017: E/AndroidRuntime(2661): FATAL EXCEPTION: main
04-25 13:35:30.017: E/AndroidRuntime(2661): Process: com.ass.app.delivermobile,PID:2661
04-25 13:35:30.017: E/AndroidRuntime(2661): java.lang.RuntimeException: Unable to start activity ComponentInfo{com.ass.app.delivermobile/com.ass.app.delivermobile.GUI.activities.PaginaPrincipalActivity}: java.lang.NullPointerException
04-25 13:35:30.017: E/AndroidRuntime(2661): at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2195)
04-25 13:35:30.017: E/AndroidRuntime(2661): at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2245)
04-25 13:35:30.017: E/AndroidRuntime(2661): at android.app.ActivityThread.access$800(ActivityThread.java:135)
04-25 13:35:30.017: E/AndroidRuntime(2661): at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1196)
04-25 13:35:30.017: E/AndroidRuntime(2661): at android.os.Handler.dispatchMessage(Handler.java:102)
04-25 13:35:30.017: E/AndroidRuntime(2661): at android.os.Looper.loop(Looper.java:136)
04-25 13:35:30.017: E/AndroidRuntime(2661): at android.app.ActivityThread.main(ActivityThread.java:5017)
04-25 13:35:30.017: E/AndroidRuntime(2661): at java.lang.reflect.Method.invokeNative(Native Method)
04-25 13:35:30.017: E/AndroidRuntime(2661): at java.lang.reflect.Method.invoke(Method.java:515)
04-25 13:35:30.017: E/AndroidRuntime(2661): at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:779)
04-25 13:35:30.017: E/AndroidRuntime(2661): at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:595)
04-25 13:35:30.017: E/AndroidRuntime(2661): at dalvik.system.NativeStart.main(Native Method)
04-25 13:35:30.017: E/AndroidRuntime(2661): Caused by: java.lang.NullPointerException
04-25 13:35:30.017: E/AndroidRuntime(2661): at com.ass.app.delivermobile.GUI.activities.PaginaPrincipalActivity.onCreate(PaginaPrincipalActivity.java:110)
04-25 13:35:30.017: E/AndroidRuntime(2661): at android.app.Activity.performCreate(Activity.java:5231)
04-25 13:35:30.017: E/AndroidRuntime(2661): at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1087)
04-25 13:35:30.017: E/AndroidRuntime(2661): at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2159)
04-25 13:35:30.017: E/AndroidRuntime(2661): ... 11 more
请,如果有人可以请帮助我,我将非常感激!