0

这应该是微不足道的,但我真的想知道发生了什么,因为我从在线课程中复制了代码,它在哪里工作,并且它给了我空指针异常。到目前为止一切似乎都是正确的

这是 MainActivity 类

public class MainActivity extends Activity {


Button button1;
TextView textview2;
ProgressBar progressbar;
@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);


    setContentView(R.layout.activity_main);

    TextView textview2=(TextView)findViewById(R.id.textview2);

    Button button1=(Button)findViewById(R.id.button1);



    ProgressBar progressbar1=(ProgressBar)findViewById(R.id.progressbar1);

        button1.setOnClickListener(new Button.OnClickListener() {


          public void onClick(View v) {



            asy task=new asy(MainActivity.this);
            task.execute("Process me","Process me too");





            }


        });
}

}

那么这就是来自 Async 的片段

public class asy extends AsyncTask<String,Integer,Long> {


public MainActivity host;

public asy (MainActivity host) {
    this.host=host;

}
@Override
protected void onPreExecute() {
    // TODO Auto-generated method stub



    super.onPreExecute();
  host.textview2.setText("Processing..");

;
}

在logcat中我看到了

E/AndroidRuntime(28044): FATAL EXCEPTION: main
E/AndroidRuntime(28044): java.lang.NullPointerException
E/AndroidRuntime(28044):    at com.example.async.asy.onPreExecute(asy.java:25)
E/AndroidRuntime(28044):    at android.os.AsyncTask.execute(AsyncTask.java:391)

我的意思是一切看起来对我来说都是正确的,我在 SetContentView 之后初始化 TextView,我使用构造函数,为什么它没有在 AsyncTask 中初始化?

4

4 回答 4

2
public class MainActivity extends Activity 
{
Button button1;
TextView textview2;
ProgressBar progressbar;
@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);


    setContentView(R.layout.activity_main);

    textview2=(TextView)findViewById(R.id.textView2);

    button1=(Button)findViewById(R.id.button1);



    progressbar=(ProgressBar)findViewById(R.id.progressbar1);

        button1.setOnClickListener(new Button.OnClickListener()
        {
          public void onClick(View v) 
          {
            asy task=new asy(MainActivity.this);
            task.execute("Process me","Process me too");
          }
        });
}

public class asy extends AsyncTask<String,Integer,Long> 
{

    public MainActivity host = new MainActivity();

    public asy (MainActivity host) 
    {
        this.host=host;
    }
    @Override
    protected void onPreExecute() 
    {
        super.onPreExecute();
        host.textview2.setText("Processing..");
    }
    @Override
    protected Long doInBackground(String... arg0) {
        // TODO Auto-generated method stub
        return null;
    }
}

}

于 2013-08-08T06:53:02.160 回答
0

假设host传递给任务的构造函数是MainActivity对象。然后在主要活动中,您没有分配TextView实例变量:

这些仍然是空的:

Button button1;
TextView textview2;
ProgressBar progressbar;

所以,这在onCreate()

TextView textview2=(TextView)findViewById(R.id.textview2);
Button button1=(Button)findViewById(R.id.button1);
ProgressBar progressbar1=(ProgressBar)findViewById(R.id.progressbar1);

应改为:

textview2=(TextView)findViewById(R.id.textview2);
button1=(Button)findViewById(R.id.button1);
progressbar=(ProgressBar)findViewById(R.id.progressbar1);
于 2013-08-08T06:13:48.287 回答
0

将您的 TextView 实例声明为全局并在 AsyncTask 中使用它。

喜欢..

TextView textview2;
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);


        setContentView(R.layout.activity_main);

        textview2=(TextView)findViewById(R.id.textview2);

并将其用作:

textview2.setText("Processing..");
于 2013-08-08T06:15:34.403 回答
0

从中删除 TextViewTextView textview2 = (TextView) findViewById(R.id.textview2);

你已经在上面声明了

于 2013-08-08T06:20:53.083 回答