我尝试使用排球从网址获取图片,但它无法正常工作 在加载数据应用程序正在崩溃...但当我删除图像加载器 正如文本数据一样正确加载evertything ... 我是android和编码的新手,所以无法弄清楚问题..
这是我的适配器类
public class RecyclerViewDataAdapter extends RecyclerView.Adapter<RecyclerViewDataAdapter.ViewHolder> {
private Pants context;
public ImageLoader mImageLoader;
//List of superHeroes
List<ListOfData> superHeroes;
public RecyclerViewDataAdapter(List<ListOfData> superHeroes, Pants context){
super();
//Getting all the superheroes
this.superHeroes = superHeroes;
this.context = context;
}
@Override
public ViewHolder onCreateViewHolder(ViewGroup parent, int viewType) {
View v = LayoutInflater.from(parent.getContext())
.inflate(R.layout.recyclerview_row, parent, false);
ViewHolder viewHolder = new ViewHolder(v);
return viewHolder;
}
@Override
public void onBindViewHolder(ViewHolder holder, int position) {
ListOfData superHero = superHeroes.get(position);
mImageLoader = MyApplication.getInstance(context).getImageLoader();
mImageLoader.get(superHero.getImageUrl(), ImageLoader.getImageListener(holder.imageView1, R.mipmap.ic_launcher, android.R.drawable.ic_dialog_alert));
holder.imageView1.setImageUrl(superHero.getImageUrl(), mImageLoader);
holder.textViewName1.setText(superHero.getName());
//holder.textViewRank.setText(String.valueOf(superHero.getRank()));
//holder.textViewRealName.setText(superHero.getRealName());
//holder.textViewCreatedBy.setText(superHero.getCreatedBy());
//holder.textViewFirstAppearance.setText(superHero.getFirstAppearance());
String powers = "";
/* for(int i = 0; i<superHero.getPowers().size(); i++){
powers+= superHero.getPowers().get(i);
}*/
//holder.textViewPowers.setText(powers);
}
@Override
public int getItemCount() {
return superHeroes.size();
}
class ViewHolder extends RecyclerView.ViewHolder{
public NetworkImageView imageView1;
public NetworkImageView imageView2;
public TextView textViewName1;
public TextView textViewName2;
public TextView textViewRank;
public TextView textViewRealName;
public TextView textViewCreatedBy;
public TextView textViewFirstAppearance;
public TextView textViewPowers;
public ViewHolder(View itemView) {
super(itemView);
imageView1 = (NetworkImageView) itemView.findViewById(R.id.thumbnail);
imageView2 = (NetworkImageView) itemView.findViewById(R.id.thumbnail2);
textViewName1 = (TextView) itemView.findViewById(R.id.category_title_one);
textViewName2 = (TextView) itemView.findViewById(R.id.category_title_two);
/* textViewRank= (TextView) itemView.findViewById(R.id.textViewRank);
textViewRealName= (TextView) itemView.findViewById(R.id.textViewRealName);
textViewCreatedBy= (TextView) itemView.findViewById(R.id.textViewCreatedBy);
textViewFirstAppearance= (TextView) itemView.findViewById(R.id.textViewFirstAppearance);
textViewPowers= (TextView) itemView.findViewById(R.id.textViewPowers);
*/
}
}
}
和JSON部分
public class Pants extends Fragment {
private Pants context;
//Creating a List of superheroes
public List<ListOfData> listSuperHeroes;
//Creating Views
private RecyclerView recyclerView;
private RecyclerView.LayoutManager layoutManager;
private RecyclerView.Adapter adapter;
public Pants() {
// Required empty public constructor
}
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
}
@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
// Inflate the layout for this fragment
View view = inflater.inflate(R.layout.tshirts_pants_shirts, container, false);
recyclerView = (RecyclerView) view.findViewById(R.id.recycler_view);
layoutManager = new LinearLayoutManager(getActivity().getApplicationContext());
recyclerView.setLayoutManager(layoutManager);
//Calling method to get data
getData();
return view;
}
private void getData() {
//Showing a progress dialog
final ProgressDialog loading = ProgressDialog.show(getActivity(), "Loading Data", "Please wait...", false, false);
//Creating a json array request
JsonArrayRequest jsonArrayRequest = new JsonArrayRequest(ProductConfig.DATA_URL,
new Response.Listener<JSONArray>() {
@Override
public void onResponse(JSONArray response) {
//Dismissing progress dialog
loading.dismiss();
//calling method to parse json array
parseData(response);
}
},
new Response.ErrorListener() {
@Override
public void onErrorResponse(VolleyError error) {
}
});
//Creating request queue
RequestQueue requestQueue = Volley.newRequestQueue(getActivity());
//Adding request to the queue
requestQueue.add(jsonArrayRequest);
}
//This method will parse json data
private void parseData(JSONArray array) {
listSuperHeroes = new ArrayList<>();
for (int i = 0; i < array.length(); i++) {
ListOfData superHero = new ListOfData();
JSONObject json = null;
try {
json = array.getJSONObject(i);
superHero.setImageUrl(json.getString(ProductConfig.TAG_IMAGE_URL));
superHero.setName(json.getString(ProductConfig.TAG_NAME));
//superHero.setRank(json.getInt(ProductConfig.TAG_RANK));
//superHero.setRealName(json.getString(ProductConfig.TAG_REAL_NAME));
//superHero.setCreatedBy(json.getString(ProductConfig.TAG_CREATED_BY));
//superHero.setFirstAppearance(json.getString(ProductConfig.TAG_FIRST_APPEARANCE));
ArrayList<String> powers = new ArrayList<String>();
JSONArray jsonArray = json.getJSONArray(ProductConfig.TAG_POWERS);
for (int j = 0; j < jsonArray.length(); j++) {
powers.add(((String) jsonArray.get(j)) + "\n");
}
superHero.setPowers(powers);
} catch (JSONException e) {
e.printStackTrace();
}
listSuperHeroes.add(superHero);
}
//Finally initializing our adapter
adapter = new RecyclerViewDataAdapter(listSuperHeroes, this);
//Adding adapter to recyclerview
recyclerView.setAdapter(adapter);
}
}
这里显示错误
Process: com.example.aninesoft.meltwishbeta, PID: 23012
java.lang.NullPointerException
at com.aninesoft.meltwishbeta.RecyclerViewDataAdapter.onBindViewHolder(RecyclerViewDataAdapter.java:45)
at com.aninesoft.meltwishbeta.RecyclerViewDataAdapter.onBindViewHolder(RecyclerViewDataAdapter.java:19)
at android.support.v7.widget.RecyclerView$Adapter.onBindViewHolder(RecyclerView.java:5277)
at android.support.v7.widget.RecyclerView$Adapter.bindViewHolder(RecyclerView.java:5310)
at android.support.v7.widget.RecyclerView$Recycler.getViewForPosition(RecyclerView.java:4568)
at android.support.v7.widget.RecyclerView$Recycler.getViewForPosition(RecyclerView.java:4461)
at android.support.v7.widget.LinearLayoutManager$LayoutState.next(LinearLayoutManager.java:1962)
at android.support.v7.widget.LinearLayoutManager.layoutChunk(LinearLayoutManager.java:1371)
at android.support.v7.widget.LinearLayoutManager.fill(LinearLayoutManager.java:1334)
at android.support.v7.widget.LinearLayoutManager.onLayoutChildren(LinearLayoutManager.java:563)
at android.support.v7.widget.RecyclerView.dispatchLayout(RecyclerView.java:2847)
at android.support.v7.widget.RecyclerView.onLayout(RecyclerView.java:3145)
at android.view.View.layout(View.java:14832)
at android.view.ViewGroup.layout(ViewGroup.java:4640)
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:14832)
at android.view.ViewGroup.layout(ViewGroup.java:4640)
at android.support.v4.view.ViewPager.onLayout(ViewPager.java:1627)
at android.view.View.layout(View.java:14832)
at android.view.ViewGroup.layout(ViewGroup.java:4640)
at android.support.design.widget.CoordinatorLayout.layoutChild(CoordinatorLayout.java:1034)
at android.support.design.widget.CoordinatorLayout.onLayoutChild(CoordinatorLayout.java:744)
at android.support.design.widget.ViewOffsetBehavior.onLayoutChild(ViewOffsetBehavior.java:42)
at android.support.design.widget.AppBarLayout$ScrollingViewBehavior.onLayoutChild(AppBarLayout.java:1180)
at android.support.design.widget.CoordinatorLayout.onLayout(CoordinatorLayout.java:757)
at android.view.View.layout(View.java:14832)
at android.view.ViewGroup.layout(ViewGroup.java:4640)
at android.widget.FrameLayout.layoutChildren(FrameLayout.java:453)
at android.widget.FrameLayout.onLayout(FrameLayout.java:388)
at android.view.View.layout(View.java:14832)
at android.view.ViewGroup.layout(ViewGroup.java:4640)
at android.support.v7.internal.widget.ActionBarOverlayLayout.onLayout(ActionBarOverlayLayout.java:437)
at android.view.View.layout(View.java:14832)
at android.view.ViewGroup.layout(ViewGroup.java:4640)
at android.widget.FrameLayout.layoutChildren(FrameLayout.java:453)
at android.widget.FrameLayout.onLayout(FrameLayout.java:388)
at android.view.View.layout(View.java:14832)
at android.view.ViewGroup.layout(ViewGroup.java:4640)
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:14832)
at android.view.ViewGroup.layout(ViewGroup.java:4640)
at android.widget.FrameLayout.layoutChildren(FrameLayout.java:453)
at android.widget.FrameLayout.onLayout(FrameLayout.java:388)
at android.view.View.layout(View.java:14832)
at android.view.ViewGroup.layout(ViewGroup.java:4640)
at android.view.ViewRootImpl.performLayout(ViewRootImpl.java:1996)
at android.view.ViewRootImpl.performTraversals(ViewRootImpl.java:1753)
at android.view.ViewRootImpl.doTraversal(ViewRootImpl.java:1004)
at android.view.ViewRootImpl$TraversalRunnable.run(ViewRootImpl.java:5739)
at android.view.Choreographer$CallbackRecord.run(Choreographer.java:761)
at android.view.Choreographer.doCallbacks(Choreographer.java:574)
at android.view.Choreographer.doFrame(Choreographer