- 아래 내용 에러 코드 임 다시 확인 할것
class MainActivity
package org.androidtown.mythread2;
import android.os.Bundle;
import android.os.Handler;
import android.os.Looper;
import android.support.v7.app.AppCompatActivity;
import android.util.Log;
import android.view.View;
import android.widget.TextView;
public class MainActivity extends AppCompatActivity {
private static final String TAG ="MainActivity";
ProcessThread processThread;
TextView textView1, textView2;
int num;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
textView1=(TextView)findViewById(R.id.textView1);
textView2=(TextView)findViewById(R.id.textView2);
}
public void onButton1Click(View v){
processThread =new ProcessThread();
processThread.start();
processThread.handler.post(new Runnable() {
@Override
public void run() {
Log.i(TAG, "메인 스레드에서 새로운 스레드로 전달");
textView1.setText("메인 스레드에서 새로운 스레드로 전달");
}
});
}
class ProcessThread extends Thread{
Handler handler =new Handler();
public ProcessThread(){
}
@Override
public void run() {
Looper.prepare();
Looper.loop();
for(int i=0; i<100; i++){
Log.i(TAG, "스레드 동작중 : # " + i);
num=i;
runOnUiThread(new Runnable() {
@Override
public void run() {
textView2.setText(num);
}
});
try {
Thread.sleep(1000);
}catch (Exception e){}
}
}
}
}
R.layout.activity_main
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:id="@+id/activity_main"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:paddingBottom="@dimen/activity_vertical_margin"
android:paddingLeft="@dimen/activity_horizontal_margin"
android:paddingRight="@dimen/activity_horizontal_margin"
android:paddingTop="@dimen/activity_vertical_margin"
tools:context="org.androidtown.mythread2.MainActivity">
<Button
android:text="스레드"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentTop="true"
android:layout_centerHorizontal="true"
android:layout_marginTop="53dp"
android:id="@+id/button"
android:onClick="onButton1Click"
/>
<TextView
android:text="TextView"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_below="@+id/button"
android:layout_centerHorizontal="true"
android:layout_marginTop="56dp"
android:id="@+id/textView1"
android:textSize="18sp" />
<TextView
android:text="TextView"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_marginTop="104dp"
android:id="@+id/textView2"
android:textSize="18sp"
android:layout_below="@+id/textView1"
android:layout_alignParentLeft="true"
android:layout_alignParentStart="true" />
</RelativeLayout>
83강
84강
댓글 ( 4)
댓글 남기기