JP409
JP409

Reputation: 225

Cannot call View in Asynctask Android, java

I am trying to call the view from another class in my Asynctask class but it doesn't seem to be working.

Here is my AsyncTack

private class parseSite extends AsyncTask<String, Void, List<Integer>> {

    protected List<Integer> doInBackground(String... arg) {
     List<Integer> output = new ArrayList<Integer>();
        try {
            htmlHelper hh = new htmlHelper(new URL(arg[0]));
            output = hh.htmlHelper(arg[0]);
        } catch (Exception e) {
            System.out.println("Error");
        }
        return output;
    }

    protected void onPostExecute(List<Integer> exe) {

        graph salesView = new graph();
        View chartView = salesView.getView(this);
        chartView.setLayoutParams(new LinearLayout.LayoutParams(
                LinearLayout.LayoutParams.FILL_PARENT,
                LinearLayout.LayoutParams.FILL_PARENT, 1f));
        LinearLayout layout = (LinearLayout) findViewById(R.id.linearview);

        layout.addView(chartView, 0);

    }
}

And here is what the activity 'graph' looks like

public class graph {

public View getView(Context context) (...etc.)

I don't understand why it can't call the view.

Upvotes: 0

Views: 79

Answers (2)

wasyl
wasyl

Reputation: 3506

I think the problem is that you try to access view from other thread that created it. Usual way is to use runOnUiThread():

(activity).runOnUiThread(new Runnable() {
     public void run() {

 //call anything to your View objects

    }
});

Upvotes: 1

Alex DiCarlo
Alex DiCarlo

Reputation: 4891

Assuming you intended graph to extend Context in some way, such as by extending Activity, and that parseSite is defined inside graph, make the following change:

View chartView = salesView.getView(graph.this);

In the original code, this is referring to parseSite which does not extend Context (and does not refer to graph as you probably intend).

As a side note, in typical Java style classes should be named with an upper case letter, i.e. Graph not graph.

Upvotes: 1

Related Questions