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

时间:2017-03-21 13:30:01

标签: android multithreading android-activity

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

我认为它与bruteForce函数

中的线程有关
package com.my.app;
import android.app.*;
import android.os.*;
import android.view.*;
import android.view.View.*;
import android.widget.*;
import android.content.*;
import android.graphics.*;
import android.media.*;
import android.net.*;
import android.text.*;
import android.util.*;
import java.util.*;
import java.text.*;
import android.test.*;
import android.view.animation.*;
import android.widget.Button;


public class TestActivity extends Activity {

private LinearLayout linear1;
private TextView original;
private TextView match;
private TextView text;
private Button bstart;
String attempt = new String();

boolean running;
int counter;

private String hash = "";
public String username = new String();
public static String password = "ZZZZZ";
public static char[] charset = "ABCDEFGHIJKLMNOPQRSTUVWXYZ".toCharArray();
private static char[] currentGuess = new char[1];




@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.test);
    initialize();
    initializeLogic();
}

private void  initialize() {
    linear1 = (LinearLayout) findViewById(R.id.linear1);
    original = (TextView) findViewById(R.id.original);
    match = (TextView) findViewById(R.id.match);
    text = (TextView) findViewById(R.id.text);
    bstart = (Button) findViewById(R.id.bstart);

    bstart.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View _v) { 
                bruteForce();
            }
        });
}


private void initializeLogic() {
    Intent test = getIntent();
    hash = test.getStringExtra("hash");
    original.setText(password);
}

public void increment()
{
    int index = currentGuess.length - 1;
    while (index >= 0)
    {
        if (currentGuess[index] == charset[charset.length - 1])
        {
            if (index == 0)
            {
                currentGuess = new char[currentGuess.length + 1];
                Arrays.fill(currentGuess, charset[0]);
                break;
            }
            else
            {
                currentGuess[index] = charset[0];
                index--;
            }
        }
        else
        {
            currentGuess[index] = charset[Arrays.binarySearch(charset, currentGuess[index]) + 1];
            break;
        }
    }
}

public String toString()
{
    return String.valueOf(currentGuess);
}

public void bruteForce()
{

    Animation animation = new Animation(){
        @Override
        protected void applyTransformation(float interpolatedTime, Transformation t) {
            text.setText(attempt);
        }
    };
    animation.setRepeatCount(Animation.INFINITE);
    text.startAnimation(animation);         
    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();
}

@Override
protected void onStop() {
    super.onStop();
    running = false;
}


public void BruteForceTest()
{
    Arrays.fill(currentGuess, charset[0]);
}


// created automatically
private void ShowMessage(String _s) {
    Toast.makeText(getApplicationContext(), _s, Toast.LENGTH_SHORT).show();
}

private int GetRandom(int _minValue ,int _maxValue){
    Random random = new Random();
    return random.nextInt(_maxValue - _minValue + 1) + _minValue;
}

public ArrayList<Integer> getCheckedItemPositionsToArray(ListView _list) {
    ArrayList<Integer> _result = new ArrayList<Integer>();
    SparseBooleanArray _arr = _list.getCheckedItemPositions();
    for (int _iIdx = 0; _iIdx < _arr.size(); _iIdx++) {
        if (_arr.valueAt(_iIdx))
            _result.add(_arr.keyAt(_iIdx));
    }
    return _result;
}

}

2 个答案:

答案 0 :(得分:0)

你的问题就在这里

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.setText(attempt)包含一个处理程序,将其发布到主线程。 e.g。

new Handler(Looper.getMainLooper()).post(new Runnable(){
    void run() {
        text.setText(attempt);
    }
});

Looper.getMainLooper()docs)将获得处理程序随后将Runnable发布到的主应用程序线程。

答案 1 :(得分:0)

if (attempt.equals(password))
{
getActivity().runOnUiThread(new Runnable()
        {
            @Override
            public void run()
            {

                text.setText(attempt);
            }
        });

this.stop();
}

类似地,无论你在哪里更改ui元素,比如设置文本或变换颜色,都可以像在上面那样在runOnUithread中进行。