The Time
The Time

Reputation: 737

The App keeps restarting itself

I am trying to pass an intent from the doInBackground method to the onLocationchanged method in the inner LocationListent of the MainActivity. I am facing the problem the appp keeps restarting, then freezing and the device restart itself.

The routes varibale has these values [7,31].

The app works fine without intent (the added code which I commetted //Like This//)

I appreciate any help.

Error:

05-17 21:19:12.553: E/ActivityThread(19497): Performing stop of activity that is not resumed: {com.bustracker/com.bustracker.MainActivity}
05-17 21:19:12.553: E/ActivityThread(19497): java.lang.RuntimeException: Performing stop of activity that is not resumed: {com.bustracker/com.bustracker.MainActivity}
05-17 21:19:12.553: E/ActivityThread(19497):    at android.app.ActivityThread.performStopActivityInner(ActivityThread.java:3822)
05-17 21:19:12.553: E/ActivityThread(19497):    at android.app.ActivityThread.handleStopActivity(ActivityThread.java:3910)
05-17 21:19:12.553: E/ActivityThread(19497):    at android.app.ActivityThread.access$1200(ActivityThread.java:177)
05-17 21:19:12.553: E/ActivityThread(19497):    at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1477)
05-17 21:19:12.553: E/ActivityThread(19497):    at android.os.Handler.dispatchMessage(Handler.java:102)
05-17 21:19:12.553: E/ActivityThread(19497):    at android.os.Looper.loop(Looper.java:145)
05-17 21:19:12.553: E/ActivityThread(19497):    at android.app.ActivityThread.main(ActivityThread.java:5944)
05-17 21:19:12.553: E/ActivityThread(19497):    at java.lang.reflect.Method.invoke(Native Method)
05-17 21:19:12.553: E/ActivityThread(19497):    at java.lang.reflect.Method.invoke(Method.java:372)
05-17 21:19:12.553: E/ActivityThread(19497):    at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:1389)
05-17 21:19:12.553: E/ActivityThread(19497):    at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:1184)

MainActivity:

public class MainActivity extends ActionBarActivity {

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

        LocationManager lm = (LocationManager) getSystemService(Context.LOCATION_SERVICE);
       //I added "this" here//.
        LocationListener ll = new myLocationListener(this);
        lm.requestLocationUpdates(LocationManager.GPS_PROVIDER, 3000, 0, ll);

    }
      //Inner class in MainActivity
    class myLocationListener implements LocationListener {
        // I added here the bContext and the constructor//
        final Context bContext;

        public myLocationListener(Context context){
            bContext = context;
        }

        @Override
        public void onLocationChanged(Location location) {

                PostData sender = new PostData();
               // I added here the context  parameter.//
                sender.post_data(jSONString, bContext);
                  //I added here this part to receive the intent from onPostExecute //
                Bundle extra = getIntent().getExtras();
                if (extras != null) {
                    ArrayList<Integer> c = extras
                            .getIntegerArrayList("stop_route");
                    for (int item : c) {
                        System.out.println("The Intent is not empty: "
                                + item);
                    }
                }  
protected void onResume() {

    super.onResume();
}

@Override
protected void onPause() {
    super.onPause();
}             
        }

Some of the output in OnLocationChanged:

05-17 21:24:04.969: I/System.out(19497): The Intent is not empty: 7
05-17 21:24:04.969: I/System.out(19497): The Intent is not empty: 31

. . . I am getting over 30 lines in one output?

PostData class:

public class PostData {
    String jSONString;

    // Context mContext;

    public PostData() {
        super();

    }

    public String getjSONString() {
        return jSONString;

    }

    public void setjSONString(String jSONString) {
        this.jSONString = jSONString;
    }

    public void post_data(String jSONString, Context context) {
        this.jSONString = jSONString;

        new MyAsyncTask(context).execute(jSONString);

    }

    class MyAsyncTask extends AsyncTask<String, Integer, Void> {
        final Context mContext;
        ArrayList<Integer> routes = new ArrayList<Integer>();


        public MyAsyncTask(Context context) {
            mContext = context;
        }


        @Override
        protected Void doInBackground(String... params) {
            // TODO Auto-generated method stub
            BufferedReader reader = null;


            try {
                System.out.println("The output of : doInBackground "
                        + params[0]);

                URL myUrl = new URL(
                        "https://blabla.rhcloud.com/test");
                HttpURLConnection conn = (HttpURLConnection) myUrl
                        .openConnection();
                conn.setRequestMethod("POST");
                conn.setDoOutput(true);
                conn.setConnectTimeout(10000);
                conn.setReadTimeout(10000);
                conn.setRequestProperty("Content-Type", "application/json");
                conn.connect();

                // System.out.println("The output of getResponsecode: "
                // + conn.getResponseCode());
                // create data output stream
                DataOutputStream wr = new DataOutputStream(
                        conn.getOutputStream());
                // write to the output stream from the string
                wr.writeBytes(params[0]);

                wr.close();

                StringBuilder sb = new StringBuilder();
                reader = new BufferedReader(new InputStreamReader(
                        conn.getInputStream()));
                String line;

                while ((line = reader.readLine()) != null) {
                    sb.append(line + "\n");

                }

                Gson gson = new Gson();
                StopsJSON data = gson.fromJson(sb.toString(), StopsJSON.class);

             routes = data.getRoutes();


                System.out.println("The output of the StringBulder before "
                        + routes);
                System.out.println("The output of the StringBulder: "
                        + sb.toString());

            } catch (IOException e) {

                e.printStackTrace();
                return null;
            } finally {
                if (reader != null) {
                    try {
                        reader.close();
                        return null;
                    } catch (Exception e) {
                        // TODO Auto-generated catch block
                        e.printStackTrace();
                    }
                }
            }

            return null;

        }

        @Override
        protected void onPostExecute(Void result) {
            // Intent with Conetxt of the Asyntask class and
            Intent intent = new Intent(mContext, MainActivity.class);
            intent.putExtra("stop_route", routes);
            mContext.startActivity(intent);

        }

    }

}

Upvotes: 0

Views: 3127

Answers (1)

zafrani
zafrani

Reputation: 4101

I think the main problem is you're creating a cycle. Your MainActivity starts. And onCreate you instantiate your LocationManager which is probably firing off immediately, calling your AsyncTask which ends and starts your MainActivity again.

The rest is an assumtpion. If you're only allowing one instance of MainActivity to exist in your app then I'm assuming when you start MainActivity again, onStop of the currently existing MainActivity gets called before onResume gets called.

Upvotes: 1

Related Questions