2015-11-20 91 views
1

我的主要活動是出此錯誤行爲的執行停止:活動中環 - 未恢復

Performing stop of activity that is not resumed

這裏是我的logcat:

11-20 01:20:16.640 19746-19746/com.podchest.podchest D/LIFECYCLE =====>: CREATED 
11-20 01:20:16.740 19746-19746/com.podchest.podchest D/LIFECYCLE =====>: STARTED 
11-20 01:20:16.740 19746-19746/com.podchest.podchest D/LIFECYCLE =====>: RESUMED 
11-20 01:20:17.173 19746-19803/com.podchest.podchest D/JSON Parser: result: {"podcast":[{"nome":"Ludocast Brasil","link":"http:\/\/www.ludopedia.com.br\/podcast\/1\/ludocast\/feed","imagem":"http:\/\/www.ludopedia.com.br\/uploads\/01\/504\/s9mg0r.png","nome_cat":"Jogos","nome_autor":"Ludocast Brasil"}]} 
11-20 01:20:17.173 19746-19803/com.podchest.podchest D/JSON result: {"podcast":[{"nome":"Ludocast Brasil","link":"http:\/\/www.ludopedia.com.br\/podcast\/1\/ludocast\/feed","imagem":"http:\/\/www.ludopedia.com.br\/uploads\/01\/504\/s9mg0r.png","nome_cat":"Jogos","nome_autor":"Ludocast Brasil"}]} 
11-20 01:20:17.173 19746-19803/com.podchest.podchest D/QFOI: Ludocast Brasil 
11-20 01:20:17.173 19746-19803/com.podchest.podchest D/IMPOSSIVEL:: http://www.ludopedia.com.br/podcast/1/ludocast/feed 
11-20 01:20:17.211 19746-19746/com.podchest.podchest D/MAIN =====>: [email protected] 
11-20 01:20:17.242 19746-19746/com.podchest.podchest D/LIFECYCLE =====>: CREATED 
11-20 01:20:17.243 19746-19746/com.podchest.podchest D/LIFECYCLE =====>: STARTED 
11-20 01:20:17.244 19746-19746/com.podchest.podchest D/LIFECYCLE =====>: RESUMED 
11-20 01:20:17.385 19746-19746/com.podchest.podchest D/LIFECYCLE =====>: RESUMED 
11-20 01:20:17.664 19746-19746/com.podchest.podchest D/MAIN =====>: [email protected] 
11-20 01:20:17.700 19746-19746/com.podchest.podchest D/LIFECYCLE =====>: CREATED 
11-20 01:20:17.701 19746-19746/com.podchest.podchest D/LIFECYCLE =====>: STARTED 
11-20 01:20:17.701 19746-19746/com.podchest.podchest D/LIFECYCLE =====>: RESUMED 
11-20 01:20:17.709 19746-19746/com.podchest.podchest E/ActivityThread: Performing stop of activity that is not resumed: {com.podchest.podchest/com.podchest.podchest.MainActivity} 
11-20 01:20:17.709 19746-19746/com.podchest.podchest E/ActivityThread: java.lang.RuntimeException: Performing stop of activity that is not resumed: {com.podchest.podchest/com.podchest.podchest.MainActivity} 
11-20 01:20:17.709 19746-19746/com.podchest.podchest E/ActivityThread:  at android.app.ActivityThread.performStopActivityInner(ActivityThread.java:3396) 
11-20 01:20:17.709 19746-19746/com.podchest.podchest E/ActivityThread:  at android.app.ActivityThread.handleStopActivity(ActivityThread.java:3477) 
11-20 01:20:17.709 19746-19746/com.podchest.podchest E/ActivityThread:  at android.app.ActivityThread.access$1100(ActivityThread.java:148) 
11-20 01:20:17.709 19746-19746/com.podchest.podchest E/ActivityThread:  at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1321) 
11-20 01:20:17.709 19746-19746/com.podchest.podchest E/ActivityThread:  at android.os.Handler.dispatchMessage(Handler.java:102) 
11-20 01:20:17.709 19746-19746/com.podchest.podchest E/ActivityThread:  at android.os.Looper.loop(Looper.java:135) 
11-20 01:20:17.709 19746-19746/com.podchest.podchest E/ActivityThread:  at android.app.ActivityThread.main(ActivityThread.java:5312) 
11-20 01:20:17.709 19746-19746/com.podchest.podchest E/ActivityThread:  at java.lang.reflect.Method.invoke(Native Method) 
11-20 01:20:17.709 19746-19746/com.podchest.podchest E/ActivityThread:  at java.lang.reflect.Method.invoke(Method.java:372) 
11-20 01:20:17.709 19746-19746/com.podchest.podchest E/ActivityThread:  at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:901) 
11-20 01:20:17.709 19746-19746/com.podchest.podchest E/ActivityThread:  at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:696) 
11-20 01:20:17.710 19746-19746/com.podchest.podchest D/LIFECYCLE =====>: RESUMED 
11-20 01:20:18.014 19746-19746/com.podchest.podchest D/MAIN =====>: [email protected] 
11-20 01:20:18.053 19746-19746/com.podchest.podchest D/LIFECYCLE =====>: CREATED 
11-20 01:20:18.055 19746-19746/com.podchest.podchest D/LIFECYCLE =====>: STARTED 
11-20 01:20:18.055 19746-19746/com.podchest.podchest D/LIFECYCLE =====>: RESUMED 

MainActivity:

package com.podchest.podchest; 

import android.app.ProgressDialog; 
import android.os.AsyncTask; 
import android.support.v7.app.AppCompatActivity; 
import android.os.Bundle; 
import android.support.v7.widget.LinearLayoutManager; 
import android.support.v7.widget.RecyclerView; 
import android.util.Log; 

import org.json.JSONArray; 
import org.json.JSONObject; 

import java.util.ArrayList; 
import java.util.HashMap; 
import java.util.List; 

public class MainActivity extends AppCompatActivity { 

    private RecyclerView mRecyclerView; 
    private RecyclerView.Adapter mAdapter; 
    private RecyclerView.LayoutManager mLayoutManager; 
    ProgressDialog pDialog; 
    Helper meuhelper; 
    JsonParser jsonParser; 
    SessionManager sessionManager; 

    private static final String TAG_SUCCESS = "success"; 
    private static final String TAG_MESSAGE = "message"; 
    private static final String TAG_PODCASTS = "podcast"; 
    private static final String TAG_ID = "id"; 
    private static final String TAG_NOME = "nome"; 
    private static final String TAG_AUTOR = "nome_autor"; 
    private static final String TAG_CATEGORIA = "nome_cat"; 
    private static final String TAG_LINK = "link"; 
    //private static final String TAG_IMAGEM = "imagem"; 
    private static final String TAG_IMG = "imagem"; 
    private static String email = ""; 
    JSONArray podcasts = null; 

    private Podcasts pdc; 
    List<Podcasts> favoritos; 


    @Override 
    protected void onCreate(Bundle savedInstanceState) { 
     super.onCreate(savedInstanceState); 
     setContentView(R.layout.activity_main); 

     meuhelper = new Helper(); 
     jsonParser = new JsonParser(); 
     sessionManager = new SessionManager(getApplicationContext()); 
     //sessionManager.cleanOldSession(); 
     sessionManager.checkLogin(); 

     favoritos = new ArrayList<>(); 
     mRecyclerView = new RecyclerView(this); 

     //mLayoutManager = new LinearLayoutManager(this); 
     //mRecyclerView.setLayoutManager(mLayoutManager); 

     mRecyclerView = (RecyclerView) findViewById(R.id.my_recycler_view); 

     mRecyclerView.setHasFixedSize(true); 

     final LinearLayoutManager layoutManager = new LinearLayoutManager(this); 
     layoutManager.setOrientation(LinearLayoutManager.VERTICAL); 
     mRecyclerView.setLayoutManager(layoutManager); 

     MainAdapter adapter = new MainAdapter(favoritos); 
     mRecyclerView.setAdapter(adapter); 

     email = sessionManager.getEmail(); 
     Log.d("MAIN =====> ", email); 
     new getFavoritos().execute(email); 


     Log.d("LIFECYCLE =====>", "CREATED"); 
    } 

    /*@Override 
    protected void onStart() { 
     super.onStart(); 
     Log.d("LIFECYCLE =====>", "STARTED"); 
    }*/ 

    @Override 
    protected void onResume() { 
     super.onResume(); 
     Log.d("LIFECYCLE =====>", "RESUMED"); 
    } 

    private class getFavoritos extends AsyncTask<String, String, JSONObject>{ 

     @Override 
     protected void onPreExecute() { 
      super.onPreExecute(); 
      pDialog = new ProgressDialog(MainActivity.this); 
      pDialog.setMessage("Attempting login..."); 
      pDialog.setIndeterminate(false); 
      pDialog.setCancelable(true); 
      pDialog.show(); 
     } 

     @Override 
     protected JSONObject doInBackground(String... args) { 
      String url = meuhelper.ip + meuhelper.url_mylist; 

      try { 

       HashMap<String, String> params = new HashMap<>(); 
       params.put("email", args[0]); 

       JSONObject json = jsonParser.makeHttpRequest(
         url, "POST", params); 

       if (json != null) { 
        Log.d("JSON result", json.toString()); 
        try { 
         JSONObject jsonObj = new JSONObject(String.valueOf(json)); 
         podcasts = jsonObj.getJSONArray(TAG_PODCASTS); 

         for (int i = 0; i < podcasts.length(); i++) { 
          JSONObject c = podcasts.getJSONObject(i); 

          //String id = c.getString(TAG_ID); 
          String nome = c.getString(TAG_NOME); 
          String autor = c.getString(TAG_AUTOR); 
          String categoria = c.getString(TAG_CATEGORIA); 
          String link = c.getString(TAG_LINK); 
          String imagem = c.getString(TAG_IMG); 
          //String descricao = c.getString(TAG_DESCRICAO); 

          Log.d("QFOI", nome); 

          Log.d("IMPOSSIVEL: ", link); 

          pdc = new Podcasts(); 

          //pdc.setId(); 
          pdc.setNome(nome); 
          pdc.setAutor(autor); 
          pdc.setCategoria(categoria); 
          pdc.setLink(link); 
          pdc.setImagem(imagem); 

          favoritos.add(pdc); 
         } 
        } catch (Exception e){ 
         e.printStackTrace(); 
        } 
       } 
      } catch (Exception e) { 
       e.printStackTrace(); 
      } 
      return null; 
     } 

     @Override 
     protected void onPostExecute(JSONObject jsonObject) { 
      super.onPostExecute(jsonObject); 
      if (pDialog != null && pDialog.isShowing()) { 
       pDialog.dismiss(); 
      } 
     } 
    } 
} 

我有一個啓動畫面(活動),運行測試以查看用戶是否有會話。如果不是,則打開LoginActivity。

我認爲這可能是我的RecyclerView的問題。我無法解決它。我希望你能幫助我。謝謝。

編輯

SessionManager:

public void checkLogin(){ 
     // Check login status 
     if(!this.isLoggedIn()){ 
      // user is not logged in redirect him to Login Activity 
      Intent i = new Intent(_context, LoginActivity.class); 
      // Closing all the Activities 
      i.addFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP); 

      // Add new Flag to start new Activity 
      i.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK); 

      // Starting Login Activity 
      _context.startActivity(i); 
     } else { 
      Intent i = new Intent(_context, MainActivity.class); 
      i.addFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP); 

      i.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK); 

      _context.startActivity(i); 
     } 

    } 

public boolean isLoggedIn(){ 
     return pref.getBoolean(IS_LOGGED, false); 
    } 
} 
+0

要設置到適配器後,初始化的最愛嗎? – droidev

+0

是的,我是。聲明:'List favoritos;'Initialized:'favoritos = new ArrayList <>();'設置爲適配器:'MainAdapter adapter = new MainAdapter(favoritos);' – Alan

+0

但列表爲空,您正在填充數據AsyncTask對不對?它應該在 – droidev

回答

1
@Override 
protected void onCreate(Bundle savedInstanceState) { 
    super.onCreate(savedInstanceState); 
    setContentView(R.layout.activity_main); 
    new getFavoritos().execute(email); 
} 

private class getFavoritos extends AsyncTask<String, String,List<Podcasts>>{ 

    @Override 
    protected void onPreExecute() { 
     dialog.show(); 
    } 

    @Override 
    protected JSONObject doInBackground(String... args) { 
     //operations 
     return favoritos; 
    } 

    @Override 
    protected void onPostExecute(List<Podcasts> favoritos) { 
     if (dialog != null && dialog.isShowing()) { 
      dialog.dismiss(); 
     } 
     if(List<Podcasts>!=null){ 
     showFavoritosList(List<Podcasts>) 
     } 
    } 
} 

public void showFavoritosList(List<Podcasts>){ 
     final LinearLayoutManager layoutManager = new LinearLayoutManager(this); 
     layoutManager.setOrientation(LinearLayoutManager.VERTICAL); 
     mRecyclerView.setLayoutManager(layoutManager); 

     MainAdapter adapter = new MainAdapter(favoritos); 
     mRecyclerView.setAdapter(adapter); 
} 

試試這個,這可能工作。

UPDATE

mRecyclerView = new RecyclerView(this); 
mRecyclerView = (RecyclerView) findViewById(R.id.my_recycler_view); 

我看到了這行代碼,你不需要兩次初始化回收視圖。當您使用findViewById時,您將收到RecyclerView的對象,因此new RecyclerView(this);不是必需的,在此情況下無法使用。

更新2

正如我懷疑你又開始在這是一個錯誤的做法您checkLogin方法MainActivity,而不是你可以直接返回boolean值。

例如:

@Override 
protected void onCreate(Bundle savedInstanceState) { 
       boolean isSessionExist = sessionManager.isLoggedIn(); 
       if(isSessionExist){ 
       //open home screen 
       }else{ 
       //open login screen 
       } 
      } 

isLoggedIn

public boolean isLoggedIn(){ 
      return pref.getBoolean(IS_LOGGED, false); 
     } 

} 
+0

它不再顯示錯誤,但仍在循環中。它保持顯示啓動畫面。 – Alan

+0

你仍然在循環中意味着什麼? – droidev

+0

我已將日誌添加到我的代碼onStart,onCreate,onResume。所以,如果你看看我發佈的logcat,它永遠不會停止,應用程序仍在運行。 Logcat顯示它正在啓動,創建,不斷恢復。 – Alan