刷新活动 / “执行未恢复的活动停止”错误

3

当GPS通过打开GPS设置而启用时,我希望能够刷新我的活动。我实现了onResume函数,但是结果在我的logcat上看到了这个:

performing stop of activity that is not resumed(...)

我查看了这个帖子,但我无法弄清楚我的代码(或我的onResume函数)有什么问题,并且如何在从GPS设置返回后刷新我的活动。
请问您能帮我吗?这是我的MainActivity.java

为什么你再次调用你的Activity...很明显onResume()会被调用两次,一次是在启动时即onCreate()之后,另一次是当你返回时。 - MOSO
@MOSO 我在onResume函数中更新了这一行代码:this.onCreate(null); 它重新加载了活动,但显然不建议这样做... - RidRoid
不建议这样做,也不是正确的编程方式。你的问题解决了吗?如果还没有解决,你需要发布包括showsettingsalert()方法在内的完整代码,然后我可以进一步提出建议。 - MOSO
你在服务类中定义了showsettingsalert(),但是你知道它没有任何UI,那么你在哪里接收结果呢?除非你将上下文转换为接收MainActivity。解决方案是将方法移动到MainActivity类中并进行进一步处理。我在下面编辑我的答案。 - MOSO
点赞可以是更好的感谢方式,这样其他人也可以发现有用。 :p :D - MOSO
显示剩余3条评论
3个回答

2
当调用您的

gps.showSettingsAlert();//inside this use startActivityForResult(new Intent().setClass(this, MainActivity.class), result_code);

编辑:你的方法应该在MainActivity.class中而不是在Service中,因为Service没有UI,所以在那里获取结果没有意义。

  public void showSettingsAlert(Context context){
            AlertDialog.Builder alertdialog = new AlertDialog.Builder(context);
            alertdialog.setTitle("Oups ! pas de données GPS");
            alertdialog.setMessage("GPS n'est pas activé sur votre appareil, voulez vous l'activer ?");

            /*
            handling the buttons :
             */
            alertdialog.setPositiveButton("Settings", new DialogInterface.OnClickListener() {


                public void onClick(DialogInterface dialog,int which) {
                    Intent intent = new Intent(Settings.ACTION_LOCATION_SOURCE_SETTINGS);
                    startActivityForResult(intent, RESULT_CODE_GPS);


                }
            });

            // on pressing cancel button
            alertdialog.setNegativeButton("Cancel", new DialogInterface.OnClickListener() {
                public void onClick(DialogInterface dialog, int which) {
                    dialog.cancel();
                }
            });

            alertdialog.show();

        }

以下是用户从设置页面返回后将调用的Activity结果方法。
@SuppressLint("NewApi") @Override
        protected void onActivityResult(int requestCode, int resultCode, Intent data){
            if(requestCode == RESULT_CODE_GPS && resultCode == 0){
              //  @SuppressWarnings("deprecation")
                String provider = Settings.Secure.getString(getContentResolver(), Settings.Secure.LOCATION_MODE);
                if(provider != null && !provider.isEmpty()){
                    Log.v("mAINaBBB", " Location providers: "+provider);
                    //Start searching for location and update the location text when update available. 
    // Do whatever you want

                }else{
                      Log.v(, "Nothing put on");
                    //Users did not switch on the GPS
                }
            }
        }

现在在MainActivity中调用之前调用过的方法。
if(gps.canGetlocation() ){

            double latitude = gps.getLatitude();
            double longitude = gps.getLongitude();

            device_location = new ParseGeoPoint(latitude,longitude);
            Toast.makeText(getApplicationContext(), "Your Location is - \nLat: " + latitude + "\nLong: " + longitude, Toast.LENGTH_LONG).show();

        }else{
            // can't get location
            // GPS or Network is not enabled
            // Ask user to enable GPS/network in settings

            showSettingsAlert(MainActivity.this); //**This line should be changed then**
        }

我按照您说的做了(在showsettingsalert()函数内使用startactivityforresult),但我收到了这个错误:无法解析方法'startactivityforresult(android.content.intent,int)'。 - RidRoid

0
在你的MainActivity.onResume()方法中,你试图启动一个新的MainActivity。
@Override
protected void onResume() {
    super.onResume();
    Intent refresh =new Intent(this, MainActivity.class);
    startActivity(refresh);
}

这只是一个糟糕的想法,会导致无限循环,其中MainActivity不断启动新的MainActivity。

只需删除您的onResume()并刷新当前活动的视图,而不是创建新的活动。


0
我遇到了一个错误 - 后来发现是因为我的代码错误 - 我在重新加载自身活动 - 这就像创建了一个无限循环。
通过将其从无限循环中删除,我解决了这个问题。

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接