2017-03-21 4 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

당신의 문제는 당신은 메인 스레드에서 UI 요소를 업데이트 할 수 있습니다 여기에

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

있다. 이것을 원한다면 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

그 응용 프로그램을 충돌하지 shouldnt! – Neji

+0

예 buddy 어디서나 UI 요소를 변경하고 있습니다. 위의 방식대로 작동합니다. 문제를 해결할 수 있습니다. 주 스레드에서 UI를 변경할 수 있습니다. –

+0

알 수없는 getActivity 메소드가 있습니다. – Lendion

관련 문제