回收使用json的数据时,Recycler视图中的问题

时间:2016-06-28 11:40:24

标签: java android json android-recyclerview

我的问题是当我使用json创建回收站视图但它显示异常任何人都可以解决这个问题,请帮助我解决这个问题,我感谢谁解决了这个错误?

public class MainActivity extends Activity {
Button b;
TextView tv;
ListView lists;
RecyclerView recyclerView;
List<Your> yourss;

@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_main);
    recyclerView = (RecyclerView) findViewById(R.id.recycle);
    LinearLayoutManager ll = new LinearLayoutManager(MainActivity.this);
    recyclerView.setHasFixedSize(true);
    recyclerView.setLayoutManager(ll);
    new JsonTask().execute("http://yoursubshop.com/webservices/categories.php");
}

public class JsonTask extends AsyncTask<String, String, List<Your>> {
    @Override
    protected List<Your> doInBackground(String... params) {
        HttpURLConnection httpURLConnection = null;
        BufferedReader bufferedReader = null;
        try {
            URL url = new URL(params[0]);
            httpURLConnection = (HttpURLConnection) url.openConnection();
            InputStreamReader inputStreamReader = new InputStreamReader(httpURLConnection.getInputStream());
            bufferedReader = new BufferedReader(inputStreamReader);
            StringBuffer sb = new StringBuffer();
            String line = "";
            while ((line = bufferedReader.readLine()) != null) {
                sb.append(line);
            }
            String finaljson = sb.toString();
            JSONObject firstobject = new JSONObject(finaljson);
            JSONArray firstarray = firstobject.getJSONArray("data");
                          List<Your> yo = new ArrayList<>();
            for (int i = 0; i < firstarray.length(); i++) {
                JSONObject finalobject = firstarray.getJSONObject(i);
                Your yos = new Your();
                yos.category_id = finalobject.getString("category_id");
                yos.category_name = finalobject.getString("category_name");
                yo.add(yos);
            }
            return yo;
        } catch (MalformedURLException e) {
            e.printStackTrace();
        } catch (IOException e) {
            e.printStackTrace();
        } catch (JSONException e) {
            e.printStackTrace();
        } finally {
            if (httpURLConnection != null) {
                httpURLConnection.disconnect();
            }
            try {
                if (bufferedReader != null) {

                    bufferedReader.close();
                }
            } catch (IOException e) {
                e.printStackTrace();
            }
        }
        return null;
    }

    @Override
    protected void onPostExecute(List<Your> result) {
        super.onPostExecute(result);
        Rvadapter listAdapter = new Rvadapter(MainActivity.this, yourss);
        recyclerView.setAdapter(listAdapter);
    }
}

public class Rvadapter extends RecyclerView.Adapter<Rvadapter.Myadapter> {
    List<Your> yours = Collections.emptyList();

    Context c;
    public class Myadapter extends RecyclerView.ViewHolder {
        TextView id;
        TextView name;
        CardView cv;
        public Myadapter(View itemView) {
            super(itemView);
            id = (TextView) itemView.findViewById(R.id.texts);
            name = (TextView) itemView.findViewById(R.id.textsa);
            cv = (CardView)itemView.findViewById(R.id.card);
        }
    }
    public Rvadapter(Context c, List<Your> yours) {
        this.yours = yours;
        this.c = c;


    }
    @Override
    public Myadapter onCreateViewHolder(ViewGroup parent, int viewType) {
        View v = LayoutInflater.from(parent.getContext()).inflate(R.layout.lists, parent, false);
        Myadapter m = new Myadapter(v);
        return m;
    }

    @Override
    public void onBindViewHolder(Myadapter holder, int position) {
        Your  y = yours.get(position);
        holder.id.setText(y.category_id);
        holder.name.setText(y.category_name);
    }

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

    @Override
    public void onAttachedToRecyclerView(RecyclerView recyclerView) {
        super.onAttachedToRecyclerView(recyclerView);
    }
}

程序中的Eception

06-28 17:03:34.078 802-802/com.example.anilkumar.json E/AndroidRuntime: FATAL EXCEPTION: main
java.lang.NullPointerException
    at com.example.anilkumar.json.MainActivity$Rvadapter.getItemCount(MainActivity.java:154)
    at android.support.v7.widget.RecyclerView.dispatchLayoutStep1(RecyclerView.java:3170)
    at android.support.v7.widget.RecyclerView.onMeasure(RecyclerView.java:2782)
    at android.view.View.measure(View.java:12723)
    at android.view.ViewGroup.measureChildWithMargins(ViewGroup.java:4698)
    at android.widget.LinearLayout.measureChildBeforeLayout(LinearLayout.java:1369)
    at android.widget.LinearLayout.measureVertical(LinearLayout.java:660)
    at android.widget.LinearLayout.onMeasure(LinearLayout.java:553)
    at android.view.View.measure(View.java:12723)
    at android.view.ViewGroup.measureChildWithMargins(ViewGroup.java:4698)
    at android.widget.FrameLayout.onMeasure(FrameLayout.java:293)
    at android.view.View.measure(View.java:12723)
    at android.view.ViewGroup.measureChildWithMargins(ViewGroup.java:4698)
    at android.widget.LinearLayout.measureChildBeforeLayout(LinearLayout.java:1369)
    at android.widget.LinearLayout.measureVertical(LinearLayout.java:660)
    at android.widget.LinearLayout.onMeasure(LinearLayout.java:553)
    at android.view.View.measure(View.java:12723)
    at android.view.ViewGroup.measureChildWithMargins(ViewGroup.java:4698)
    at android.widget.FrameLayout.onMeasure(FrameLayout.java:293)
    at com.android.internal.policy.impl.PhoneWindow$DecorView.onMeasure(PhoneWindow.java:2092)
    at android.view.View.measure(View.java:12723)
    at android.view.ViewRootImpl.performTraversals(ViewRootImpl.java:1064)
    at android.view.ViewRootImpl.handleMessage(ViewRootImpl.java:2442)
    at android.os.Handler.dispatchMessage(Handler.java:99)
    at android.os.Looper.loop(Looper.java:137)
    at android.app.ActivityThread.main(ActivityThread.java:4424)
    at java.lang.reflect.Method.invokeNative(Native Method)
    at java.lang.reflect.Method.invoke(Method.java:511)
    at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:784)
    at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:551)

lists.xml

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:orientation="vertical"
android:layout_width="match_parent"
android:layout_height="match_parent">
<android.support.v7.widget.CardView
    android:layout_width="300dp"
    android:layout_height="80dp"
    android:id="@+id/card">
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:id="@+id/texts"
android:text="id"
android:padding="20dp"
android:layout_margin="20sp"
android:textSize="20sp"/>
<TextView
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:id="@+id/textsa"
    android:layout_margin="20sp"
    android:text="Resturants"
    android:textSize="20sp"/>
</android.support.v7.widget.CardView>

2 个答案:

答案 0 :(得分:0)

问题是,在尝试获取计数时,您没有初始化数组。 尝试更改为。

@Override
public int getItemCount() {
    return yours == null ? 0 : yours.size();
}

答案 1 :(得分:0)

在你的onPostExecute中,首先改变

 @Override
protected void onPostExecute(List<Your> result) {
    super.onPostExecute(result);
    Rvadapter listAdapter = new Rvadapter(MainActivity.this, yourss);
    recyclerView.setAdapter(listAdapter);
}

@Override
protected void onPostExecute(List<Your> result) {
    super.onPostExecute(result);
    Rvadapter listAdapter = new Rvadapter(MainActivity.this, result);
    recyclerView.setAdapter(listAdapter);
}

现在检查它是否正常工作。

使用以下代码编辑lists.xml:

 <?xml version="1.0" encoding="utf-8"?>
 <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
     android:orientation="vertical"
     android:layout_width="match_parent"
     android:layout_height="wrap_content">
       <android.support.v7.widget.CardView
           android:layout_width="300dp"
           android:layout_height="80dp"
           android:id="@+id/card">
               <TextView
                   android:layout_width="wrap_content"
                   android:layout_height="wrap_content"
                   android:id="@+id/texts"
                   android:text="id"
                   android:padding="20dp"
                   android:layout_margin="20sp"
                   android:textSize="20sp"/>

                <TextView
                   android:layout_width="wrap_content"
                   android:layout_height="wrap_content"
                   android:id="@+id/textsa"
                   android:layout_margin="20sp"
                   android:text="Resturants"
                   android:textSize="20sp"/>
        </android.support.v7.widget.CardView>
  </LinearLayout>