簡體   English   中英

刷新活動/“執行未恢復活動的停止”錯誤

[英]Refreshing activity / “performing stop of activity that is not resumed” error

通過打開GPS設置的alertdialog啟用GPS后,我正在嘗試刷新我的活動。 我實現了onResume函數,但結果我在我的logcat上得到了這個:

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

我看過這篇文章,但我無法弄清楚我的代碼(或我的onResume函數)有什么問題,以及如何在從GPS設置返回后刷新我的活動。

你能幫我嗎 ? 這是我的MainActivity.java

打電話給你

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

編輯:您的方法應該在MainActivity.class中而不是在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();

        }

這是您的活動結果方法,將在用戶從“設置”頁面返回后調用。

@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
                }
            }
        }

現在,在MainAcitivity中,調用之前調用過的方法

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**
        }

我收到了這個錯誤 - 我發現這是由於我的代碼錯誤 - 我再次重新加載自我活動 - 這有點創造了一個無限循環。

我把它從無限循環中取出來擺脫它。

在MainActivity.onResume()中,您嘗試啟動新的MainActivity。

@Override
protected void onResume() {
    super.onResume();
    Intent refresh =new Intent(this, MainActivity.class);
    startActivity(refresh);
}

這只是一個壞主意,並且會給出一個無限循環,其中MainActivity不斷啟動新的MainActivity。

只需刪除onResume()並刷新當前活動的視圖,而不是創建新活動。

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM