Warning: file_get_contents(/data/phpspider/zhask/data//catemap/3/android/195.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Android 我的应用程序一直崩溃,但我不知道为什么_Android_Multithreading_Android Activity - Fatal编程技术网

Android 我的应用程序一直崩溃,但我不知道为什么

Android 我的应用程序一直崩溃,但我不知道为什么,android,multithreading,android-activity,Android,Multithreading,Android Activity,这是当我单击“开始”时崩溃的视图,但我不知道为什么 我认为这与bruteForce函数中的threding有关 你的问题就在这里 new Thread(){ @Override public void run() { running = true; while(running) if (attempt.equals(password)) { text.setText(a

这是当我单击“开始”时崩溃的视图,但我不知道为什么

我认为这与bruteForce函数中的threding有关

你的问题就在这里

new Thread(){
    @Override
    public void run() {
        running = true;
        while(running)
            if (attempt.equals(password))
            {
                text.setText(attempt);
                this.stop();
            }
        attempt = toString();
        text.setText(attempt);
        increment();
    }
}.start();
只能从主线程更新UI元素。如果您想这样做,您需要使用一个将text.settexttrunt发布到主线程的处理程序包装text.settexttrunt。e、 g

new Handler(Looper.getMainLooper()).post(new Runnable(){
    void run() {
        text.setText(attempt);
    }
});
getMainLooper将获取主应用程序线程,然后处理程序将Runnable发布到该线程


类似地,无论你在哪里更改ui元素,如设置文本或更改颜色,都要像我上面所做的那样在runOnUithread中进行操作。

你需要添加stacktrace。这是什么,我该怎么做?当你的应用程序崩溃时,Android Studio中的红色大文本。我认为你是在后台线程上进行ui操作。这可能有助于阅读:不应该崩溃应用程序!是的,伙计,无论你在哪里更改ui元素,只要按照我上面所做的方式去做就可以解决你的问题,你可以从主线程对ui进行更改,我得到了未知的方法getActivity
new Handler(Looper.getMainLooper()).post(new Runnable(){
    void run() {
        text.setText(attempt);
    }
});
if (attempt.equals(password))
{
getActivity().runOnUiThread(new Runnable()
        {
            @Override
            public void run()
            {

                text.setText(attempt);
            }
        });

this.stop();
}