skip to Main Content

I tried to use a seperate class in which Alertdialog is called for showing message. But app crashes without any error



package com.example.library;

import android.app.AlertDialog;
import android.content.Context;
import android.content.DialogInterface;
import android.widget.Toast;

public class java_class {

    public void msgbox(Context context, String msg)
    {
        AlertDialog.Builder dlgAlert  = new AlertDialog.Builder(context.getApplicationContext());
        dlgAlert.setMessage(msg)
                .setCancelable(false)
                .setPositiveButton("Ok",
                        new DialogInterface.OnClickListener()
                        {
                            public void onClick(DialogInterface dialog, int which)
                            {
                                //Toast.makeText(context, "test", Toast.LENGTH_SHORT).show();
                                // no action
                            }
                        })
                .show();
    }
}

in MainActivity i called it this way..

        Context context= getApplicationContext();
        java_class msbox=new java_class();
        msbox.msgbox(context,"Test");

when used debugger it ran upto ‘public void onClick(DialogInterface dialog, int which)’ line were app crashed.

but when i used the msgbox(String msg) in Mainactivity as function it works fine.

pls help…

2

Answers


  1. Chosen as BEST ANSWER

    2023-05-05 21:46:25.102 24043-24043 AndroidRuntime com.example.library E FATAL EXCEPTION: main Process: com.example.library, PID: 24043 java.lang.RuntimeException: Unable to start activity ComponentInfo{com.example.library/com.example.library.MainActivity}: android.view.WindowManager$BadTokenException: Unable to add window -- token null is not valid; is your activity running? at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:3645) at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:3782) at android.app.servertransaction.LaunchActivityItem.execute(LaunchActivityItem.java:101) at android.app.servertransaction.TransactionExecutor.executeCallbacks(TransactionExecutor.java:135) at android.app.servertransaction.TransactionExecutor.execute(TransactionExecutor.java:95) at android.app.ActivityThread$H.handleMessage(ActivityThread.java:2307) at android.os.Handler.dispatchMessage(Handler.java:106) at android.os.Looper.loopOnce(Looper.java:201) at android.os.Looper.loop(Looper.java:288) at android.app.ActivityThread.main(ActivityThread.java:7872) at java.lang.reflect.Method.invoke(Native Method) at com.android.internal.os.RuntimeInit$MethodAndArgsCaller.run(RuntimeInit.java:548) at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:936) Caused by: android.view.WindowManager$BadTokenException: Unable to add window -- token null is not valid; is your activity running? at android.view.ViewRootImpl.setView(ViewRootImpl.java:1312) at android.view.WindowManagerGlobal.addView(WindowManagerGlobal.java:405) at android.view.WindowManagerImpl.addView(WindowManagerImpl.java:148) at android.app.Dialog.show(Dialog.java:352) at android.app.AlertDialog$Builder.show(AlertDialog.java:1131) at com.example.library.javaclass.msgbox(javaclass.java:23) at com.example.library.MainActivity.onCreate(MainActivity.java:32) at android.app.Activity.performCreate(Activity.java:8305) at android.app.Activity.performCreate(Activity.java:8284) at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1417) at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:3626) at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:3782)  at android.app.servertransaction.LaunchActivityItem.execute(LaunchActivityItem.java:101)  at android.app.servertransaction.TransactionExecutor.executeCallbacks(TransactionExecutor.java:135)  at android.app.servertransaction.TransactionExecutor.execute(TransactionExecutor.java:95)  at android.app.ActivityThread$H.handleMessage(ActivityThread.java:2307)  at android.os.Handler.dispatchMessage(Handler.java:106)  at android.os.Looper.loopOnce(Looper.java:201)  at android.os.Looper.loop(Looper.java:288)  at android.app.ActivityThread.main(ActivityThread.java:7872)  at java.lang.reflect.Method.invoke(Native Method)  at com.android.internal.os.RuntimeInit$MethodAndArgsCaller.run(RuntimeInit.java:548)  at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:936)  2023-05-05 21:46:25.663 24043-24043 Process com.example.library I Sending signal. PID: 24043 SIG: 9 ---------------------------- PROCESS ENDED (24043) for package com.example.library ----------------------------

    This is screen shot of debugger... it goes till line. once it reaches "public void onClick(DialogInterface dialog, int which)" debugger crashes


  2. There’s no need to ask app context from context.

    import android.app.AlertDialog;
    import android.content.Context;
    import android.content.DialogInterface;
    
    
    public class JavaClass {
    
        public void msgbox(Context context, String msg)
        {
            AlertDialog.Builder dlgAlert  = new AlertDialog.Builder(context); //<- this changed 
            dlgAlert.setMessage(msg)
                    .setCancelable(false)
                    .setPositiveButton("Ok",
                            new DialogInterface.OnClickListener()
                            {
                                public void onClick(DialogInterface dialog, int which)
                                {
                                    //Toast.makeText(context, "test", Toast.LENGTH_SHORT).show();
                                    // no action
                                }
                            })
                    .show();
        }
    }
    

    also you can use this to get context in activity class.

    JavaClass javaClass = new JavaClass();
    javaClass.msgbox(this, "Hello World2");
    

    In Android, we can use the this keyword to get the context of the current Activity instance. When used inside an Activity, this refers to that Activity instance, which is a subclass of the Context class. If used inside a listener, we can reference the current context by using the Activity name, such as MainActivity.this in Java or this@MainActivity in Kotlin.

    enter image description here

    Login or Signup to reply.
Please signup or login to give your own answer.
Back To Top
Search