我编写这个程序但是当我运行这个程序时,程序无法读取run()方法,因为在logCat中没有显示log.e()而在textView中没有显示消息。为什么呢?
MainActivity.java:
public class MainActivity extends Activity{
private TextView textModifiedThread;
private int threadModifiedInt=4;
private Point threadModifiedPoint=new Point(20,10);
boolean activeThread=true;
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
Log.e("CREATE", "READ");
textModifiedThread=(TextView)findViewById(R.id.textId);
Thread currentThread=new Thread(){
public void Run(){
Log.e("RUN", "READ");
try{
threadModifiedInt=20;
threadModifiedPoint.set(30, 40);
int timeCounter=100;
while(activeThread&&(timeCounter>0)){
try {
sleep(100);
} catch (InterruptedException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
if(timeCounter%10==0){
threadHandler.sendEmptyMessage((int)timeCounter/10);
Log.e("WHile", "Read");
}
timeCounter--;
}
}finally{
finish();
}
}
};
currentThread.start();
}
@Override
public boolean onTouchEvent(MotionEvent event){
if(event.getAction()==MotionEvent.ACTION_DOWN){
activeThread=false;
Log.e("TOUCH", "READ");
}
return super.onTouchEvent(event);
}
private Handler threadHandler=new Handler(){
public void handleMessage(android.os.Message msg){
textModifiedThread.setText("test int is "+threadModifiedInt+"\ntest point is "+threadModifiedPoint.toString()+"\ncounter is "+Integer.toString(msg.what));
Log.e("ERROR", "Read this");
}
};
}
activity_main.xml中:
<?xml version="1.0" encoding="UTF-8"?>
<LinearLayout
android:layout_height="fill_parent"
android:layout_width="fill_parent"
android:orientation="vertical"
xmlns:android="http://schemas.android.com/apk/res/android">
<TextView
android:layout_height="wrap_content"
android:layout_width="fill_parent"
android:id="@+id/textId"/>
</LinearLayout>
答案 0 :(得分:3)
因为你错误拼写了运行词:
public void Run(){
运行必须是小写的。所以正确的方法是:
public void run()
答案 1 :(得分:0)
请尝试使用Runnable Thread。
并且您的公共虚空Run()尝试将其更改为public void run()
。
这可能是它的原因。
Thread currentThread=new Thread(new Runnable){
public void run(){
}
}