2

I am new to this site. I have an error with my AsyncTask and my android program... this is the first async task that I have written.

The main Class called SocialApp.java

@Override
public void onCreate(Bundle savedInstanceState){
    super.onCreate(savedInstanceState);
    setContentView(R.layout.main);
    TextView t = (TextView) this.findViewById(R.id.textView1);       

    HTMLParser task = new HTMLParser();
    task.setTextView(t);
    task.execute();        
}

A basic AsyncTask

public class HTMLParser extends AsyncTask<Void, Void, Boolean> {

    TextView t;
    String title = "error";

    public void setTextView(TextView t){
        this.t = t;
    }

    @Override
    protected void onPreExecute() {
        t.setText(title);
    }

    @Override
    protected Boolean doInBackground(Void ... arg0) {
        t.setText(title);
    }

    @Override
    protected void onPostExecute(Boolean result) {
        t.setText(title);
    }
}

Resulting in this

12-15 12:34:11.808: E/AndroidRuntime(590):  at cv.mk.android.utapp.HTMLParser.onPreExecute(HTMLParser.java:24)
12-15 12:34:11.808: E/AndroidRuntime(590):  at android.os.AsyncTask.executeOnExecutor(AsyncTask.java:561)
12-15 12:34:11.808: E/AndroidRuntime(590):  at android.os.AsyncTask.execute(AsyncTask.java:511)
12-15 12:34:11.808: E/AndroidRuntime(590):  at cv.mk.android.utapp.SocialApp.onCreate(SocialApp.java:26)
12-15 12:34:11.808: E/AndroidRuntime(590):  at android.app.Activity.performCreate(Activity.java:4465)
12-15 12:34:11.808: E/AndroidRuntime(590):  at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1049)
12-15 12:34:11.808: E/AndroidRuntime(590):  at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:1919)
12-15 12:34:11.808: E/AndroidRuntime(590):  ... 11 more
12-15 12:34:19.666: D/dalvikvm(609): GC_FOR_ALLOC freed 33K, 3% free 9890K/10179K, paused 215ms
12-15 12:34:19.677: I/dalvikvm-heap(609): Grow heap (frag case) to 11.363MB for 1700016-byte allocation
12-15 12:34:19.747: D/dalvikvm(609): GC_CONCURRENT freed <1K, 3% free 11549K/11847K, paused 3ms+12ms
12-15 12:34:19.967: D/dalvikvm(609): GC_CONCURRENT freed 1K, 2% free 12079K/12231K, paused 2ms+3ms
12-15 12:34:20.037: D/dalvikvm(609): GC_FOR_ALLOC freed 1K, 2% free 12298K/12487K, paused 24ms
12-15 12:34:20.277: D/AndroidRuntime(609): Shutting down VM
12-15 12:34:20.277: W/dalvikvm(609): threadid=1: thread exiting with uncaught exception (group=0x409961f8)
12-15 12:34:20.277: D/dalvikvm(609): GC_CONCURRENT freed 1K, 1% free 13063K/13191K, paused 3ms+3ms
12-15 12:34:20.287: E/AndroidRuntime(609): FATAL EXCEPTION: main
12-15 12:34:20.287: E/AndroidRuntime(609): java.lang.RuntimeException: Unable to start activity ComponentInfo{cv.mk.android.utapp/cv.mk.android.utapp.SocialApp}: java.lang.NullPointerException
12-15 12:34:20.287: E/AndroidRuntime(609):  at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:1955)
12-15 12:34:20.287: E/AndroidRuntime(609):  at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:1980)
12-15 12:34:20.287: E/AndroidRuntime(609):  at android.app.ActivityThread.access$600(ActivityThread.java:122)
12-15 12:34:20.287: E/AndroidRuntime(609):  at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1146)
12-15 12:34:20.287: E/AndroidRuntime(609):  at android.os.Handler.dispatchMessage(Handler.java:99)
12-15 12:34:20.287: E/AndroidRuntime(609):  at android.os.Looper.loop(Looper.java:137)
12-15 12:34:20.287: E/AndroidRuntime(609):  at android.app.ActivityThread.main(ActivityThread.java:4340)
12-15 12:34:20.287: E/AndroidRuntime(609):  at java.lang.reflect.Method.invokeNative(Native Method)
12-15 12:34:20.287: E/AndroidRuntime(609):  at java.lang.reflect.Method.invoke(Method.java:511)
12-15 12:34:20.287: E/AndroidRuntime(609):  at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:784)
12-15 12:34:20.287: E/AndroidRuntime(609):  at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:551)
12-15 12:34:20.287: E/AndroidRuntime(609):  at dalvik.system.NativeStart.main(Native Method)
12-15 12:34:20.287: E/AndroidRuntime(609): Caused by: java.lang.NullPointerException
12-15 12:34:20.287: E/AndroidRuntime(609):  at cv.mk.android.utapp.HTMLParser.onPreExecute(HTMLParser.java:24)
12-15 12:34:20.287: E/AndroidRuntime(609):  at android.os.AsyncTask.executeOnExecutor(AsyncTask.java:561)
12-15 12:34:20.287: E/AndroidRuntime(609):  at android.os.AsyncTask.execute(AsyncTask.java:511)
12-15 12:34:20.287: E/AndroidRuntime(609):  at cv.mk.android.utapp.SocialApp.onCreate(SocialApp.java:26)
12-15 12:34:20.287: E/AndroidRuntime(609):  at android.app.Activity.performCreate(Activity.java:4465)
12-15 12:34:20.287: E/AndroidRuntime(609):  at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1049)
12-15 12:34:20.287: E/AndroidRuntime(609):  at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:1919)
12-15 12:34:20.287: E/AndroidRuntime(609):  ... 11 more

I have tried everything and I am really confused as to what is going on here...

Thank you for our time and effort.

BalusC
  • 1,082,665
  • 372
  • 3,610
  • 3,555
Mathew Kurian
  • 5,949
  • 5
  • 46
  • 73

2 Answers2

0

You can't call t.setText in the doInBackground. That method is run in a different thread so it cannot update the UI. Only things in the UI thread can do that. So, during processing if ou want to update the UI, you must call publishProgress, then the UI thread will call onProgressUpdate, there you can update the UI.

See this thread for a good explanation:

UpdateUI from AsyncTask

While this is still valid, I missed the NullPointer exception. t is probably not getting initialized as was stated above.

Community
  • 1
  • 1
Kaediil
  • 5,465
  • 2
  • 21
  • 20
0

Put the asynctask inside the activity, like this:

public class HTMLParser extends AsyncTask<Void, Void, Boolean> {

String title = "error";

public void setTextView(TextView t){
    ((TextView) SocialApp.this.findViewById(R.id.yourtextview)).setText(title);
}

@Override
protected void onPreExecute() {

    ((TextView) SocialApp.this.findViewById(R.id.yourtextview)).setText(title);

}

@Override
protected Boolean doInBackground(Void ... arg0) {
    ((TextView) SocialApp.this.findViewById(R.id.yourtextview)).setText(title);
}

@Override
protected void onPostExecute(Boolean result) {
    ((TextView) SocialApp.this.findViewById(R.id.yourtextview)).setText(title);


}
}
Noureddine AMRI
  • 2,942
  • 1
  • 21
  • 28