0

I am trying to make a splash screen for my app and then show an alert message for registration, but my app starts shows the splash picture and stops before alertDialog.

I know that in my code the app must stop after choose any of two buttons because there is no farther work to do but why it stop before?

This is my code:

public class MainActivity extends AppCompatActivity {
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        Thread timerThread = new Thread() {
            public void run() {
                try {
                    sleep(3000);}
                catch (InterruptedException e) {
                    e.printStackTrace();}
                finally {
                    AlertDialog.Builder alert= new AlertDialog.Builder(getBaseContext());
                    alert.setMessage("you are not registered")
                            .setTitle("Alert")
                            .setPositiveButton("I will register", new DialogInterface.OnClickListener() {
                                @Override
                                public void onClick(DialogInterface dialog, int which) {
                                    Toast.makeText(getBaseContext(),"hello in our app",Toast.LENGTH_LONG);
                                }})
                            .setNegativeButton("Cancel", new DialogInterface.OnClickListener() {
                                @Override
                                public void onClick(DialogInterface dialog, int which) {
                                    finish();
                                }}).show();
                }}};
        timerThread.start();
    }
    @Override
    protected void onPause() {
        super.onPause();
        finish();
    }
}

And this is the error message I got:

02-11 20:48:45.425 17071-17071/? I/art: Not late-enabling -Xcheck:jni (already on)
02-11 20:48:45.426 17071-17071/? W/art: Unexpected CPU variant for X86 using defaults: x86
02-11 20:48:45.926 17071-17071/com.project.nuha.splashforproject W/System: ClassLoader referenced unknown path: /data/app/com.project.nuha.splashforproject-1/lib/x86
02-11 20:48:45.950 17071-17071/com.project.nuha.splashforproject I/InstantRun: starting instant run server: is main process
02-11 20:48:46.241 17071-17071/com.project.nuha.splashforproject W/art: Before Android 4.1, method android.graphics.PorterDuffColorFilter android.support.graphics.drawable.VectorDrawableCompat.updateTintFilter(android.graphics.PorterDuffColorFilter, android.content.res.ColorStateList, android.graphics.PorterDuff$Mode) would have incorrectly overridden the package-private method in android.graphics.drawable.Drawable
02-11 20:48:46.713 17071-17108/com.project.nuha.splashforproject I/OpenGLRenderer: Initialized EGL, version 1.4
02-11 20:48:46.713 17071-17108/com.project.nuha.splashforproject D/OpenGLRenderer: Swap behavior 1
02-11 20:48:46.714 17071-17108/com.project.nuha.splashforproject W/OpenGLRenderer: Failed to choose config with EGL_SWAP_BEHAVIOR_PRESERVED, retrying without...
02-11 20:48:46.714 17071-17108/com.project.nuha.splashforproject D/OpenGLRenderer: Swap behavior 0
02-11 20:48:46.834 17071-17108/com.project.nuha.splashforproject D/EGL_emulation: eglCreateContext: 0xaa5852a0: maj 2 min 0 rcv 2
02-11 20:48:47.153 17071-17108/com.project.nuha.splashforproject D/EGL_emulation: eglMakeCurrent: 0xaa5852a0: ver 2 0 (tinfo 0xaa583210)
02-11 20:48:47.235 17071-17071/com.project.nuha.splashforproject W/art: Before Android 4.1, method int android.support.v7.widget.ListViewCompat.lookForSelectablePosition(int, boolean) would have incorrectly overridden the package-private method in android.widget.ListView
02-11 20:48:47.281 17071-17108/com.project.nuha.splashforproject D/EGL_emulation: eglMakeCurrent: 0xaa5852a0: ver 2 0 (tinfo 0xaa583210)
02-11 20:48:49.614 17071-17107/com.project.nuha.splashforproject E/AndroidRuntime: FATAL EXCEPTION: Thread-4
                                                                                   Process: com.project.nuha.splashforproject, PID: 17071
                                                                                   java.lang.RuntimeException: Can't create handler inside thread that has not called Looper.prepare()
                                                                                       at android.os.Handler.<init>(Handler.java:200)
                                                                                       at android.os.Handler.<init>(Handler.java:114)
                                                                                       at android.app.Dialog.<init>(Dialog.java:121)
                                                                                       at android.app.Dialog.<init>(Dialog.java:166)
                                                                                       at android.support.v7.app.AppCompatDialog.<init>(AppCompatDialog.java:46)
                                                                                       at android.support.v7.app.AlertDialog.<init>(AlertDialog.java:97)
                                                                                       at android.support.v7.app.AlertDialog$Builder.create(AlertDialog.java:929)
                                                                                       at android.support.v7.app.AlertDialog$Builder.show(AlertDialog.java:954)
                                                                                       at com.project.nuha.splashforproject.MainActivity$1.run(MainActivity.java:33)
02-11 20:48:51.135 17071-17108/com.project.nuha.splashforproject D/EGL_emulation: eglMakeCurrent: 0xaa5852a0: ver 2 0 (tinfo 0xaa583210)
marianosimone
  • 3,366
  • 26
  • 32
  • 1
    Possible duplicate of [Can't create handler inside thread that has not called Looper.prepare()](https://stackoverflow.com/questions/3875184/cant-create-handler-inside-thread-that-has-not-called-looper-prepare) – pushasha Feb 11 '18 at 18:19

1 Answers1

0

Its crashes becuase you have written UI code in non-ui thread.

write your dialog showing code in runOnUiThread

Thread timerThread = new Thread()
    {
        public void run()
        {
            try
            {
                sleep(3000);
            }
            catch (InterruptedException e)
            {
                e.printStackTrace();
            }
            finally
            {

                runOnUiThread(new Runnable()
                {
                    @Override
                    public void run()
                    {
                        AlertDialog.Builder alert = new AlertDialog.Builder(new ContextThemeWrapper(MainActivity.this, R.style.AppTheme));
                        alert.setMessage("you are not registered")
                                .setTitle("Alert")
                                .setPositiveButton("I will register", new DialogInterface.OnClickListener()
                                {
                                    @Override
                                    public void onClick(DialogInterface dialog, int which)
                                    {
                                        Toast.makeText(getBaseContext(), "hello in our app", Toast.LENGTH_LONG);
                                    }
                                })
                                .setNegativeButton("Cancel", new DialogInterface.OnClickListener()
                                {
                                    @Override
                                    public void onClick(DialogInterface dialog, int which)
                                    {
                                        finish();
                                    }
                                }).show();
                    }
                });

            }
        }
    };
    timerThread.start();

Make sure to pass right style for AlertDialog context when using the code if you are using AppCompatActivity.

for example here I have used

new AlertDialog.Builder(new ContextThemeWrapper(MainActivity.this, R.style.AppTheme));
Kirtan
  • 1,782
  • 1
  • 13
  • 35