我的主要活动显示此错误:
停止未恢复的活动
这是我的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 =====>: administrador@podchest.com
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 =====>: administrador@podchest.com
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 =====>: administrador@podchest.com
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的一个问题。我无法解决它。我希望你能帮助我。 THX。
修改
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 :(得分: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);
}
试试这个,这可能有效。
<强>更新强>
mRecyclerView = new RecyclerView(this);
mRecyclerView = (RecyclerView) findViewById(R.id.my_recycler_view);
我在您的代码中看到了这一行,您不需要两次初始化回收站视图。当您使用findViewById
时,您将获得RecyclerView的对象,因此new RecyclerView(this);
不是必需的,在这种情况下无用。
更新2
我怀疑你再次在checkLogin方法中启动MainActivity,这是一种错误的方法,而不是你可以直接返回布尔值。
例如:
@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);
}
}