Fragment中的RecyclerView错误

时间:2017-03-24 04:20:19

标签: java android json android-fragments android-recyclerview

我的Android应用程序出错,从本地主机提取json数据,并在主页碎片中使用RecycleView查看。我对此有点新意,我不确定自己做错了什么。请帮忙

HomeFragment.java

import android.app.ProgressDialog;
import android.os.AsyncTask;
import android.os.Bundle;
import android.support.design.widget.FloatingActionButton;
import android.support.v4.app.Fragment;
import android.support.v7.widget.LinearLayoutManager;
import android.support.v7.widget.RecyclerView;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ListView;
import android.widget.Toast;

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

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


/**
 * A simple {@link Fragment} subclass.
 */
public class HomeFragment extends Fragment {

    public static final int CONNECTION_TIMEOUT = 10000;
    public static final int READ_TIMEOUT = 15000;
    private RecyclerView mRVFishPrice;
    private AdapterFish mAdapter;

    ArrayList<listMovie> movieList;
    AdapterFish adapter;
    public HomeFragment() {
        // Required empty public constructor
    }


    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container,
                             Bundle savedInstanceState) {
        // Inflate the layout for this fragment
        final View rootView = inflater.inflate(R.layout.fragment_home, container, false);
        RecyclerView mRecyclerView = (RecyclerView) rootView.findViewById(R.id.fishPriceList);

        RecyclerView.LayoutManager mLayoutManager = new LinearLayoutManager(getActivity());
        mRecyclerView.setLayoutManager(mLayoutManager);
        new AsyncFetch().execute();
        return rootView;
    }

    private class AsyncFetch extends AsyncTask<String, String, String> {
        ProgressDialog pdLoading = new ProgressDialog(getActivity());
        HttpURLConnection conn;
        URL url = null;

        @Override
        protected void onPreExecute() {
            super.onPreExecute();

            pdLoading.setMessage("\tLoading...");
            pdLoading.setCancelable(false);
            pdLoading.show();
        }

        @Override
        protected String doInBackground(String... params) {
            try {
                url = new URL("http://100.74.245.14/finalproject/select_images.php");
            } catch (MalformedURLException e) {
                e.printStackTrace();
                return e.toString();
            }

            try {
                conn = (HttpURLConnection) url.openConnection();
                conn.setReadTimeout(READ_TIMEOUT);
                conn.setConnectTimeout(CONNECTION_TIMEOUT);
                conn.setRequestMethod("GET");

                conn.setDoOutput(true);
            } catch (IOException e1) {
                e1.printStackTrace();
            }

            try {
                int response_code = conn.getResponseCode();

                if(response_code == HttpURLConnection.HTTP_OK) {

                    InputStream input = conn.getInputStream();
                    BufferedReader reader = new BufferedReader(new InputStreamReader(input));

                    StringBuilder result = new StringBuilder();
                    String line;

                    while((line = reader.readLine()) != null) {
                        result.append(line);
                    }

                    return (result.toString());
                }
                else {
                    return ("Unsucessful");
                }
            } catch (IOException e) {
                e.printStackTrace();
                return e.toString();
            }finally {
                conn.disconnect();
            }

        }

        @Override
        protected void onPostExecute(String result) {

            pdLoading.dismiss();
            List<listMovie> data = new ArrayList<>();

            pdLoading.dismiss();

            try {
                JSONObject jsonObj = new JSONObject(result);
                JSONArray jArray = jsonObj.getJSONArray("test");
                //Log.d("json", result);
                for(int i = 0; i < jArray.length(); i++) {
                    Log.d("json", "test");
                    JSONObject json_data = jArray.getJSONObject(i);
                    listMovie movieData = new listMovie();
                    movieData.movieId = json_data.getString("movie_id");
                    movieData.movieName = json_data.getString("name");
                    movieData.movieImage = json_data.getString("imageurl");
                    data.add(movieData);
                }

                mRVFishPrice = (RecyclerView) getView().findViewById(R.id.fishPriceList);
                mAdapter = new AdapterFish(getActivity(), data);
                mRVFishPrice.setAdapter(mAdapter);
                mRVFishPrice.setLayoutManager(new LinearLayoutManager(getActivity()));
            } catch (JSONException e) {
                Toast.makeText(getActivity(), e.toString(), Toast.LENGTH_LONG).show();
            }
        }
    }

}

AdapterFish.java

import android.content.Context;
import android.support.v7.widget.RecyclerView;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ImageView;
import android.widget.TextView;

import com.bumptech.glide.Glide;

import java.util.Collections;
import java.util.List;


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

    private Context context;
    private LayoutInflater inflater;
    List<listMovie> data = Collections.emptyList();
    listMovie current;
    int currentPos = 0;

    public AdapterFish(Context context, List<listMovie> data) {
        this.context = context;
        inflater = LayoutInflater.from(context);
        this.data = data;
    }

    @Override
    public RecyclerView.ViewHolder onCreateViewHolder(ViewGroup parent, int viewType) {
        View view = inflater.inflate(R.layout.fragment_home, parent, false);
        MyHolder holder = new MyHolder(view);
        return holder;
    }

    @Override
    public void onBindViewHolder(RecyclerView.ViewHolder holder, int position) {

        MyHolder myHolder = (MyHolder) holder;

        listMovie current = data.get(position);
        Log.d("movie", current.movieName);
        myHolder.textMovieName.setText(current.movieName);
        myHolder.textMovieId.setText(current.movieId);

        Glide.with(context).load(current.movieImage)
                .placeholder(R.drawable.placeholder)
                .error(R.drawable.ic_menu_camera)
                .into(myHolder.ivMovie);
    }

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

    class MyHolder extends RecyclerView.ViewHolder {

        TextView textMovieName;
        TextView textMovieId;
        ImageView ivMovie;

        public MyHolder(View itemView) {
            super(itemView);
            Log.d("myholder", "test");
            textMovieName = (TextView) itemView.findViewById(R.id.textMovieName);
            textMovieId = (TextView) itemView.findViewById(R.id.textMovieId);
            ivMovie = (ImageView) itemView.findViewById(R.id.ivMovie);
        }
    }

}

listMovies.java

public class listMovie {

    public String movieId;
    public String movieName;
    public String movieImage;

    public String getMovieId() {
        return movieId;
    }

    public void setMovieId(String movieId) {
        this.movieId = movieId;
    }

    public String getMovieName() {
        return movieName;
    }

    public void setMovieName(String movieName) {
        this.movieName = movieName;
    }

    public String getMovieImage() {
        return movieImage;
    }

    public void setMovieImage(String movieImage) {
        this.movieImage = movieImage;
    }




}

fragment_home.xml

<FrameLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:tools="http://schemas.android.com/tools"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    tools:context="com.example.fyp.HomeFragment">

    <!-- TODO: Update blank fragment layout -->

    <android.support.v7.widget.RecyclerView
        android:id="@+id/fishPriceList"
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:paddingTop="10dp"
        android:layout_weight="1" />
</FrameLayout>

container_fish.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:clickable="true"
    android:padding="10dp" >

    <ImageView
        android:layout_width="60dp"
        android:layout_height="40dp"
        android:scaleType="fitXY"
        android:id="@+id/ivMovie"/>

    <TextView
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:textAppearance="?android:attr/textAppearanceMedium"
        android:text="fish name"
        android:id="@+id/textMovieName"
        android:layout_marginLeft="5dp"
        android:layout_toRightOf="@id/ivMovie"
        android:layout_toLeftOf="@+id/textMovieId"/>

    <TextView
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:textAppearance="?android:attr/textAppearanceSmall"
        android:text="price"
        android:id="@+id/textMovieId"
        android:textColor="@color/colorAccent"
        android:layout_alignParentEnd="true"
        android:layout_marginLeft="5dp"
        android:layout_marginRight="5dp" />

</LinearLayout>

JSON文件

{"test":[{"movie_id":"1","name":"Movie 1","imageurl":"http:\/\/10.0.2.2\/finalproject\/images\/AJ2-2.png"},{"movie_id":"2","name":"Movie 2","imageurl":"http:\/\/10.0.2.2\/finalproject\/images\/aj.png"},{"movie_id":"3","name":"Movie 3","imageurl":"http:\/\/10.0.2.2\/finalproject\/images\/gameover.png"},{"movie_id":"4","name":"Movie 4","imageurl":"http:\/\/10.0.2.2\/finalproject\/images\/tar.jpg"}]}

错误出现

E/AndroidRuntime: FATAL EXCEPTION: main
                  Process: com.example.arshadjailani.fyp, PID: 2582
                  java.lang.NullPointerException
                      at com.example.arshadjailani.fyp.AdapterFish.onBindViewHolder(AdapterFish.java:50)
                      at android.support.v7.widget.RecyclerView$Adapter.onBindViewHolder(RecyclerView.java:5825)
                      at android.support.v7.widget.RecyclerView$Adapter.bindViewHolder(RecyclerView.java:5858)
                      at android.support.v7.widget.RecyclerView$Recycler.getViewForPosition(RecyclerView.java:5094)
                      at android.support.v7.widget.RecyclerView$Recycler.getViewForPosition(RecyclerView.java:4970)
                      at android.support.v7.widget.LinearLayoutManager$LayoutState.next(LinearLayoutManager.java:2029)
                      at android.support.v7.widget.LinearLayoutManager.layoutChunk(LinearLayoutManager.java:1414)
                      at android.support.v7.widget.LinearLayoutManager.fill(LinearLayoutManager.java:1377)
                      at android.support.v7.widget.LinearLayoutManager.onLayoutChildren(LinearLayoutManager.java:578)
                      at android.support.v7.widget.RecyclerView.dispatchLayoutStep2(RecyclerView.java:3315)
                      at android.support.v7.widget.RecyclerView.dispatchLayout(RecyclerView.java:3124)
                      at android.support.v7.widget.RecyclerView.onLayout(RecyclerView.java:3568)
                      at android.view.View.layout(View.java:14817)
                      at android.view.ViewGroup.layout(ViewGroup.java:4631)
                      at android.widget.FrameLayout.layoutChildren(FrameLayout.java:453)
                      at android.widget.FrameLayout.onLayout(FrameLayout.java:388)
                      at android.view.View.layout(View.java:14817)
                      at android.view.ViewGroup.layout(ViewGroup.java:4631)
                      at android.widget.RelativeLayout.onLayout(RelativeLayout.java:1055)
                      at android.view.View.layout(View.java:14817)
                      at android.view.ViewGroup.layout(ViewGroup.java:4631)
                      at android.support.design.widget.HeaderScrollingViewBehavior.layoutChild(HeaderScrollingViewBehavior.java:131)
                      at android.support.design.widget.ViewOffsetBehavior.onLayoutChild(ViewOffsetBehavior.java:42)
                      at android.support.design.widget.AppBarLayout$ScrollingViewBehavior.onLayoutChild(AppBarLayout.java:1364)
                      at android.support.design.widget.CoordinatorLayout.onLayout(CoordinatorLayout.java:846)
                      at android.view.View.layout(View.java:14817)
                      at android.view.ViewGroup.layout(ViewGroup.java:4631)
                      at android.support.v4.widget.DrawerLayout.onLayout(DrawerLayout.java:1197)
                      at android.view.View.layout(View.java:14817)
                      at android.view.ViewGroup.layout(ViewGroup.java:4631)
                      at android.widget.FrameLayout.layoutChildren(FrameLayout.java:453)
                      at android.widget.FrameLayout.onLayout(FrameLayout.java:388)
                      at android.view.View.layout(View.java:14817)
                      at android.view.ViewGroup.layout(ViewGroup.java:4631)
                      at android.widget.LinearLayout.setChildFrame(LinearLayout.java:1671)
                      at android.widget.LinearLayout.layoutVertical(LinearLayout.java:1525)
                      at android.widget.LinearLayout.onLayout(LinearLayout.java:1434)
                      at android.view.View.layout(View.java:14817)
                      at android.view.ViewGroup.layout(ViewGroup.java:4631)
                      at android.widget.FrameLayout.layoutChildren(FrameLayout.java:453)
                      at android.widget.FrameLayout.onLayout(FrameLayout.java:388)
                      at android.view.View.layout(View.java:14817)
                      at android.view.ViewGroup.layout(ViewGroup.java:4631)
                      at android.widget.LinearLayout.setChildFrame(LinearLayout.java:1671)
                      at android.widget.LinearLayout.layoutVertical(LinearLayout.java:1525)
                      at android.widget.LinearLayout.onLayout(LinearLayout.java:1434)
                      at android.view.View.layout(View.java:14817)
                      at android.view.ViewGroup.layout(ViewGroup.java:4631)
                      at android.widget.FrameLayout.layoutChildren(FrameLayout.java:453)
                      at android.widget.FrameLayout.onLayout(FrameLayout.java:388)
                      at android.view.View.layout(View.java:14817)
                      at android.view.ViewGroup.layout(ViewGroup.java:4631)
                      at android.view.ViewRootImpl.performLayout(ViewRootImpl.java:1987)
                      at android.view.ViewRootImpl.performTraversals(ViewRootImpl.java:1744)
                      at android.view.ViewRootImpl.doTraversal(ViewRootImpl.java:1000)
                      at android.view.ViewRootImpl$TraversalRunnable.run(ViewRootImpl.java:5670)
                      at android.view.Choreographer$CallbackRecord.run(Choreographer.java:761)
                    at android.view.Choreographer.doCa

错误指向此行

myHolder.textMovieName.setText(current.movieName);
        myHolder.textMovieId.setText(current.movieId);

4 个答案:

答案 0 :(得分:1)

您在onCreateViewHolder

虚增错误的布局文件
View view = inflater.inflate(R.layout.fragment_home, parent, false);

应该是

View view = inflater.inflate(R.layout.container_fish, parent, false);

第二个错误!

public class HomeFragment extends Fragment {
...
RecyclerView mRecyclerView;
...

@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
                         Bundle savedInstanceState) {
mRecyclerView = (RecyclerView) rootView.findViewById(R.id.fishPriceList);
}
}
postExecute

中的

mAdapter = new AdapterFish(getActivity(), data);
mRecyclerView .setAdapter(mAdapter);
mRecyclerView .setLayoutManager(new LinearLayoutManager(getActivity()));

答案 1 :(得分:0)

在postexecute上的异步提取中,您已在适配器设置后设置了布局管理器,以便在适配器设置之前设置布局管理器

答案 2 :(得分:0)

将您的onBindViewHolder()更改为以下内容:

@Override public void onBindViewHolder(RecyclerView.ViewHolder holder, int position) { 
listMovie current = data.get(position);
Log.d("movie", current.movieName);
holder.textMovieName.setText(current.movieName);
holder.textMovieId.setText(current.movieId);
Glide.with(context).load(current.movieImage) .placeholder(R.drawable.placeholder) .error(R.drawable.ic_menu_camera) .into(holder.ivMovie); }

答案 3 :(得分:0)

我认为你在OnCreateViewHolder方法中指定了错误的布局。 将方法替换为:

@Override
public RecyclerView.ViewHolder onCreateViewHolder(ViewGroup parent, int viewType) {
    View view = inflater.inflate(R.layout.container_fish, parent, false);
    MyHolder holder = new MyHolder(view);
    return holder;
}