应用程序不断重启自己

时间:2015-05-17 19:45:13

标签: android

我试图将doInBackground方法中的intent传递给MainActivity的内部LocationListent中的onLocationchanged方法。我正面临appp不断重启的问题,然后冻结,设备重启。

路线varibale具有这些值[7,31]。

该应用程序没有意图正常工作(添加的代码我记得//喜欢这个//)

我感谢任何帮助。

错误:

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();
}             
        }

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

。   。   。我在一个输出中获得了超过30行?

PostData类:

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);

        }

    }

}

1 个答案:

答案 0 :(得分:1)

我认为主要的问题是你正在创造一个循环。 您的MainActivity开始了。并且onCreate你实例化你的LocationManager,它可能会立即启动,调用你的AsyncTask结束并再次启动你的MainActivity。

其余的是一个假设。 如果你只允许在你的应用程序中存在一个MainActivity实例,那么当你再次启动MainActivity时,我假设,在调用onResume之前调用当前存在的MainActivity的onStop。