当我运行我的代码时,我收到此错误:
: E/InputEventReceiver(1363): Exception dispatching input event.
: E/MessageQueue-JNI(1363): Exception in MessageQueue callback: handleReceiveCallback
: D/dalvikvm(1363): GC_CONCURRENT freed 1898K, 30% free 4921K/6992K, paused 78ms+107ms, total 413ms
: E/MessageQueue-JNI(1363): android.view.WindowManager$BadTokenException: Unable to add window -- token null is not for an application
: E/MessageQueue-JNI(1363):at com.example.ikmantest2.MainActivity$6.onItemClick(MainActivity.java:269)
这是我得到错误的代码:
gallery.setOnItemClickListener(new OnItemClickListener(){
@Override
public void onItemClick(AdapterView<?> arg0, View arg1, int arg2,
long arg3) {
final Dialog dialog = new Dialog(getBaseContext());
dialog.setContentView(R.layout.image_dialog_layout);
// set the custom dialog components - text, image and button
imageView=(ImageView)dialog.findViewById(R.id.bigger_image);
imageView.setImageBitmap(exListAdapter.getImageByPosition(lastClickedGroup, arg2));
pre=(ImageButton)dialog.findViewById(R.id.btn_pre);
back=(ImageButton)dialog.findViewById(R.id.btn_back);
next=(ImageButton)dialog.findViewById(R.id.btn_next);
// if button is clicked, close the custom dialog
pre.setOnClickListener(new OnClickListener() {
@Override
public void onClick(View v) {
dialog.dismiss();
}
});
back.setOnClickListener(new OnClickListener() {
@Override
public void onClick(View v) {
dialog.dismiss();
}
});
next.setOnClickListener(new OnClickListener() {
@Override
public void onClick(View v) {
dialog.dismiss();
}
});
/*this is line number 269 as in error code*/ dialog.show();
}
});
那我怎么能解决这个错误呢?
答案 0 :(得分:8)
这样做
final Dialog dialog = new Dialog(MainActivity.this);
答案 1 :(得分:2)
改变最终
Dialog dialog = new Dialog(getBaseContext());
到
final Dialog dialog = new Dialog(YourActivity.this);
答案 2 :(得分:0)
当传递错误的上下文时会发生此错误,只需更改Dailog context to activity context
。
使用:
final Dialog dialog = new Dialog(MainActivity.this);
相反:
final Dialog dialog = new Dialog(getBaseContext());
答案 3 :(得分:0)
final Dialog dialog = new Dialog(MainActivity.this);
或
final Dialog dialog = new Dialog(getApplicationContext());
答案 4 :(得分:0)
Dialog可以由Activity显示,而不是(直接)由Application显示(主要不是由Service提供),因此Dialog的构造函数需要Activity的Context而不是Application的Context。
如果您知道活动的名称,那么
final Dialog dialog = new Dialog(YOUR_ACTIVITY_NAME.this);
当然有效。
但是,如果您不想在代码中刻录活动名称,请使用以下代码段:
public class MainActivity extends Activity {
Context context; // for context of the application
Context acontext; // for context of the activity
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
context = getApplicationContext(); // Don't use for Dialog
acontext = this; // Use for Dialog
setContentView(R.layout.activity_main);
// Dialog block started
final Dialog dialog = new Dialog(acontext);
dialog.setContentView(R.layout.custom);
dialog.setTitle("Title");
Button dialogButton = (Button) dialog.findViewById(R.id.dialogButtonOK);
// if button is clicked, close the custom dialog
dialogButton.setOnClickListener(new OnClickListener() {
@Override
public void onClick(View v) {
dialog.dismiss();
}
});
dialog.show();
// Dialog block ended
}}