我已经写完了我的BACK按钮,因为我创建了一个警报对话框,以便在按下后退按钮时显示。但我正在关注异常。
06-03 12:49:56.304: I/AFSDFDS(10186): Back Pressed
06-03 12:49:56.335: D/AndroidRuntime(10186): Shutting down VM
06-03 12:49:56.335: W/dalvikvm(10186): threadid=1: thread exiting with uncaught exception (group=0x40018578)
06-03 12:49:56.343: E/AndroidRuntime(10186): FATAL EXCEPTION: main
06-03 12:49:56.343: E/AndroidRuntime(10186): android.view.WindowManager$BadTokenException: Unable to add window -- token null is not for an application
06-03 12:49:56.343: E/AndroidRuntime(10186): at android.view.ViewRoot.setView(ViewRoot.java:536)
06-03 12:49:56.343: E/AndroidRuntime(10186): at android.view.WindowManagerImpl.addView(WindowManagerImpl.java:177)
06-03 12:49:56.343: E/AndroidRuntime(10186): at android.view.WindowManagerImpl.addView(WindowManagerImpl.java:91)
06-03 12:49:56.343: E/AndroidRuntime(10186): at android.app.Dialog.show(Dialog.java:241)
06-03 12:49:56.343: E/AndroidRuntime(10186): at com.example.nirbhaya.MainActivity.onBackPressed(MainActivity.java:174)
06-03 12:49:56.343: E/AndroidRuntime(10186): at android.app.Activity.onKeyUp(Activity.java:1898)
06-03 12:49:56.343: E/AndroidRuntime(10186): at android.view.KeyEvent.dispatch(KeyEvent.java:1294)
06-03 12:49:56.343: E/AndroidRuntime(10186): at android.app.Activity.dispatchKeyEvent(Activity.java:2078)
06-03 12:49:56.343: E/AndroidRuntime(10186): at com.android.internal.policy.impl.PhoneWindow$DecorView.dispatchKeyEvent(PhoneWindow.java:1741)
06-03 12:49:56.343: E/AndroidRuntime(10186): at android.view.ViewRoot.deliverKeyEventToViewHierarchy(ViewRoot.java:2648)
06-03 12:49:56.343: E/AndroidRuntime(10186): at android.view.ViewRoot.handleFinishedEvent(ViewRoot.java:2623)
06-03 12:49:56.343: E/AndroidRuntime(10186): at android.view.ViewRoot.handleMessage(ViewRoot.java:1873)
06-03 12:49:56.343: E/AndroidRuntime(10186): at android.os.Handler.dispatchMessage(Handler.java:99)
06-03 12:49:56.343: E/AndroidRuntime(10186): at android.os.Looper.loop(Looper.java:130)
06-03 12:49:56.343: E/AndroidRuntime(10186): at android.app.ActivityThread.main(ActivityThread.java:3687)
06-03 12:49:56.343: E/AndroidRuntime(10186): at java.lang.reflect.Method.invokeNative(Native Method)
06-03 12:49:56.343: E/AndroidRuntime(10186): at java.lang.reflect.Method.invoke(Method.java:507)
06-03 12:49:56.343: E/AndroidRuntime(10186): at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:867)
06-03 12:49:56.343: E/AndroidRuntime(10186): at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:625)
06-03 12:49:56.343: E/AndroidRuntime(10186): at dalvik.system.NativeStart.main(Native Method)
和我的java代码:
package com.example.nirbhaya;
import android.app.Activity;
import android.app.AlertDialog;
import android.content.Context;
import android.content.DialogInterface;
import android.content.Intent;
import android.os.Bundle;
import android.util.Log;
import android.view.MenuInflater;
import android.view.MenuItem;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
import android.widget.LinearLayout;
import android.widget.Toast;
public class MainActivity extends Activity implements OnClickListener {
Button bdefsms,bsms,bmms,bemail;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
LinearLayout ll = (LinearLayout)findViewById(R.id.myLinear);
//ll.setBackgroundColor(Color.RED);
initialiasition();
}
private void initialiasition() {
// TODO Auto-generated method stub
bdefsms = (Button)findViewById(R.id.bDefSms);
bsms = (Button)findViewById(R.id.bSMS);
bmms = (Button)findViewById(R.id.bMMS);
bemail = (Button)findViewById(R.id.bMail);
bdefsms.setOnClickListener(this);
bsms.setOnClickListener(this);
bmms.setOnClickListener(this);
bemail.setOnClickListener(this);
}
@Override
public void onClick(View arg0) {
// TODO Auto-generated method stub
.............
................
.............
}
public void onBackPressed() {
// do something on back.
Log.i("AFSDFDS","Back Pressed");
AlertDialog.Builder alertDialogBuilder2 = new AlertDialog.Builder(getBaseContext());
alertDialogBuilder2.setTitle("EXIT");
alertDialogBuilder2.setMessage("ARE YOU SURE?").setCancelable(false)
.setPositiveButton("Yes",new DialogInterface.OnClickListener() {
public void onClick(DialogInterface dialog,int id) {
finish();
}
})
.setNegativeButton("No",new DialogInterface.OnClickListener() {
public void onClick(DialogInterface dialog,int id) {
dialog.cancel();
}
});
AlertDialog alertDialog2 = alertDialogBuilder2.create();
alertDialog2.show();
}
}
在获得例外
alertDialog2.show();