-2

I Can't use findViewById() from my Kclass=( How can i use it? `

private class BackGroundTask extends AsyncTask<Void, Void, Void>{



    @Override
    protected Void doInBackground(Void... params) {
        try {
            //execute - means sending
        response =httpClient.execute(request);
        HttpEntity entity=response.getEntity();
        String str=EntityUtils.toString(entity);
        Log.v("Json=", str);
        //adding new response to our Response Class
        resp.addNewResponse(str);
        } catch (ClientProtocolException e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        } catch (IOException e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        } catch (Exception e) {
            Log.e(TAG, "Exception caught: ", e);
        }
        return null;
    }

    protected void onPostExecute(Void result) {
        super.onPostExecute(result);
        httpClient.getConnectionManager().shutdown();
        TextView textView = (TextView) findViewById(R.id.textView1);

        textView.setText("Done");
    }


  }
}

please write an exact code. This class is in another .java file. Not in MainActivity.java

codeMagic
  • 44,549
  • 13
  • 77
  • 93
Raqim
  • 93
  • 1
  • 1
  • 6
  • Any `View` involving action within your `AsyncTask` is a bad idea, since an `AsyncTask` doesn't have anything to do with views and has different mechanisms on how to communicate with an `Activity`. Use a `Handler` or an `BroadcastReceiver` for that kind of communication. – nKn Feb 10 '14 at 19:28
  • @NKN Updating the UI from `onPostExecute` is fine. – Simon Feb 10 '14 at 19:30
  • @Simon true! But this is only usable once `doInBackground()` concluded, don't really know what kind of problem is the OP trying to solve... – nKn Feb 10 '14 at 19:31

3 Answers3

4

The easiest way would be to not use findViewById() at all since you are in a separate file and that is an Activity method.

You can simply use an interface to create a callback in onPostExecute() and send back the String result to your Activity. Then in your callback you can simply set the text as you wish.

This answer gives an example of using an interface with AsyncTask

Community
  • 1
  • 1
codeMagic
  • 44,549
  • 13
  • 77
  • 93
0

You may pass Activity context(YourActivity.this) to your AsyncTask and use activity.findViewById(...);

EDIT Some code:

Add to AsyncTask:

private Activity mActivity;

public YourAsyncTask(Activity activity){
this.mActivity = activity
}

... 
....

protected void onPostExecute(Void result) {
    super.onPostExecute(result);
    httpClient.getConnectionManager().shutdown();
    if(mActivity!=null){
        TextView textView = (TextView) mActivity.findViewById(R.id.textView1);
    }
    textView.setText("Done");
}
Sam
  • 1,652
  • 17
  • 25
  • If you don't understand - look for some Java lessons - you really need them) – Sam Feb 10 '14 at 19:48
  • but how can i call my AsyncTask(Activity activity) ??? I called it from another class like - BackGroundTask doItInBackGround = new BackGroundTask(); doItInBackGround.execute(); – Raqim Feb 11 '14 at 20:28
  • what param's i need write here? – Raqim Feb 11 '14 at 20:29
  • facepalm, http://bit.ly/1lxhnC1 You need to learn some OOP principles & basic Java. Passing a variable is kind of most basic thing – Sam Feb 11 '14 at 20:40
0
private Activity activity;

and in constructor:

public BackGroundTask(Activity activity){
    this.activity=activity;
}

and in post Execute:

 protected void onPostExecute(Void result) {
    super.onPostExecute(result);
    httpClient.getConnectionManager().shutdown();
    TextView textView = (TextView) activity.findViewById(R.id.textView1);

    textView.setText("Done");
}

If creating new background class from activity class call:

BackGroundTask doItInBackGround = new BackGroundTask(this);

If in a click listener or any class inside Activity call:

BackGroundTask doItInBackGround = new BackGroundTask(ActivityName.this);
vipul mittal
  • 17,343
  • 3
  • 41
  • 44
  • declare a property in async task of type activity and from the constructor set it and in post execute use it – vipul mittal Feb 10 '14 at 19:44
  • but how can i call my class if i add constructor? i called like BackGroundTask doItInBackGround = new BackGroundTask(); doItInBackGround.execute(); and how can i call it now? – Raqim Feb 11 '14 at 20:42