Warning: file_get_contents(/data/phpspider/zhask/data//catemap/3/android/183.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Java RecyclerView活动在返回后被杀死_Java_Android_Json - Fatal编程技术网

Java RecyclerView活动在返回后被杀死

Java RecyclerView活动在返回后被杀死,java,android,json,Java,Android,Json,我放置了一个recyclerview,其中显示了一些包含文本和图像的卡片。这些卡片可以单击,并引导用户进入另一个活动以查看卡片内容。但当用户返回搜索活动时,recyclerview会删除这些项目。它显示找不到项,因为如果异步任务返回结果无效,则我已将文本“找不到项”放入 提前谢谢 我已包括以下代码: @Override public void onRestart() { super.onRestart(); if(!cd.isConnectingToInternet()){

我放置了一个recyclerview,其中显示了一些包含文本和图像的卡片。这些卡片可以单击,并引导用户进入另一个活动以查看卡片内容。但当用户返回搜索活动时,recyclerview会删除这些项目。它显示找不到项,因为如果异步任务返回结果无效,则我已将文本“找不到项”放入

提前谢谢

我已包括以下代码:

 @Override
public void onRestart() {
    super.onRestart();
    if(!cd.isConnectingToInternet()){
        alert.showAlertDialog(pgrentertabsearch.this,
                "No Internet Connection",
                "Please connect to Internet", false);
        // stop executing code by return
        return;

    }
    else {

        search(areavalue, pricevalue, numberofbedsvalue);
    }


}












private void search(final String searcharea, final String searchprice, final String searchnumberofbeds) {


    class PGAsync extends AsyncTask<String, Void, String> {

        private Dialog loadingDialog;

        @Override
        protected void onPreExecute() {
            super.onPreExecute();
            spinner.setVisibility(View.VISIBLE);
            loading.setVisibility(View.VISIBLE);
        }

        @Override
        protected String doInBackground(String... params) {
            String searcharea = params[0];
            String searchprice = params[1];
            String searchnumberofbeds = params[2];

            InputStream is = null;
            List<NameValuePair> nameValuePairs = new ArrayList<NameValuePair>();
            nameValuePairs.add(new BasicNameValuePair("searcharea", searcharea));
            nameValuePairs.add(new BasicNameValuePair("searchprice", searchprice));
            nameValuePairs.add(new BasicNameValuePair("searchnumberofbeds", searchnumberofbeds));
            String result = null;

            try {
                HttpClient httpClient = new DefaultHttpClient();
                HttpPost httpPost = new HttpPost(
                        "http://xxx.php");
                httpPost.setEntity(new UrlEncodedFormEntity(nameValuePairs));

                HttpResponse response = httpClient.execute(httpPost);

                HttpEntity entity = response.getEntity();

                is = entity.getContent();

                BufferedReader reader = new BufferedReader(new InputStreamReader(is, "UTF-8"), 8);
                StringBuilder sb = new StringBuilder();

                String line = null;
                while ((line = reader.readLine()) != null) {
                    sb.append(line + "\n");
                }
                result = sb.toString();
            } catch (ClientProtocolException e) {
                e.printStackTrace();
            } catch (UnsupportedEncodingException e) {
                e.printStackTrace();
            } catch (IOException e) {
                e.printStackTrace();
            }
            return result;
        }

        @Override
        protected void onPostExecute(String result) {
            String s = jsonresult.trim();
    spinner.setVisibility(View.GONE);
                loading.setVisibility(View.GONE);
            if (!s.equalsIgnoreCase("invalid")) {
                found.setVisibility(View.VISIBLE);
                myJSON = result;
                showList();


            } else {
                pgnotfound.setVisibility(View.VISIBLE);
                pgnotfound1.setVisibility(View.VISIBLE);
                pgnotfoundimage.setVisibility(View.VISIBLE);


            }

        }
    }

    PGAsync g = new PGAsync();
    g.execute(searcharea, searchprice, searchnumberofbeds);


}


protected void showList() {


    try {
        JSONObject jsonObj = new JSONObject(myJSON);

        pgarray = jsonObj.getJSONArray(TAG_RESULTS);
        for(int i=0;i<pgarray.length();i++){
            JSONObject c = pgarray.getJSONObject(i);
            PGCardItemReturn pgcarditemreturn = new PGCardItemReturn();
            pgcarditemreturn.setId(c.getString(TAG_ID));



            pgcarditemreturn.setFurnishing(c.getString(TAG_FURNISHING));


            pgcarditemreturn.setArea(c.getString(TAG_AREA));

            pgcarditemreturn.setPrice(c.getString(TAG_PRICE));

            pgcarditemreturn.setNumberofbeds(c.getString(TAG_NUMBEROFBEDS));


            pgcarditemreturn.setImageUrl(c.getString(TAG_IMAGE_URL));


            listPG.add(pgcarditemreturn);



        }


    } catch (JSONException e) {
        e.printStackTrace();

    }
    adapter = new PGSearchCardAdapter(listPG, this);

    //Adding adapter to recyclerview
    recyclerView.setAdapter(adapter);


}
她的名字叫logcat:

11-28 15:56:06.377 1894-2620/eya.eya D/skia: --- SkImageDecoder::Factory returned null
11-28 15:56:07.552 1894-2619/eya.eya D/skia: --- SkImageDecoder::Factory returned null
11-28 15:56:07.843 1894-2620/eya.eya D/skia: --- SkImageDecoder::Factory returned null
11-28 15:56:08.858 1894-1936/eya.eya W/EGL_emulation: eglSurfaceAttrib not implemented
11-28 15:56:08.858 1894-1936/eya.eya W/OpenGLRenderer: Failed to set EGL_SWAP_BEHAVIOR on surface 0xa3bb8020, error=EGL_SUCCESS
11-28 15:56:08.886 1894-1894/eya.eya E/RecyclerView: No adapter attached; skipping layout
11-28 15:56:08.898 1894-1894/eya.eya E/RecyclerView: No adapter attached; skipping layout
11-28 15:56:09.023 1894-1936/eya.eya D/OpenGLRenderer: endAllStagingAnimators on 0xa4349f00 (RippleDrawable) with handle 0xa3bffb50
11-28 15:56:09.233 1894-1894/eya.eya E/RecyclerView: No adapter attached; skipping layout
11-28 15:57:57.915 1894-1901/eya.eya W/art: Suspending all threads took: 5.356ms
11-28 16:02:08.480 1894-1901/eya.eya W/art: Suspending all threads took: 5.204ms
11-28 16:02:38.129 1894-1901/eya.eya W/art: Suspending all threads took: 5.316ms

如果我的理解是正确的,有两种活动:

活动A->RecyclerView显示搜索结果

活动B->每个recyclerview项目的详细信息


当您从活动B返回到活动A时,您的活动B已被销毁

因此,您需要在活动A中重写onSaveInstanceState(Bundle savedInstanceState)和onRestoreInstanceState(Bundle savedInstanceState),以便检索数据列表

例如:

@Override
public void onSaveInstanceState(Bundle savedInstanceState) {
super.onSaveInstanceState(outState);
savedInstanceState.putParcelable("key", myObject);
}

@Override
public void onRestoreInstanceState(Bundle savedInstanceState) {
super.onRestoreInstanceState(savedInstanceState);
Object myObject = savedInstanceState.getParcelable("key"); 
}
小心,您的对象类必须实现Parcelable

您可以查看以下文档:

当您在“类概述”上clic时,有一个典型的Parcelable实现

还有一个自动实现Parcelable的工具:


有两个活动:活动A-在recyclerview活动B中显示结果。活动B:显示从活动A中选择的特定卡的详细信息。当用户从B返回到活动A时,所有数据消失,活动显示:未找到任何项目。还有一件事,这件事总是发生在RAM设置为1GB的Android Emulator中。但是,当我在装有2GB内存的手机上试用时,效果很好。同样,如果我的手机在搜索时运行缓慢,问题仍然存在。在调用OnPostExecute时,我的列表中有数据。当我从B回到活动A时,数据丢失了。非常感谢你在这方面帮助我。但不幸的是,我不知道如何实现这一点。如果您能简单地向我介绍一下如何实现它,我将不胜感激。我的回答很有帮助吗?在这种情况下,请投她的票。
@Override
public void onSaveInstanceState(Bundle savedInstanceState) {
super.onSaveInstanceState(outState);
savedInstanceState.putParcelable("key", myObject);
}

@Override
public void onRestoreInstanceState(Bundle savedInstanceState) {
super.onRestoreInstanceState(savedInstanceState);
Object myObject = savedInstanceState.getParcelable("key"); 
}