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();
}
-X、Y 和 Z 指的是加速度计的值 - 该方法应该在单击按钮时启动 - 计数器变量用于对特定的移动进行计数 - 我认为这可能是一个线程问题,但我不确定
最佳答案
您无法从另一个线程访问 UI 元素。尝试像这样在 UI 线程中运行代码。将 tv.setText(counter)
替换为以下代码。
YourActivity.this.runOnUiThread((new Runnable() {
public void run() {
tv.setText(counter);
}
});
关于java - Android-当我尝试在文本框中显示某些内容时崩溃,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/32217972/