2017-07-19 80 views
-1

每當我啓動我的活動它崩潰。我不知道問題是什麼。我的代碼,XML資源和Android清單看起來很好。如果有人可以 幫助我,將不勝感激!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

[查看我的答案更新](https://stackoverflow.com/a/45193312/6296561) – Zoe

回答

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(); 



      } 
     } 
+0

我試過,但這給了我錯誤..然後我試圖(Machine.this)它工作正常。沒有錯誤彈出。仍然我的應用程序崩潰..我認爲問題是與軟件 –

+0

嘗試@Pratik Dhingani解決方案... ... - 結合Machine.this –

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線程
  • 隨着應用程序上下文

在UI線程外部使用以leake結束d窗口。


剛纔看到了,你在不同的線程上運行。

裏面的onClick方法,你補充一點:

runOnUiThread(new Runnable() { 
    @Override 
    public void run() { 
     //Add your dialog code here 
    } 
}); 

,並添加里面的對話框代碼。

0

您不能在onCreate()方法中寫入AlertDialog。

將其替換爲onResume()方法。

+0

你能解釋爲什麼不呢? –

+0

如果我在該方法中聲明一個方法並執行警告對話框,然後在onCreate()方法中調用它,它會起作用嗎? –

+0

我嘗試此解決方案,但無法正常工作。可能是Android Studios的問題 –

0

有一件事我不明白如何在創建活動時得到調用。 onClick應該在點擊事件時被調用。

+0

1)這是一條評論。 2)有些人在提問時忽略了細節,比如他們按下了一個按鈕來觸發它。對話是理由,並不是神奇地被稱爲。 OP必須按下按鈕,或者如此棧跟蹤說 – Zoe