android应用程序在控件到达recyclerview的findviewbyid()时崩溃?

vkc1a9a2  于 2021-06-30  发布在  Java
关注(0)|答案(4)|浏览(311)

我打算在后台从connectionclass获取数据,然后将其填充到recyclerview中。
现在,我已经成功地从url获取了json格式的数据,但是当我试图使用recyclerview显示数据时,我的应用程序崩溃了。
recyclerview的适配器类

package com.example.mainapp;

import android.content.Context;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.TextView;

import androidx.annotation.NonNull;
import androidx.recyclerview.widget.RecyclerView;

import java.util.List;

import Model.PersonDetails;

public class PersonDetailsAdapter extends RecyclerView.Adapter<PersonDetailsAdapter.ViewHolder> {

    private List<PersonDetails> personDetails;
    PersonDetailsAdapter(List<PersonDetails>details)
    {

        personDetails = details;
    }

    @NonNull
    @Override
    public PersonDetailsAdapter.ViewHolder onCreateViewHolder(@NonNull ViewGroup parent, int viewType) {
        Log.d("create view " , personDetails.toString());
        Context context = parent.getContext();
        LayoutInflater  inflater = LayoutInflater.from(context) ;
        View personView =  inflater.inflate(R.layout.person_details_view , parent , true) ;
        ViewHolder view = new ViewHolder(personView) ;
        return view;

    }

    @Override
    public void onBindViewHolder(@NonNull ViewHolder holder, int position) {
        PersonDetails details = personDetails.get(position);
        holder.nameTextView.setText(details.getName());
        holder.aliasTextView.setText(details.getAlias());

    }

    @Override
    public int getItemCount() {
        return personDetails.size();
    }

    public class ViewHolder extends RecyclerView.ViewHolder{
        public TextView nameTextView;
        public TextView aliasTextView;

        public ViewHolder(@NonNull View itemView) {
            super(itemView);
            nameTextView = (TextView) itemView.findViewById(R.id.nameHolder);
            aliasTextView = (TextView) itemView.findViewById(R.id.aliasHolder);
        }
    }

}

主活动.java

package com.example.mainapp;

import androidx.appcompat.app.AppCompatActivity;
import androidx.recyclerview.widget.GridLayoutManager;
import androidx.recyclerview.widget.LinearLayoutManager;
import androidx.recyclerview.widget.RecyclerView;

import android.content.Context;
import android.os.Bundle;
import android.util.Log;
import android.view.View;
import android.widget.Button;

import com.example.mainapp.Utility.ConnectionClass;

import org.json.JSONArray;
import org.json.JSONException;
import org.json.JSONObject;
import org.w3c.dom.Text;

import java.net.MalformedURLException;
import java.util.List;

import Model.PersonDetails;

public class MainActivity extends AppCompatActivity {

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        Button getButton  = findViewById(R.id.get);
        Button postButton  = findViewById(R.id.post);
        Context context = this.getBaseContext();
        getButton.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                ConnectionClass conn = null;
                try {

                    conn = new ConnectionClass("URL", context);
                    conn.execute();

                } catch (MalformedURLException e) {
                    System.out.println("connection not set ");
                    e.printStackTrace();
                }
            }
        });
        postButton.setOnClickListener(new View.OnClickListener() {
                                          @Override
                                          public void onClick(View v) {
                                            postRequest();
                                          }
                                      }
        );

    }
    public void updateView( List<PersonDetails> personDetails ) {
        Log.d("1" , "******************************************called fro");
        RecyclerView rView = (RecyclerView)findViewById(R.id.recyclerViewPerson);
        Log.d("2" , "******************************************iosdjdsa");
        PersonDetailsAdapter adapter = new PersonDetailsAdapter(personDetails);
        rView.setLayoutManager(new GridLayoutManager(this, 5));
        rView.setAdapter(adapter);
        adapter.notifyDataSetChanged();

    }

    private void postRequest() {
    }
}

连接类.java

package com.example.mainapp.Utility;

import android.content.Context;
import android.os.AsyncTask;
import android.util.Log;
import android.widget.Toast;

import com.example.mainapp.MainActivity;

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

import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;
import java.net.HttpURLConnection;
import java.net.MalformedURLException;
import java.net.ProtocolException;
import java.net.URL;
import java.util.ArrayList;
import java.util.List;

import Model.PersonDetails;
public class ConnectionClass extends AsyncTask<Void, Void, Void>  {
    private URL url ;
    private HttpURLConnection conn;
    private String  response ;
    private Context context ;

    public ConnectionClass() {
        super();
    }

    @Override
    protected void onPostExecute(Void aVoid) {
        super.onPostExecute(aVoid);
        Log.d("onPost", "*****************************************postexe");
        Toast.makeText(context, "Records fetched",Toast.LENGTH_SHORT);
        List<PersonDetails> details = new ArrayList<>();
        try {

            JSONArray arr = new JSONArray(response);
            for( int i = 0; i < arr.length() ; ++i )
            {
                JSONObject jObject = arr.getJSONObject(i);
                PersonDetails record = new PersonDetails(jObject.getString("name"),jObject.getString("alias") );
                details.add(record);

            }
        } catch (JSONException e) {
            Log.d("t","json exception");
            e.printStackTrace();
        }
        Log.d( "detalis", "**************************************"+details.toString());
        MainActivity activity = new MainActivity();
        activity.updateView(details);
    }

    public ConnectionClass(String url, Context context) throws MalformedURLException {
        this.url = new URL (url);
        conn = null ;
        this.context = context;
    }
//    public void sendRequest(){
//        try {
//            System.out.println("\n\n***************************************hello**********************\n\n");
//            conn = (HttpURLConnection) url.openConnection();
//            conn.setDoOutput(false);
//            conn.setDoInput(true);
//            conn.setUseCaches(false);
//            conn.setRequestMethod("GET");
//            conn.setRequestProperty("Content-Type", "application/json");
//            conn.connect();
//            // handle the response
//            System.out.println("\n\n***************************************here**********************\n\n");
//
//            int status = conn.getResponseCode();
//            System.out.println(status);
//
//            if (status != 200)
//                throw new IOException("Request not completed");
//            else
//            {
//                BufferedReader in = new BufferedReader(new InputStreamReader(conn.getInputStream()));
//                String inputLine ;
//                StringBuilder builder = new StringBuilder() ;
//                while((inputLine = in.readLine())  != null )
//                    builder.append(inputLine);
//                in.close();
//                response = builder.toString();
//            }
//
//        }
//        catch (IOException e) {
//            e.printStackTrace();
//        }
//        finally{
//            if( conn != null )
//                conn.disconnect();
//        }
//    }
    public void setUrl(String url) throws MalformedURLException {
        this.url = new URL(url);
    }
    public String getResponse() {
        return response;
    }

    @Override
    protected Void doInBackground(Void... voids) {

        try {
            conn = (HttpURLConnection) url.openConnection();
            Log.d("conn", "***************************************************************************connectioon set " ) ;
        } catch (IOException e) {
            e.printStackTrace();
        }
        try {
            conn.setRequestMethod("GET");
        } catch (ProtocolException e) {
            e.printStackTrace();
        }
        conn.setReadTimeout(10000 /* milliseconds */);
        conn.setConnectTimeout(15000 /* milliseconds */);

        conn.setDoOutput(true);

        try {
            conn.connect();
            Log.d("conn " , "**********************************************************************connected ");
        } catch (IOException e) {
            e.printStackTrace();
        }
        BufferedReader br = null;
        StringBuilder sb = new StringBuilder();
        try{
         br =new BufferedReader(new InputStreamReader(url.openStream()));
        char[] buffer = new char[1024];
        String line;
        while ((line = br.readLine()) != null) {
            sb.append(line+"\n");
        }
        Log.d("sb" , String.valueOf(sb));
        }
        catch (IOException e)
        {
            e.printStackTrace();
        }
        finally{
            Log.d("finally ", "*****************************************************************************************finally");
            response = sb.toString();
            System.out.println("JSON: " + response);
            conn.disconnect();
        }

        return null ;

    }
}

活动\u main.xml

<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout
    xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:app="http://schemas.android.com/apk/res-auto"
    xmlns:tools="http://schemas.android.com/tools"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    tools:context=".MainActivity">

        <RelativeLayout
            android:layout_width="match_parent"
            android:layout_height="200dp"
            android:layout_alignParentTop="true"
            android:layout_marginTop="-3dp">

            <TextView
                android:id="@+id/nameText"
                android:layout_width="141dp"
                android:layout_height="45dp"/>

            <TextView
                android:id="@+id/aliasText"
                android:layout_width="141dp"
                android:layout_height="45dp"
                android:layout_marginLeft="119dp"
                android:layout_toRightOf="@+id/nameText" />

            <Button
                android:id="@+id/post"
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:layout_alignParentLeft="true"
                android:layout_alignParentBottom="true"
                android:layout_marginLeft="-11dp"
                android:layout_marginBottom="2dp"
                android:text="POST" />

            <Button
                android:id="@+id/get"
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:layout_alignParentRight="true"
                android:layout_alignParentBottom="true"
                android:layout_marginRight="1dp"
                android:layout_marginBottom="3dp"
                android:text="GET" />

        </RelativeLayout>
            <androidx.recyclerview.widget.RecyclerView
                android:id="@+id/recyclerViewPerson"
                android:layout_width="match_parent"
                android:layout_height="wrap_content"
                android:background="#972A2A" />
</RelativeLayout>

用于recyclerview内容的xml

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:orientation="vertical"
    android:layout_margin="50dp"
    android:padding="10dp">

    <TextView
        android:id="@+id/nameHolder"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:padding="10dp"
        android:text="TextView" />

    <TextView
        android:id="@+id/aliasHolder"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:padding="10dp"
        android:text="TextView" />
</LinearLayout>

控件到达updateview()时应用程序正在崩溃?

tkclm6bt

tkclm6bt1#

让我澄清@sdex的答案:
创建的示例时 Activity -对你来说 MainActivity -它没有视图,用户看不到,也没有添加到活动堆栈中。你可以说-这个活动“死了”
以便将结果显示给当前运行的 MainActivity ,因此它将显示给用户,您需要将它的示例传递给 ConnectionClass 班级。例如,将其传递给connectionclass的构造函数。
顺便说一下-请考虑离开这里 AsyncTask ,这可能是 Context 泄漏,意味着-你将有大量的对象,不再可见的用户保留在内存中。
为了避免泄漏活动引用,请将其存储为“weakreference”

q8l4jmvw

q8l4jmvw2#

在mainactivity的底部添加asynctask代码,然后更新recyclerview,如下所示: updateView(details);

db2dz4w8

db2dz4w83#

MainActivity activity = new MainActivity(); 不能只使用构造函数创建示例。您应该传递mainactivity的引用以使其工作。您只需更改几行代码:
ConnectionClass 替换字段 private Context context;private MainActivity activity; 并相应改变构造器:

public ConnectionClass(String url, MainActivity activity) throws MalformedURLException {
    this.url = new URL (url);
    conn = null ;
    this.activity = activity;
}

之后,你就可以打电话了 activity.updateView(details); .
别忘了移除 MainActivity activity = new MainActivity(); 进一步阅读:
正确使用异步任务
异步任务否决和替代方案

2ul0zpep

2ul0zpep4#

MainActivity activity = new MainActivity();
    activity.updateView(details);

你不能像这样示例化活动;如果要在asynktask类的活动中调用方法 ConnectionClass ; 您可以将一个侦听器传递给它,并在活动中实现该侦听器,然后在需要调用活动中的某些功能时调用该侦听器的回调。

interface ConnectionClassListener {
    void onPostExecuteFinished(List<PersonDetails> personDetails);
}

将类传递给asynctask构造函数

public class ConnectionClass extends AsyncTask<Void, Void, Void>  {

    ConnectionClassListener mConnectionClassListener;
    public ConnectionClass(String url, Context context, ConnectionClassListener listener) throws MalformedURLException {
        this.url = new URL (url);
        conn = null ;
        this.context = context;
        mConnectionClassListener = listener;
    }

通过活动实现侦听器

public class MainActivity extends AppCompatActivity implements ConnectionClassListener {
    @Override
    void onPostExecuteFinished(List<PersonDetails> personDetails) {
            //  Do here what you want when the ConnectionClass calls `onPostExecuteFinished()`

            updateView(personDetails);
    }
}

添加 this 创建asynktask类时

conn = new ConnectionClass("URL", context, this);

最后打电话来 onPostExecuteFinished() 在你的 ConnectionClass ```
public class ConnectionClass extends AsyncTask<Void, Void, Void> {
// ...

@Override
protected void onPostExecute(Void aVoid) {
    super.onPostExecute(aVoid);

    //        ...
    mConnectionClassListener.onPostExecuteFinished(details);

相关问题