我一直在遇到这个错误,我似乎无法弄清楚如何修复它,因为我对java和android开发相当新,所以我真的很感激我能得到的任何帮助!
我遇到的错误是,当我使用.setText()
定期更新TextView
元素时,屏幕上显示的文字实际上从未发生变化。
我相信这是由于startCrunching()
方法中的while(true)循环,我用来运行主计算过程,因为在我启动该方法之前,屏幕使用测试数据更新了喂它。
我也知道,当while循环启动时,updateScreen()
方法只是从while循环调用而不是重复处理程序,因为处理程序在方法启动时停止将日志发布到logcat但是while循环日志开始发布。
我想要实现的是while(true)循环尽可能快地运行,同时经常(作为时间变量而不是while循环的循环)使用有关while循环中的进程的信息更新屏幕
我知道while循环正在运行,并且正在调用updateScreen()
方法。
以下完整资料来源:
package com.example.android.collatzconjecturepathcruncher;
import android.os.Handler;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.util.Log;
import android.view.View;
import android.widget.EditText;
import android.widget.TextView;
import java.math.BigInteger;
import java.util.concurrent.TimeUnit;
public class MainActivity extends AppCompatActivity {
TextView longestPathDisplay;
TextView longestPathSeedDisplay;
TextView currentSeedDisplay;
EditText startingNumberDisplay;
BigInteger longestPathSeed= BigInteger.ONE;
int longestPath=0;
BigInteger currentSeed=BigInteger.ZERO;
int currentPath=0;
BigInteger workingSeed=BigInteger.ONE;
boolean run;
int temp =0;
private Handler mHandler;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
longestPathDisplay = findViewById(R.id.longest_path);
longestPathSeedDisplay = findViewById(R.id.longest_path_seed);
currentSeedDisplay = findViewById(R.id.current_seed_display);
startingNumberDisplay = findViewById(R.id.starting_number_display);
longestPathDisplay.setText(getString(R.string.longest_path_display,longestPath));
longestPathSeedDisplay.setText(getString(R.string.longest_path_seed_display,longestPathSeed));
currentSeedDisplay.setText(getString(R.string.current_seed_display,currentSeed));
mHandler = new Handler();
startRepeatingTask();
}
@Override
public void onDestroy(){
super.onDestroy();
stopRepeatingTask();
}
public void startCrunching(View view){
String value = startingNumberDisplay.getText().toString();
currentSeed = new BigInteger(value);
workingSeed=currentSeed;
run=true;
while(run){
if(workingSeed.compareTo(BigInteger.ONE)==0){
if(currentPath>longestPath){
longestPath=currentPath;
longestPathSeed=currentSeed;
}
currentSeed= currentSeed.add(BigInteger.ONE);
workingSeed=currentSeed;
Log.d("end", "startCrunching: Finished "+(currentSeed.subtract(BigInteger.ONE))+" at "+currentPath+". Starting "+currentSeed);
currentPath=0;
updateScreen();
}
if (workingSeed.mod(new BigInteger("2")).compareTo(BigInteger.ZERO)==0){
workingSeed=workingSeed.divide(new BigInteger("2"));
}else{
workingSeed=(workingSeed.multiply(new BigInteger("3"))).add(BigInteger.ONE);
}
currentPath++;
}
}
public void updateScreen() {
//longestPathDisplay.setText(getString(R.string.longest_path_display, longestPath));
//longestPathSeedDisplay.setText(getString(R.string.longest_path_seed_display, longestPathSeed));
//currentSeedDisplay.setText(getString(R.string.current_seed_display, currentSeed));
longestPathDisplay.setText(getString(R.string.longest_path_display, temp));
longestPathSeedDisplay.setText(getString(R.string.longest_path_seed_display, temp));
currentSeedDisplay.setText(getString(R.string.current_seed_display, temp));
Log.d("update","requested screen update. Temp currently: "+temp);
temp++;
}
Runnable mStatusChecker = new Runnable() {
@Override
public void run() {
try{
updateScreen();
Log.d("repeat","Tried Updating Screen");
}finally {
mHandler.postDelayed(mStatusChecker,5000);
}
}
};
void startRepeatingTask(){
mStatusChecker.run();
}
void stopRepeatingTask() {
mHandler.removeCallbacks(mStatusChecker);
}
}
提前致谢! -Michael
答案 0 :(得分:0)
也许我错过了,但我不知道你实际上在哪里打电话给你的crunchnumber方法。
答案 1 :(得分:0)
startCrunching()
永远不会调用此方法。我想你需要改变你的执行顺序