gpt4 book ai didi

android - 该应用程序不断自动重启

转载 作者:行者123 更新时间:2023-11-29 01:32:47 24 4
gpt4 key购买 nike

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

主要 Activity :

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

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

}

}

}

编辑

    05-18 01:00:05.245: I/System.out(4962): The output of : doInBackground {"mac":"10:A5:D0:06:C6:E9","latitude":53.8689588,"longitude":10.66550319,"route":0}
05-18 01:00:05.255: I/System.out(4962): (HTTPLog)-Static: isSBSettingEnabled false
05-18 01:00:05.325: I/System.out(4962): KnoxVpnUidStorageknoxVpnSupported API value returned is false
05-18 01:00:05.986: I/System.out(4962): The output of the StringBulder before [7, 31]
05-18 01:00:05.986: I/System.out(4962): The output of the StringBulder: {"status":201,"routes":[7,31]}
05-18 01:00:05.996: I/Timeline(4962): Timeline: Activity_launch_request id:com.bustracker time:14086740
05-18 01:00:06.016: I/System.out(4962): The output of : doInBackground {"mac":"10:A5:D0:06:C6:E9","latitude":53.8689588,"longitude":10.66550319,"route":4}
05-18 01:00:06.016: I/System.out(4962): (HTTPLog)-Static: isSBSettingEnabled false
05-18 01:00:06.086: D/Activity(4962): performCreate Call secproduct feature valuefalse
05-18 01:00:06.086: D/Activity(4962): performCreate Call debug elastic valuetrue
05-18 01:00:06.256: I/Timeline(4962): Timeline: Activity_idle id: android.os.BinderProxy@c867c1 time:14087003
05-18 01:00:06.346: I/System.out(4962): The output of the StringBulder before [7, 31]
05-18 01:00:06.346: I/System.out(4962): The output of the StringBulder: {"status":201,"routes":[7,31]}
05-18 01:00:06.346: I/Timeline(4962): Timeline: Activity_launch_request id:com.bustracker time:14087094
05-18 01:00:06.346: I/System.out(4962): The output of : doInBackground {"mac":"10:A5:D0:06:C6:E9","latitude":53.8689588,"longitude":10.66550319,"route":4}
05-18 01:00:06.346: I/System.out(4962): (HTTPLog)-Static: isSBSettingEnabled false
05-18 01:00:06.407: D/Activity(4962): performCreate Call secproduct feature valuefalse
05-18 01:00:06.407: D/Activity(4962): performCreate Call debug elastic valuetrue
05-18 01:00:06.517: I/Timeline(4962): Timeline: Activity_idle id: android.os.BinderProxy@16efd0b5 time:14087265
05-18 01:00:06.587: I/System.out(4962): The output of the StringBulder before [7, 31]
05-18 01:00:06.587: I/System.out(4962): The output of the StringBulder: {"status":201,"routes":[7,31]}
05-18 01:00:06.587: I/Timeline(4962): Timeline: Activity_launch_request id:com.bustracker time:14087338
05-18 01:00:06.587: I/System.out(4962): The output of : doInBackground {"mac":"10:A5:D0:06:C6:E9","latitude":53.8689588,"longitude":10.66550319,"route":0}
05-18 01:00:06.587: I/System.out(4962): (HTTPLog)-Static: isSBSettingEnabled false
05-18 01:00:06.647: D/Activity(4962): performCreate Call secproduct feature valuefalse
05-18 01:00:06.647: D/Activity(4962): performCreate Call debug elastic valuetrue
05-18 01:00:06.757: I/System.out(4962): The output of the StringBulder before [7, 31]
05-18 01:00:06.757: I/System.out(4962): The output of the StringBulder: {"status":201,"routes":[7,31]}
05-18 01:00:06.757: I/System.out(4962): The output of : doInBackground {"mac":"10:A5:D0:06:C6:E9","latitude":53.8689588,"longitude":10.66550319,"route":0}
05-18 01:00:06.757: I/System.out(4962): (HTTPLog)-Static: isSBSettingEnabled false
05-18 01:00:06.767: I/Timeline(4962): Timeline: Activity_launch_request id:com.bustracker time:14087515
05-18 01:00:06.887: D/Activity(4962): performCreate Call secproduct feature valuefalse
05-18 01:00:06.887: D/Activity(4962): performCreate Call debug elastic valuetrue
05-18 01:00:06.967: I/Timeline(4962): Timeline: Activity_idle id: android.os.BinderProxy@109f7fdd time:14087719
05-18 01:00:06.977: I/Timeline(4962): Timeline: Activity_idle id: android.os.BinderProxy@c4a769 time:14087720
05-18 01:00:06.997: I/System.out(4962): The output of the StringBulder before [7, 31]
05-18 01:00:06.997: I/System.out(4962): The output of the StringBulder: {"status":201,"routes":[7,31]}
05-18 01:00:06.997: I/System.out(4962): The output of : doInBackground {"mac":"10:A5:D0:06:C6:E9","latitude":53.8689588,"longitude":10.66550319,"route":4}
05-18 01:00:06.997: I/System.out(4962): (HTTPLog)-Static: isSBSettingEnabled false
05-18 01:00:07.017: I/Timeline(4962): Timeline: Activity_launch_request id:com.bustracker time:14087762
05-18 01:00:07.077: D/Activity(4962): performCreate Call secproduct feature valuefalse
05-18 01:00:07.077: D/Activity(4962): performCreate Call debug elastic valuetrue
05-18 01:00:07.147: I/System.out(4962): The output of the StringBulder before [7, 31]
05-18 01:00:07.147: I/System.out(4962): The output of the StringBulder: {"status":201,"routes":[7,31]}
05-18 01:00:07.197: I/Timeline(4962): Timeline: Activity_launch_request id:com.bustracker time:14087944
05-18 01:00:07.297: D/Activity(4962): performCreate Call secproduct feature valuefalse
05-18 01:00:07.297: D/Activity(4962): performCreate Call debug elastic valuetrue
05-18 01:00:07.468: I/Timeline(4962): Timeline: Activity_idle id: android.os.BinderProxy@255edd7c time:14088211
05-18 01:00:07.468: I/Timeline(4962): Timeline: Activity_idle id: android.os.BinderProxy@2134ae11 time:14088211
05-18 01:00:07.828: V/ActivityThread(4962): updateVisibility : ActivityRecord{1ac84605 token=android.os.BinderProxy@37a1708c {com.bustracker/com.bustracker.MainActivity}} show : false
05-18 01:00:07.828: V/ActivityThread(4962): updateVisibility : ActivityRecord{1979865a token=android.os.BinderProxy@c867c1 {com.bustracker/com.bustracker.MainActivity}} show : false
05-18 01:00:07.828: V/ActivityThread(4962): updateVisibility : ActivityRecord{2349d08b token=android.os.BinderProxy@16efd0b5 {com.bustracker/com.bustracker.MainActivity}} show : false
05-18 01:00:07.828: V/ActivityThread(4962): updateVisibility : ActivityRecord{1a7a5568 token=android.os.BinderProxy@c4a769 {com.bustracker/com.bustracker.MainActivity}} show : false
05-18 01:00:07.828: V/ActivityThread(4962): updateVisibility : ActivityRecord{28c3d81 token=android.os.BinderProxy@109f7fdd {com.bustracker/com.bustracker.MainActivity}} show : false
05-18 01:00:07.828: V/ActivityThread(4962): updateVisibility : ActivityRecord{2339ae26 token=android.os.BinderProxy@2134ae11 {com.bustracker/com.bustracker.MainActivity}} show : false

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

最佳答案

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

你在这里也有变量不匹配:

Bundle extra = getIntent().getExtras();
if (extras != null) {

此外,您似乎没有为 LocationListener 覆盖一些必要的覆盖。

尝试使用 onNewIntent(),捕获在 AsyncTask 完成后进入的新 Intent,看看是否可行。参见 documentation here .

此外,将 android:launchMode="singleTop" 添加到 MainActivity 的 AndroidManifest.xml 中:

<activity
android:name=".MainActivity"
android:launchMode="singleTop"

编辑:从日志来看,您的后台线程似乎正在从过于频繁调用的 AsyncTask 中备份。

尝试给它多一点时间来执行。需要注意的一件事是,每次您从 onPostExecute() 调用 startActivity() 时,您的 MainActivity 将暂停并再次恢复。所以,看起来您只是经常这样做(根据日志,每秒多次)。

尝试以稍大的间隔注册您的位置更新,并给它一个小的最小距离,看看是否能解决问题:

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

}

}
}

关于android - 该应用程序不断自动重启,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/30292622/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com