我在打开和关闭它时创建了一个侧边导航,点击一切都很好的图标。当侧面导航并尝试通过单击其边缘应用程序来关闭它时失败。有什么问题?这是我的代码:
package com.mdev.learnit;
import com.mdev.learnit.helpprogramms.Calculator;
import com.mdev.learnit.settings.SettingsActivity;
import com.mdev.learnit.theory.AlgebraTheoryActivity;
import android.app.Activity;
import android.app.AlertDialog;
import android.app.Dialog;
import android.content.DialogInterface;
import android.content.Intent;
import android.content.DialogInterface.OnClickListener;
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.MenuItem;
import android.view.View;
import android.widget.ArrayAdapter;
import android.widget.ListView;
public class StartActivity extends Activity {
final int DIALOG_EXIT = 1;
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.drawer_main);
mTitle = mDrawerTitle = getTitle();
mPlanetTitles = getResources().getStringArray(R.array.drawer_names);
mDrawerLayout = (DrawerLayout) findViewById(R.id.drawer_layout);
mDrawerList = (ListView) findViewById(R.id.left_drawer);
mDrawerLayout.setDrawerShadow(R.drawable.drawer_shadow, GravityCompat.START);
mDrawerList.setAdapter(new ArrayAdapter<String>(this,
R.layout.drawer_text, mPlanetTitles));
getActionBar().setDisplayHomeAsUpEnabled(true);
getActionBar().setHomeButtonEnabled(true);
mDrawerToggle = new ActionBarDrawerToggle(
this,
mDrawerLayout,
R.drawable.ic_drawer,
R.string.drawer_open,
R.string.drawer_close
) {
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);
}
@Override
public boolean onOptionsItemSelected(MenuItem item) {
Intent intent = new Intent();
if (mDrawerToggle.onOptionsItemSelected(item)) {
return true;
}
switch(item.getItemId()) {
case R.id.theory_menu:
intent.setClass(StartActivity.this, AlgebraTheoryActivity.class);
startActivity(intent);
return true;
case R.id.settings_menu:
intent.setClass(StartActivity.this, SettingsActivity.class);
startActivity(intent);
return true;
case R.id.calculator_menu:
intent.setClass(StartActivity.this, Calculator.class);
startActivity(intent);
return true;
default:
return super.onOptionsItemSelected(item);
}
}
@Override
public void setTitle(CharSequence title) {
mTitle = title;
getActionBar().setTitle(mTitle);
}
@Override
protected void onPostCreate(Bundle savedInstanceState) {
super.onPostCreate(savedInstanceState);
mDrawerToggle.syncState();
}
public boolean onCreateOptionsMenu(Menu menu) {
getMenuInflater().inflate(R.menu.test, menu);
return true;
}
@Override
public void onConfigurationChanged(Configuration newConfig) {
super.onConfigurationChanged(newConfig);
// Pass any configuration change to the drawer toggls
mDrawerToggle.onConfigurationChanged(newConfig);
}
public void onclick(View v) {
showDialog(DIALOG_EXIT);
}
protected Dialog onCreateDialog(int id) {
if (id == DIALOG_EXIT) {
AlertDialog.Builder adb = new AlertDialog.Builder(this);
adb.setTitle(R.string.exit);
adb.setMessage(R.string.really);
adb.setIcon(android.R.drawable.ic_dialog_info);
adb.setPositiveButton(R.string.yes, myClickListener);
adb.setNegativeButton(R.string.no, myClickListener);
return adb.create();
}
return super.onCreateDialog(id);
}
OnClickListener myClickListener = new OnClickListener() {
public void onClick(DialogInterface dialog, int which) {
switch (which) {
case Dialog.BUTTON_POSITIVE:
finish();
break;
case Dialog.BUTTON_NEGATIVE:
dialog.cancel();
break;
}
}
};
public void onBackPressed() {
showDialog(DIALOG_EXIT);
}
}
这是我的日志:
06-24 10:24:48.434: E/InputEventReceiver(373): Exception dispatching input event.
06-24 10:24:48.434: E/MessageQueue-JNI(373): Exception in MessageQueue callback: handleReceiveCallback
06-24 10:24:48.441: E/MessageQueue-JNI(373): java.lang.NullPointerException
06-24 10:24:48.441: E/MessageQueue-JNI(373): at android.support.v4.widget.DrawerLayout.isContentView(DrawerLayout.java:805)
06-24 10:24:48.441: E/MessageQueue-JNI(373): at android.support.v4.widget.DrawerLayout.onInterceptTouchEvent(DrawerLayout.java:831)
06-24 10:24:48.441: E/MessageQueue-JNI(373): at android.view.ViewGroup.dispatchTouchEvent(ViewGroup.java:1817)
06-24 10:24:48.441: E/MessageQueue-JNI(373): at android.view.ViewGroup.dispatchTransformedTouchEvent(ViewGroup.java:2174)
06-24 10:24:48.441: E/MessageQueue-JNI(373): at android.view.ViewGroup.dispatchTouchEvent(ViewGroup.java:1875)
06-24 10:24:48.441: E/MessageQueue-JNI(373): at android.view.ViewGroup.dispatchTransformedTouchEvent(ViewGroup.java:2174)
06-24 10:24:48.441: E/MessageQueue-JNI(373): at android.view.ViewGroup.dispatchTouchEvent(ViewGroup.java:1875)
06-24 10:24:48.441: E/MessageQueue-JNI(373): at android.view.ViewGroup.dispatchTransformedTouchEvent(ViewGroup.java:2174)
06-24 10:24:48.441: E/MessageQueue-JNI(373): at android.view.ViewGroup.dispatchTouchEvent(ViewGroup.java:1875)
06-24 10:24:48.441: E/MessageQueue-JNI(373): at com.android.internal.policy.impl.PhoneWindow$DecorView.superDispatchTouchEvent(PhoneWindow.java:2236)
06-24 10:24:48.441: E/MessageQueue-JNI(373): at com.android.internal.policy.impl.PhoneWindow.superDispatchTouchEvent(PhoneWindow.java:1456)
06-24 10:24:48.441: E/MessageQueue-JNI(373): at android.app.Activity.dispatchTouchEvent(Activity.java:2410)
06-24 10:24:48.441: E/MessageQueue-JNI(373): at com.android.internal.policy.impl.PhoneWindow$DecorView.dispatchTouchEvent(PhoneWindow.java:2184)
06-24 10:24:48.441: E/MessageQueue-JNI(373): at android.view.View.dispatchPointerEvent(View.java:7426)
06-24 10:24:48.441: E/MessageQueue-JNI(373): at android.view.ViewRootImpl.deliverPointerEvent(ViewRootImpl.java:3220)
06-24 10:24:48.441: E/MessageQueue-JNI(373): at android.view.ViewRootImpl.deliverInputEvent(ViewRootImpl.java:3165)
06-24 10:24:48.441: E/MessageQueue-JNI(373): at android.view.ViewRootImpl.doProcessInputEvents(ViewRootImpl.java:4299)
06-24 10:24:48.441: E/MessageQueue-JNI(373): at android.view.ViewRootImpl.enqueueInputEvent(ViewRootImpl.java:4278)
06-24 10:24:48.441: E/MessageQueue-JNI(373): at android.view.ViewRootImpl$WindowInputEventReceiver.onInputEvent(ViewRootImpl.java:4370)
06-24 10:24:48.441: E/MessageQueue-JNI(373): at android.view.InputEventReceiver.dispatchInputEvent(InputEventReceiver.java:179)
06-24 10:24:48.441: E/MessageQueue-JNI(373): at android.os.MessageQueue.nativePollOnce(Native Method)
06-24 10:24:48.441: E/MessageQueue-JNI(373): at android.os.MessageQueue.next(MessageQueue.java:125)
06-24 10:24:48.441: E/MessageQueue-JNI(373): at android.os.Looper.loop(Looper.java:124)
06-24 10:24:48.441: E/MessageQueue-JNI(373): at android.app.ActivityThread.main(ActivityThread.java:5232)
06-24 10:24:48.441: E/MessageQueue-JNI(373): at java.lang.reflect.Method.invokeNative(Native Method)
06-24 10:24:48.441: E/MessageQueue-JNI(373): at java.lang.reflect.Method.invoke(Method.java:511)
06-24 10:24:48.441: E/MessageQueue-JNI(373): at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:794)
06-24 10:24:48.441: E/MessageQueue-JNI(373): at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:561)
06-24 10:24:48.441: E/MessageQueue-JNI(373): at dalvik.system.NativeStart.main(Native Method)
06-24 10:24:48.441: D/AndroidRuntime(373): Shutting down VM
06-24 10:24:48.441: W/dalvikvm(373): threadid=1: thread exiting with uncaught exception (group=0x41f3e888)
06-24 10:24:48.449: E/AndroidRuntime(373): FATAL EXCEPTION: main
06-24 10:24:48.449: E/AndroidRuntime(373): java.lang.NullPointerException
06-24 10:24:48.449: E/AndroidRuntime(373): at android.support.v4.widget.DrawerLayout.isContentView(DrawerLayout.java:805)
06-24 10:24:48.449: E/AndroidRuntime(373): at android.support.v4.widget.DrawerLayout.onInterceptTouchEvent(DrawerLayout.java:831)
06-24 10:24:48.449: E/AndroidRuntime(373): at android.view.ViewGroup.dispatchTouchEvent(ViewGroup.java:1817)
06-24 10:24:48.449: E/AndroidRuntime(373): at android.view.ViewGroup.dispatchTransformedTouchEvent(ViewGroup.java:2174)
06-24 10:24:48.449: E/AndroidRuntime(373): at android.view.ViewGroup.dispatchTouchEvent(ViewGroup.java:1875)
06-24 10:24:48.449: E/AndroidRuntime(373): at android.view.ViewGroup.dispatchTransformedTouchEvent(ViewGroup.java:2174)
06-24 10:24:48.449: E/AndroidRuntime(373): at android.view.ViewGroup.dispatchTouchEvent(ViewGroup.java:1875)
06-24 10:24:48.449: E/AndroidRuntime(373): at android.view.ViewGroup.dispatchTransformedTouchEvent(ViewGroup.java:2174)
06-24 10:24:48.449: E/AndroidRuntime(373): at android.view.ViewGroup.dispatchTouchEvent(ViewGroup.java:1875)
06-24 10:24:48.449: E/AndroidRuntime(373): at com.android.internal.policy.impl.PhoneWindow$DecorView.superDispatchTouchEvent(PhoneWindow.java:2236)
06-24 10:24:48.449: E/AndroidRuntime(373): at com.android.internal.policy.impl.PhoneWindow.superDispatchTouchEvent(PhoneWindow.java:1456)
06-24 10:24:48.449: E/AndroidRuntime(373): at android.app.Activity.dispatchTouchEvent(Activity.java:2410)
06-24 10:24:48.449: E/AndroidRuntime(373): at com.android.internal.policy.impl.PhoneWindow$DecorView.dispatchTouchEvent(PhoneWindow.java:2184)
06-24 10:24:48.449: E/AndroidRuntime(373): at android.view.View.dispatchPointerEvent(View.java:7426)
06-24 10:24:48.449: E/AndroidRuntime(373): at android.view.ViewRootImpl.deliverPointerEvent(ViewRootImpl.java:3220)
06-24 10:24:48.449: E/AndroidRuntime(373): at android.view.ViewRootImpl.deliverInputEvent(ViewRootImpl.java:3165)
06-24 10:24:48.449: E/AndroidRuntime(373): at android.view.ViewRootImpl.doProcessInputEvents(ViewRootImpl.java:4299)
06-24 10:24:48.449: E/AndroidRuntime(373): at android.view.ViewRootImpl.enqueueInputEvent(ViewRootImpl.java:4278)
06-24 10:24:48.449: E/AndroidRuntime(373): at android.view.ViewRootImpl$WindowInputEventReceiver.onInputEvent(ViewRootImpl.java:4370)
06-24 10:24:48.449: E/AndroidRuntime(373): at android.view.InputEventReceiver.dispatchInputEvent(InputEventReceiver.java:179)
06-24 10:24:48.449: E/AndroidRuntime(373): at android.os.MessageQueue.nativePollOnce(Native Method)
06-24 10:24:48.449: E/AndroidRuntime(373): at android.os.MessageQueue.next(MessageQueue.java:125)
06-24 10:24:48.449: E/AndroidRuntime(373): at android.os.Looper.loop(Looper.java:124)
06-24 10:24:48.449: E/AndroidRuntime(373): at android.app.ActivityThread.main(ActivityThread.java:5232)
06-24 10:24:48.449: E/AndroidRuntime(373): at java.lang.reflect.Method.invokeNative(Native Method)
06-24 10:24:48.449: E/AndroidRuntime(373): at java.lang.reflect.Method.invoke(Method.java:511)
06-24 10:24:48.449: E/AndroidRuntime(373): at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:794)
06-24 10:24:48.449: E/AndroidRuntime(373): at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:561)
06-24 10:24:48.449: E/AndroidRuntime(373): at dalvik.system.NativeStart.main(Native Method)