-3

this is the view that crashes when i click start but i dont know why

i think it has something to do with the threding in the bruteForce function

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

}
Lendion
  • 307
  • 1
  • 3
  • 9

2 Answers2

0

Your problem lies here

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

You can only update an UI element from the main thread. If you want to do it likes this you need to wrap text.setText(attempt) with a handler that posts it to the main thread. e.g.

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

Looper.getMainLooper() (docs) will get the main application thread which the handler then posts the Runnable to .

Ryan
  • 1,863
  • 13
  • 20
0
if (attempt.equals(password))
{
getActivity().runOnUiThread(new Runnable()
        {
            @Override
            public void run()
            {

                text.setText(attempt);
            }
        });

this.stop();
}

Similarly wherever you are changing ui elements like setting text or changin colors do it in runOnUithread as I did above.

Muhammad Saad Rafique
  • 3,158
  • 1
  • 13
  • 21