2017-03-21 108 views
-3

這是崩潰,當我點擊開始,但我不知道爲什麼我的應用程序總是崩潰,但我不知道爲什麼

我認爲這是與在bruteForce功能threding認爲

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

} 
+1

您需要添加堆棧跟蹤。 –

+0

這是什麼,我該怎麼做 – Lendion

+0

當你的應用程序崩潰時,Android Studio中的紅色大文本。 –

回答

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)來包裝一個處理器,它將它發佈到主線程。例如

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

Looper.getMainLooper()docs)將獲得主應用程序線程該處理器然後投遞的Runnable來。

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

       text.setText(attempt); 
      } 
     }); 

this.stop(); 
} 

同樣,無論你正在改變UI元素,如設置文本或顏色了變化做它在runOnUithread正如我上面一樣。

+0

這應該不會崩潰! – Neji

+0

是的,無論你在哪裏改變用戶界面好友只要做到我在上面做的方式它會解決你的問題,你可以從主線程改變用戶界面 –

+0

我有未知的方法getActivity – Lendion

相關問題