Android 应用程序会自动重新启动

Android 应用程序会自动重新启动,android,Android,在我开始将意图(来自服务器的响应)从doInBackground方法传递到MainActivity的内部LocationListent中的onLocationchanged方法之前,一切正常。我面临的问题是appp不断重启,然后冻结,设备自行重启 主要活动: public class MainActivity extends ActionBarActivity { @Override protected void onCreate(Bundle savedInstanceSta

在我开始将意图(来自服务器的响应)从doInBackground方法传递到MainActivity的内部LocationListent中的onLocationchanged方法之前,一切正常。我面临的问题是appp不断重启,然后冻结,设备自行重启

主要活动:

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

        }

    }

}

代码中存在一些问题,但我看到的主要问题是,在调用启动异步任务的
sender.post_data()
之后,您直接调用了
getExtras()
。记住,根据定义,AsyncTask是异步的,所以在使用其结果中的任何数据之前,必须等待它完成

您在这里也有变量未匹配:

Bundle extra = getIntent().getExtras();
         if (extras != null) {
另外,看起来您还没有覆盖LocationListener的一些必要覆盖

尝试使用
onNewIntent()
,以捕获在
AsyncTask
完成后出现的新意图,并查看其是否有效。看

另外,将android:launchMode=“singleTop”添加到MainActivity的AndroidManifest.xml中:

<activity
    android:name=".MainActivity"
    android:launchMode="singleTop"
完整代码:

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, 10000, 0.1F, ll);

    }

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

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

    @Override
    protected void onNewIntent(Intent intent) {
        super.onNewIntent(intent);
        // getIntent() should always return the most recent
        setIntent(intent);

        //I added here this part to receive the intent from onPostExecute //
        Bundle extras = 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);
            }
        }
    }

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

        @Override
        public void onStatusChanged(String provider, int status, Bundle extras) {

        }

        @Override
        public void onProviderEnabled(String provider) {

        }

        @Override
        public void onProviderDisabled(String provider) {

        }

    }
}
公共类MainActivity扩展了ActionBarActivity{
@凌驾
创建时受保护的void(Bundle savedInstanceState){
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
LocationManager lm=(LocationManager)getSystemService(Context.LOCATION\u服务);
//我在这里加了“this”。
LocationListener ll=新的myLocationListener(此);
lm.RequestLocationUpdate(LocationManager.GPS_提供程序,10000,0.1F,ll);
}
@凌驾
受保护的void onResume(){
super.onResume();
}
@凌驾
受保护的void onPause(){
super.onPause();
}
@凌驾
受保护的void onNewIntent(意图){
super.onNewIntent(意向);
//getIntent()应始终返回最新的
设定意图(intent);
//我在这里添加此部分是为了从onPostExecute接收意图//
Bundle extras=getIntent().getExtras();
如果(附加值!=null){
ArrayList c=附加值
.getIntegerArrayList(“停止路由”);
用于(int项目:c){
System.out.println(“目的不是空的:”
+项目);
}
}
}
//主活动中的内类
类myLocationListener实现LocationListener{
//我在这里添加了bContext和构造函数//
最后的语境;
公共myLocationListener(上下文){
b上下文=上下文;
}
@凌驾
已更改位置上的公共无效(位置){
PostData sender=新的PostData();
//我在这里添加了上下文参数//
sender.post_数据(jSONString、bContext);
}
@凌驾
public void onStatusChanged(字符串提供程序、int状态、Bundle extra){
}
@凌驾
公共无效onProviderEnabled(字符串提供程序){
}
@凌驾
公共无效onProviderDisabled(字符串提供程序){
}
}
}

奇怪,很难说是什么导致这种行为。你能在日志中查找崩溃点,并将它们添加到问题中,如果你能找到它们吗?@Daniel:我不知道如何才能找到崩溃点,因为Logcat中的输出太多了。但我在logcat中发现红色输出。我在问题中添加了完整的MainActivity代码?不,我的MainActivity大约有300行,因为我看到onLocationChanged()方法没有关闭,这是完整的代码吗?我添加了该方法,但应用程序仍在自行重新启动我添加了一些日志,也许你可以看到一些东西。以前的错误也出现在logcat中。我已经注册了以前更新过的位置,如
lm.requestLocationUpdates(LocationManager.GPS_PROVIDER,1000*30,ll)在我把问题放到网上后,我对它做了一点修改。我将其更改为
lm.requestLocationUpdates(LocationManager.GPS_PROVIDER,10000,0.1F,ll)什么都没有改变…同样的问题。这是一个奇怪的行为,因为我从服务器得到了很多作为响应的输出,而不是每10秒一次!Asynctask还会在一秒钟内多次向服务器发送请求。@MrAsker在您的代码中是否有调用
post_data()
的其他地方没有显示在问题中的代码中?没有,我只调用一次
PostData sender=new PostData();sender.post_数据(jSONString、bContext)
<activity
    android:name=".MainActivity"
    android:launchMode="singleTop"
lm.requestLocationUpdates(LocationManager.GPS_PROVIDER, 10000, 0.1F, ll);
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, 10000, 0.1F, ll);

    }

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

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

    @Override
    protected void onNewIntent(Intent intent) {
        super.onNewIntent(intent);
        // getIntent() should always return the most recent
        setIntent(intent);

        //I added here this part to receive the intent from onPostExecute //
        Bundle extras = 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);
            }
        }
    }

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

        @Override
        public void onStatusChanged(String provider, int status, Bundle extras) {

        }

        @Override
        public void onProviderEnabled(String provider) {

        }

        @Override
        public void onProviderDisabled(String provider) {

        }

    }
}