-1

I have four classes:

  1. MainActivity extends AppCompactActivity
  2. MainAsyncTask extends AsyncTask<String, String, List>
  3. Intermediate extends MainAsyncTask and have two functions. (FuncA, FuncB)
  4. Leaf extends Intermediate and implementation of doInBackground() and onPostExecute().

When I run the application it prompts:

Unable to instantiate activity ComponentInfo{}: android.os.NetworkonMainThreadException.

How can I get rid off the Error. As far as My understanding is concerned, doInBackground() and onPostExecute()should be implemented in MainAsyncTask class?

Classes are : MianActivity.java

import android.os.Bundle;
import android.support.design.widget.FloatingActionButton;
import android.support.design.widget.Snackbar;
import android.support.v7.app.AppCompatActivity;
import android.support.v7.widget.Toolbar;
import android.view.View;
import android.view.Menu;
import android.view.MenuItem;
import android.widget.Button;

public class MainActivity extends AppCompatActivity {
    Leaf object = new Leaf();

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);

        Button fab = (Button) findViewById(R.id.fab);
        fab.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
               object.execute();
            }
        });
    }
}

MainAsyncTask.java

import android.os.AsyncTask;


public class MainAsync  extends AsyncTask<String, String, String> {

    @Override
    protected String doInBackground(String... params) {
        return null;
    }

    @Override
    protected void onPostExecute(String result) {

    }


    @Override
    protected void onPreExecute() {

    }

    @Override
    protected void onProgressUpdate(String... text) {


    }
}

Intermediate.java

import org.jsoup.Jsoup;
import org.jsoup.nodes.Document;

import java.io.IOException;

public class Intermediate extends MainAsync{

    public Document FunA(){
        System.out.println("Printed FunA()");
        String url = "http://blogs.tribune.com.pk/story/37034/zakir-naik-has-a-large-following-in-pakistan-should-we-be-alarmed/";
            Document doc = null;
            try {
                doc = Jsoup.connect(url).timeout(10 * 1000).get();
            } catch (IOException e) {
                e.printStackTrace();
        }
        return doc;
    }

    public void FunB(){ System.out.println("Printed FunB()");}
}
    }

Leaf.java

import org.jsoup.nodes.Document;
import org.jsoup.nodes.Element;
import org.jsoup.select.Elements;

public class Leaf extends Intermediate{

       Document HTM = FunA();

        public void FunC() {
            String heading = "";
            System.out.println("Printed FunC()");
            Elements seep = HTM.select("h1");
            for (Element foo : seep) {
                heading = foo.text();
                System.out.println(heading);
                break;
            }

        }

        public void FunD() {
            System.out.println("Printed FunD()");
        }

        public void FunE() {
            System.out.println("Printed FunE()");

        }
        @Override
        protected String doInBackground(String... params) {
            FunB();FunC();FunD();FunE();
            return null;
        }

        @Override
        protected void onPostExecute(String result) {

        }


        @Override
        protected void onPreExecute() {

        }

        @Override
        protected void onProgressUpdate(String... text) {


        }
    }

The purpose of doing in this way is to add FuncA and FuncB along with AsyncTask methods in one class that is Leaf class.

waqas
  • 143
  • 1
  • 4
  • 15
  • 1
    The error is saying you are attempting to run network operations on the main thread. You have not posted code so it is not possible to determine where your error lies. – Kuffs Aug 18 '16 at 09:39
  • It is impossible that your posted code throws this exception because there are no network operations. – Kuffs Aug 18 '16 at 09:59
  • @Kuffs It is throwing. Kindly run the code. – waqas Aug 18 '16 at 10:02
  • http://stackoverflow.com/questions/6343166/how-to-fix-android-os-networkonmainthreadexception – Kuffs Aug 18 '16 at 10:10
  • @Kuffs It did not helped me. Code is updated. Now see the code. – waqas Aug 18 '16 at 10:47
  • It would help you if you learned how to use an AsyncTask properly. – Kuffs Aug 18 '16 at 11:19

1 Answers1

0

In the absence of a stack trace I would say:

Initiating your Leaf class causes the HTM variable to be initiated.

The HTM variable is initiated by calling the FunA method.

The FunA method runs network accessing code and is not running inside DoInBackground (and therefore running on the main thread).

Your network code can only be run inside DoInBackground if you wish to not get a NetworkOnMainThread Exception.

The exception is thrown before you even start the AsyncTask as just the act of creating it causes the FunA code to run.

Move this line inside DoInBackground so it runs when the AsyncTask is executed and not when it is created.

Document HTM = FunA();

On a separate note, your class hierarchy is very convoluted. You do not need Intermediate or Leaf classes. All that code could be easily moved to the MainAsync class and so would be much easier to understand.

Kuffs
  • 35,581
  • 10
  • 79
  • 92