启动消息和警报对话框

时间:2018-02-11 18:14:56

标签: android

我正在尝试为我的应用制作启动画面,然后显示注册的提醒消息,但我的应用启动显示启动画面并在alertDialog之前停止。

我知道在我的代码中,应用程序必须在选择任意两个按钮后停止,因为没有更多的工作要做,但为什么它之前停止?

这是我的代码:

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

这是我收到的错误消息:

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)

1 个答案:

答案 0 :(得分:0)

它的崩溃是因为你在非ui线程中编写了UI代码。

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

如果您使用的是AppCompatActivity,请确保在使用代码时为AlertDialog上下文传递正确的样式。

例如我在这里使用了

new AlertDialog.Builder(new ContextThemeWrapper(MainActivity.this, R.style.AppTheme));