-1

Whenever I launch my activity it crashes. I don't know what the problem is. My code, xml resources and Android manifest looks fine. If anyone could help me that would be appreciated!

The issue is caused by a android.view.WindowManager$BadTokenException, which is mentioned in the LogCat

Java Code

 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>

Manifest

<?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

4 Answers4

0

Change the getBaseContext() to 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

I assume the space in AppCompatActivity (the extend part) is an SO import issue.

You use getBaseContext, when you should reference the context to the current object:

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*/);

When showing dialogs, you need to do it:

  • On the UI thread
  • With the application context

Using outside the UI thread ends up with leaked window.


Just saw now, you run on a different thread.

Inside the onClick method, you add this:

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

and add the dialog code inside.

Zoe
  • 27,060
  • 21
  • 118
  • 148
0

You can not write AlertDialog in onCreate() method.

Replace it at onResume() method.

0

One thing I do not understand how come onClick get called while activity is being created. onClick should be called on clicking event.

Imran Baig
  • 36
  • 2
  • 1) this is a comment. 2) Some people leave out details when asking questions, like the fact that they press a button to trigger it. The dialog is the reason, and it isn't magically called. OP has to press the button, or so the stacktrace says – Zoe Jul 19 '17 at 15:26