Java Android-当我试图在文本框中显示某些内容时崩溃
-十、 Y和Z是指加速计的值 -该方法应该在单击按钮时开始 -计数器变量用于对特定移动进行计数Java Android-当我试图在文本框中显示某些内容时崩溃,java,android,multithreading,crash,Java,Android,Multithreading,Crash,-十、 Y和Z是指加速计的值 -该方法应该在单击按钮时开始 -计数器变量用于对特定移动进行计数 -我认为这可能是一个线程问题,但我不确定你不能从另一个线程访问UI元素。尝试像这样在UI线程中运行代码。用以下代码替换tv.setText(计数器) public void onClick_Start(View v) { //stores the weight value entered by user final EditText sWeight = (EditText) findV
-我认为这可能是一个线程问题,但我不确定你不能从另一个线程访问UI元素。尝试像这样在UI线程中运行代码。用以下代码替换
tv.setText(计数器)
public void onClick_Start(View v) {
//stores the weight value entered by user
final EditText sWeight = (EditText) findViewById(R.id.editTextWeight);
Log.v("EditText", sWeight.getText().toString());
String w = sWeight.getText().toString();
Weight_double = Double.parseDouble(w);
//sets to true because start was clicked
start = true;
counter = 0;
final int x1 = (int)lowX;
final int y1 = (int)lowY;
final int z1 = (int)lowZ;
new Thread(new Runnable() {
public void run() {
int x2;
int y2;
int z2;
int x3 = 10000;
while(start)
{
x2 = (int)lowX;
y2 = (int)lowY;
z2 = (int)lowZ;
if(x2 != x3)
{
if(x2 == x1&& y2 ==y1 && z2 == z1) {
counter++;
tv.setText(counter);
}
}
x3 = x2;
}
}
}).start();
}
从不同线程更新UI的另一种方法是使用处理程序访问主线程
YourActivity.this.runOnUiThread((new Runnable() {
public void run() {
tv.setText(counter);
}
});
你的事故记录在哪里?
final String counterText = String.valueOf(counter);
//Variable accessed from inner class must be declared final
Handler mHandler = new Handler(YourActivity.this.getMainLooper());
Runnable mRunnable = new Runnable() {
@Override
public void run() {
/* Update UI Elements here */
tv.setText(counterText);
}
};
mHandler.post(mRunnable);