我想創建一個通過從左向右滑動打開的導航抽屜菜單,並且我通過Google開發者教程來完成,但本教程展示瞭如何創建導航抽屜以及如何設置菜單中的項以打開新的片段佈局,但它不是我想要的......我想要的是使用導航抽屜菜單中的項目打開新活動,而且我也這樣做了,但是當Sliding.java活動必須打開時,我的應用崩潰了。 .java是包含菜單代碼的活動。如何使用導航抽屜打開新活動?
Sliding.java:
package com.orar.cngcnasaud;
import android.app.Activity;
import android.content.Intent;
import android.content.res.Configuration;
import android.os.Bundle;
import android.support.v4.app.ActionBarDrawerToggle;
import android.support.v4.view.GravityCompat;
import android.support.v4.widget.DrawerLayout;
import android.view.Menu;
import android.view.MenuInflater;
import android.view.MenuItem;
import android.view.View;
import android.widget.AdapterView;
import android.widget.ArrayAdapter;
import android.widget.ListView;
public class Sliding extends Activity {
private DrawerLayout mDrawerLayout;
private ListView mDrawerList;
private ActionBarDrawerToggle mDrawerToggle;
private CharSequence mDrawerTitle;
private CharSequence mTitle;
private String[] mPlanetTitles;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
mTitle = mDrawerTitle = getTitle();
mPlanetTitles = getResources().getStringArray(R.array.planets_array);
mDrawerLayout = (DrawerLayout) findViewById(R.id.drawer_layout);
mDrawerList = (ListView) findViewById(R.id.left_drawer);
// set a custom shadow that overlays the main content when the drawer opens
mDrawerLayout.setDrawerShadow(R.drawable.drawer_shadow, GravityCompat.START);
// set up the drawer's list view with items and click listener
mDrawerList.setAdapter(new ArrayAdapter<String>(this,
R.layout.drawer_list_item, mPlanetTitles));
mDrawerList.setOnItemClickListener(new DrawerItemClickListener());
// enable ActionBar app icon to behave as action to toggle nav drawer
getActionBar().setDisplayHomeAsUpEnabled(true);
getActionBar().setHomeButtonEnabled(true);
// ActionBarDrawerToggle ties together the the proper interactions
// between the sliding drawer and the action bar app icon
mDrawerToggle = new ActionBarDrawerToggle(
this, /* host Activity */
mDrawerLayout, /* DrawerLayout object */
R.drawable.ic_drawer, /* nav drawer image to replace 'Up' caret */
R.string.drawer_open, /* "open drawer" description for accessibility */
R.string.drawer_close /* "close drawer" description for accessibility */
) {
public void onDrawerClosed(View view) {
getActionBar().setTitle(mTitle);
invalidateOptionsMenu(); // creates call to onPrepareOptionsMenu()
}
public void onDrawerOpened(View drawerView) {
getActionBar().setTitle(mDrawerTitle);
invalidateOptionsMenu(); // creates call to onPrepareOptionsMenu()
}
};
mDrawerLayout.setDrawerListener(mDrawerToggle);
if (savedInstanceState == null) {
selectItem(0);
}
}
private void selectItem(int i) {
// TODO Auto-generated method stub
}
@Override
public boolean onCreateOptionsMenu(Menu menu) {
MenuInflater inflater = getMenuInflater();
inflater.inflate(R.menu.main, menu);
return super.onCreateOptionsMenu(menu);
}
/* Called whenever we call invalidateOptionsMenu() */
@Override
public boolean onOptionsItemSelected(MenuItem item) {
if (mDrawerToggle.onOptionsItemSelected(item)) {
return true;
}
return false;
}
private class DrawerItemClickListener implements ListView.OnItemClickListener {
@Override
public void onItemClick(AdapterView<?> arg0, View arg1, int arg2,
long arg3) {
// TODO Auto-generated method stub
}
}
@Override
public void setTitle(CharSequence title) {
mTitle = title;
getActionBar().setTitle(mTitle);
}
@Override
protected void onPostCreate(Bundle savedInstanceState) {
super.onPostCreate(savedInstanceState);
mDrawerToggle.syncState();
}
@Override
public void onConfigurationChanged(Configuration newConfig) {
super.onConfigurationChanged(newConfig);
mDrawerToggle.onConfigurationChanged(newConfig);
}
@SuppressWarnings("unused")
private void selectItem1(final int position) {
switch(position) {
case 1:
Intent a = new Intent(this, MainActivity.class);
startActivity(a);
break;
case 2:
Intent b = new Intent(this, Profesori.class);
startActivity(b);
break;
default:
}
}
}
我沒有錯誤,當我運行它,但它仍然崩潰..
編輯:
的logcat:
02-17 16:29:58.496: I/dalvikvm(322): Could not find method com.orar.cngcnasaud.Sliding.getActionBar, referenced from method com.orar.cngcnasaud.Sliding.onCreate
02-17 16:29:58.496: W/dalvikvm(322): VFY: unable to resolve virtual method 5311: Lcom/orar/cngcnasaud/Sliding;.getActionBar()Landroid/app/ActionBar;
02-17 16:29:58.496: D/dalvikvm(322): VFY: replacing opcode 0x6e at 0x0058
02-17 16:29:58.496: D/dalvikvm(322): VFY: dead code 0x005b-0087 in Lcom/orar/cngcnasaud/Sliding;.onCreate (Landroid/os/Bundle;)V
02-17 16:29:58.504: I/dalvikvm(322): Could not find method com.orar.cngcnasaud.Sliding.getActionBar, referenced from method com.orar.cngcnasaud.Sliding.setTitle
02-17 16:29:58.504: W/dalvikvm(322): VFY: unable to resolve virtual method 5311: Lcom/orar/cngcnasaud/Sliding;.getActionBar()Landroid/app/ActionBar;
02-17 16:29:58.504: D/dalvikvm(322): VFY: replacing opcode 0x6e at 0x0002
02-17 16:29:58.504: D/dalvikvm(322): VFY: dead code 0x0005-000b in Lcom/orar/cngcnasaud/Sliding;.setTitle (Ljava/lang/CharSequence;)V
02-17 16:29:58.534: D/AndroidRuntime(322): Shutting down VM
02-17 16:29:58.534: W/dalvikvm(322): threadid=1: thread exiting with uncaught exception (group=0x4001d800)
02-17 16:29:58.554: E/AndroidRuntime(322): FATAL EXCEPTION: main
02-17 16:29:58.554: E/AndroidRuntime(322): java.lang.RuntimeException: Unable to start activity ComponentInfo{com.orar.cngcnasaud/com.orar.cngcnasaud.Sliding}: java.lang.NullPointerException
02-17 16:29:58.554: E/AndroidRuntime(322): at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2663)
02-17 16:29:58.554: E/AndroidRuntime(322): at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2679)
02-17 16:29:58.554: E/AndroidRuntime(322): at android.app.ActivityThread.access$2300(ActivityThread.java:125)
02-17 16:29:58.554: E/AndroidRuntime(322): at android.app.ActivityThread$H.handleMessage(ActivityThread.java:2033)
02-17 16:29:58.554: E/AndroidRuntime(322): at android.os.Handler.dispatchMessage(Handler.java:99)
02-17 16:29:58.554: E/AndroidRuntime(322): at android.os.Looper.loop(Looper.java:123)
02-17 16:29:58.554: E/AndroidRuntime(322): at android.app.ActivityThread.main(ActivityThread.java:4627)
02-17 16:29:58.554: E/AndroidRuntime(322): at java.lang.reflect.Method.invokeNative(Native Method)
02-17 16:29:58.554: E/AndroidRuntime(322): at java.lang.reflect.Method.invoke(Method.java:521)
02-17 16:29:58.554: E/AndroidRuntime(322): at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:868)
02-17 16:29:58.554: E/AndroidRuntime(322): at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:626)
02-17 16:29:58.554: E/AndroidRuntime(322): at dalvik.system.NativeStart.main(Native Method)
02-17 16:29:58.554: E/AndroidRuntime(322): Caused by: java.lang.NullPointerException
02-17 16:29:58.554: E/AndroidRuntime(322): at com.orar.cngcnasaud.Sliding.onCreate(Sliding.java:39)
02-17 16:29:58.554: E/AndroidRuntime(322): at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1047)
02-17 16:29:58.554: E/AndroidRuntime(322): at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2627)
02-17 16:29:58.554: E/AndroidRuntime(322): ... 11 more
02-17 16:30:06.275: I/Process(322): Sending signal. PID: 322 SIG: 9
滑動.xml:
<android.support.v4.widget.DrawerLayout
xmlns:android="http://schemas.android.com/apk/res/android"
android:id="@+id/drawer_layout"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:background="@drawable/bff">
<!-- As the main content view, the view below consumes the entire
space available using match_parent in both dimensions. -->
<FrameLayout
android:id="@+id/content_frame"
android:layout_width="match_parent"
android:layout_height="match_parent" />
<ListView
android:id="@+id/left_drawer"
android:layout_width="240dp"
android:layout_height="match_parent"
android:layout_gravity="start"
android:choiceMode="singleChoice"
android:divider="@android:color/transparent"
android:dividerHeight="0dp"
android:background="#111"/>
</android.support.v4.widget.DrawerLayout>
logcat的,請... – Enrichman
很抱歉,但我總是忘記這件事:(..我編輯的第一個帖子..對不起! – Ezekiel
沒問題!讓我們來看看發生了什麼事! – Enrichman