2011-02-24 6 views
0

안드로이드에서 스레드와 핸들러를 사용하고 있습니다. 응용 프로그램은 내가 외부 활동에서 어떤 기능을하지 않는 한 잘 작동합니다. 하지만 스레드 내부에서 활동 외부에서 재미있는 부분을 호출하면 NullPointerException이 발생합니다.안쪽 스레드 또는 asyntask 함수를 호출 할 수 없습니다.

package com.prog; 

import com.API.TextBoxCheck; 

import android.app.Activity; 
import android.app.ProgressDialog; 
import android.os.Bundle; 
import android.os.Handler; 
import android.util.Log; 
import android.view.View; 
import android.view.View.OnClickListener; 
import android.widget.Button; 
import android.widget.EditText; 
import android.widget.TextView; 

public class ProgressBarExample extends Activity { 
    private Handler handler = new Handler(); 
    int i; 
    /** Called when the activity is first created. */ 
    @Override 
    public void onCreate(Bundle savedInstanceState) { 
     super.onCreate(savedInstanceState); 
     setContentView(R.layout.main); 


     Button btn=(Button)findViewById(R.id.button1); 
     btn.setOnClickListener(new OnClickListener(){ 
      TextView tv=(TextView)findViewById(R.id.textView1); 

      @Override 
      public void onClick(View v) { 
       // TODO Auto-generated method stub 

       Thread thread = new Thread(null, doBackgroundThreadProcessing, 
       "Background"); 
       thread.start(); 
      }}); 
     Button stopBtn=(Button)findViewById(R.id.button2); 
     stopBtn.setOnClickListener(new OnClickListener(){ 

      @Override 
      public void onClick(View v) { 
       // TODO Auto-generated method stub 
      finish(); 
      }}); 
    } 

    private Runnable doBackgroundThreadProcessing = new Runnable() { 
    public void run() { 
    try { 
     backgroundThreadProcessing(); 
    } catch (InterruptedException e) { 
     // TODO Auto-generated catch block 
     e.printStackTrace(); 
    } 
    } 
    }; 

    private void backgroundThreadProcessing() throws InterruptedException { 

     TextView tv=(TextView)findViewById(R.id.textView1); 

     i=0; 
     while(i<100) 
     { 
      handler.post(doUpdateGUI); 
     Thread.sleep(50); 

     i++;  
     } 
     EditText et=(EditText)findViewById(R.id.editText1); 

     TextBoxCheck tbc = new com.API.TextBoxCheck(); 
     String reply=tbc.TextBoxChecker(et.getText().toString(),10); 
     Log.d("thread", reply); 
    } 


    private Runnable doUpdateGUI = new Runnable() { 
    public void run() { 
    updateGUI(); 
    } 

    private void updateGUI() { 
     // TODO Auto-generated method stub 
     TextView tv=(TextView)findViewById(R.id.textView1); 
     tv.setText(i+"%"); 

    } 
    }; 
} 

여기 textBoxCheck becoz 코드를 생략했습니다. 여기서는 불필요하다고 생각합니다. 도와주세요.

추신. : AsyncTask를 사용하여 시도했지만 동일한 문제가 발생합니다.

답변

1

UI 스레드가 아닙니다. UI 항목을 조작하려면 UI 스레드에 있어야합니다. UI 스레드에서 핸들러를 만들고 backgroundThreadProcessing();을 호출하십시오. UI가 아닌 스레드가 아닌 핸들러에서 가져옵니다.

관련 문제