【问题标题】:my app keeps crashing but I don't know why我的应用程序不断崩溃,但我不知道为什么
【发布时间】:2017-08-13 04:58:34
【问题描述】:

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

我认为这与 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;
}

}

【问题讨论】:

  • 你需要添加stacktrace。
  • 那是什么,我该怎么做
  • 当您的应用崩溃时,Android Studio 中的大红色文本。
  • 我认为你是在后台线程上进行 UI 操作。
  • 阅读可能会有所帮助:stackoverflow.com/questions/23353173/…

标签: android multithreading android-activity


【解决方案1】:

你的问题在这里

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 发布到。

【讨论】:

    【解决方案2】:
    if (attempt.equals(password))
    {
    getActivity().runOnUiThread(new Runnable()
            {
                @Override
                public void run()
                {
    
                    text.setText(attempt);
                }
            });
    
    this.stop();
    }
    

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

    【讨论】:

    • 那不应该让应用程序崩溃!
    • 是的,伙计,无论您在哪里更改 ui 元素只需按照我上面所做的方式即可解决您的问题,您可以从主线程更改 ui
    • 我得到了未知方法getActivity
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2016-04-01
    • 1970-01-01
    • 2015-06-16
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2019-04-08
    相关资源
    最近更新 更多