每当我发起我的活动时,它都会崩溃。我不知道问题是什么。我的代码,xml资源和Android清单看起来很好。如果有人可以 帮助我,我将不胜感激!
问题是由android.view.WindowManager$BadTokenException
引起的,这在LogCat中提到
Java代码
package com.example.hp.machine;
import android.app.AlertDialog;
import android.content.DialogInterface;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.view.View;
import android.widget.Button;
import android.widget.Toast;
public class Machine extends AppCompatActivity {
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.machine);
Button click = (Button) findViewById(R.id.click);
click.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
AlertDialog.Builder builder = new AlertDialog.Builder(Machine.this);
builder.setTitle("Warning")
.setIcon(R.drawable.bomb)
.setMessage("Do you want to Die ?")
.setCancelable(false) ;
AlertDialog alert = builder.create();
alert.show();
}
});
}
}
XML
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:id="@+id/machine"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:paddingBottom="@dimen/activity_vertical_margin"
android:paddingLeft="@dimen/activity_horizontal_margin"
android:paddingRight="@dimen/activity_horizontal_margin"
android:paddingTop="@dimen/activity_vertical_margin"
tools:context="com.example.hp.machine.Machine"
android:background="@drawable/splash_screen"
>
<Button
android:layout_height="wrap_content"
android:layout_width="130dp"
android:text="click"
android:layout_marginTop="200dp"
android:id="@+id/click"
android:textColor="@android:color/white"
android:textStyle="bold"
android:fontFamily="serif"
android:background="@android:color/background_dark"
/>
</RelativeLayout>
清单
<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
package="com.example.hp.machine">
<application
android:allowBackup="true"
android:icon="@mipmap/ic_launcher"
android:label="@string/app_name"
android:supportsRtl="true"
android:theme="@style/AppTheme">
<activity android:name=".Machine">
<intent-filter>
<action android:name="android.intent.action.MAIN" />
<category android:name="android.intent.category.LAUNCHER" />
</intent-filter>
</activity>
</application>
</manifest>
logcat的
Caused by: android.view.WindowManager$BadTokenException: Unable to add window -- token null is not for an application
at android.view.ViewRootImpl.setView(ViewRootImpl.java:789)
at android.view.WindowManagerGlobal.addView(WindowManagerGlobal.java:298)
at android.view.WindowManagerImpl.addView(WindowManagerImpl.java:91)
at android.app.Dialog.show(Dialog.java:325)
at com.example.hp.machine.Machine.onCreate(Machine.java:46)
at android.app.Activity.performCreate(Activity.java:6609)
at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1134)
at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:3113)
at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:3275)
at android.app.ActivityThread.access$1000(ActivityThread.java:218)
at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1744)
at android.os.Handler.dispatchMessage(Handler.java:102)
at android.os.Looper.loop(Looper.java:145)
at android.app.ActivityThread.main(ActivityThread.java:7007)
at java.lang.reflect.Method.invoke(Native Method)
at java.lang.reflect.Method.invoke(Method.java:372)
at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:1404) at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:1199)
07-19 17:13:23.830 28841-28841/com.example.hp.machine I/Process: Sending signal. PID: 28841 SIG: 9
答案 0 :(得分:0)
将getBaseContext()
更改为Machine.this
package com.example.hp.machine;
import android.app.AlertDialog;
import android.content.DialogInterface;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.view.View;
import android.widget.Button;
import android.widget.Toast;
public class Machine extends AppCompatActivity {
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.machine);
Button click = (Button) findViewById(R.id.click);
AlertDialog.Builder builder = new AlertDialog.Builder(Machine.this);
builder.setTitle("Warning")
.setIcon(R.drawable.bomb)
.setMessage("Do you want to Die ?")
.setCancelable(false) ;
AlertDialog alert = builder.create();
alert.show();
}
}
答案 1 :(得分:0)
我假设AppCompatActivity中的空间(扩展部分)是SO导入问题。
使用getBaseContext时,应该将上下文引用到当前对象:
AlertDialog.Builder builder = new AlertDialog.Builder(Machine.this
/*Inside nested classes, this refers to that class. This is just good practice to do even
if you aren't inside a nested class/thread*/);
显示对话框时,您需要这样做:
在UI线程外部使用最终会泄露窗口。
现在看,你在不同的线程上运行。
在onClick方法中,添加以下内容:
runOnUiThread(new Runnable() {
@Override
public void run() {
//Add your dialog code here
}
});
并在里面添加对话框代码。
答案 2 :(得分:0)
您无法在 onCreate()方法中编写AlertDialog。
将其替换为 onResume()方法。
答案 3 :(得分:0)
有一件事我不明白在创建活动时如何调用onClick。 应该在点击事件时调用onClick。