开发者

cant call function from inside thread or asyntask

I am using a thread and a handler in android. The app works fine as long as i dont any function from outside the activity. But if i call some funcyion from outside the activity from inside a thread, it gives NullPointerException.

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(Vi开发者_开发知识库ew 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+"%");

    }
    };
}

I have left out the code of textBoxCheck becoz i think it may be unnecesarry here. Please help me on this.

PS. : I also tried using AsyncTask but the same problem occurs.


You are not on UI thread. You must be on UI thread to operate on any UI items. Create a handler on the UI thread and call your backgroundThreadProcessing(); from the handler and not from a non-UI thread.

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜