过去10天我一直试图解决这个问题我无法解决。我已经使用parse sdk为我的应用程序,我能够从解析服务器检索但没有它崩溃与此错误。
java.lang.NullPointerException: Attempt to invoke virtual method void android.support.v7.widget.RecyclerView.setLayoutManager(android.support.v7.widget.RecyclerView$LayoutManager) on a null object reference at com.example.sabhaysardana.coolDude.ExamWise.onCreateView(ExamWise.java:75)at android.support.v4.app.Fragment.performCreateView(Fragment.java:2087) at android.support.v4.app.FragmentManagerImpl.moveToState(FragmentManager.java:1113) at android.support.v4.app.FragmentManagerImpl.moveToState(FragmentManager.java:1295) at android.support.v4.app.BackStackRecord.run(BackStackRecord.java:801) at android.support.v4.app.FragmentManagerImpl.execSingleAction(FragmentManager.java:1643) at android.support.v4.app.BackStackRecord.commitNowAllowingStateLoss(BackStackRecord.java:679) at android.support.v4.app.FragmentPagerAdapter.finishUpdate(FragmentPagerAdapter.java:143) at android.support.v4.view.ViewPager.populate(ViewPager.java:1272) at android.support.v4.view.ViewPager.populate(ViewPager.java:1120) at android.support.v4.view.ViewPager.onMeasure(ViewPager.java:1646)
因此,考虑到应用程序能够从解析服务器打印我的值,但它没有显示在表(行)中,即recyclerView
(我的考试片段)
public class ExamWise extends Fragment implements RecyclerViewAdapter.ClickListener {
private RecyclerView recyclerView;
private RecyclerViewAdapter adapter;
private LinearLayoutManager layoutManager;
ArrayList<ItemsModel> data = new ArrayList<>();
ProgressDialog dialog;
public ExamWise() {
}
@Override
public void onActivityResult(int requestCode, int resultCode, Intent data) {
super.onActivityResult(requestCode, resultCode, data);
}
@Override
public void onViewCreated(View view, @Nullable Bundle savedInstanceState) {
super.onViewCreated(view, savedInstanceState);
}
@Nullable
@Override
public View onCreateView(LayoutInflater inflater, @Nullable ViewGroup container, @Nullable Bundle savedInstanceState) {
View rootView = inflater.inflate(R.layout.exam_row,container,false);
rootView = (RecyclerView)rootView.findViewById(R.id.my_recycler_view);
layoutManager = new LinearLayoutManager(getActivity());
layoutManager.setOrientation(LinearLayoutManager.VERTICAL);
recyclerView.setLayoutManager(layoutManager);
rootView.findViewById(R.id.my_recycler_view);
recyclerView.setOnScrollListener(new EndlessRecyclerView(layoutManager) {
@Override
public void onLoadMire(int current_page) {
int limit = current_page * 10;
LoadItems(limit);
}
});
return rootView;
}
@Override
public void onResume() {
super.onResume();
recyclerView.setOnScrollListener(new EndlessRecyclerView(layoutManager) {
@Override
public void onLoadMire(int current_page) {
int limit = current_page * 10;
LoadItems(limit);
}
});
loadData();
}
@Override
public void onSaveInstanceState(Bundle outState) {
super.onSaveInstanceState(outState);
}
@Override
public void OnItemClick(View view, int position) {
Intent i = new Intent(getActivity(), ActivityItemView.class);
ItemsModel model = data.get(position);
i.putExtra("examName",model.getTitle());
i.putExtra("examImage",model.getImageFile());
startActivity(i);
}
private void LoadItems(int limit) {
ParseQuery<ParseObject> query = ParseQuery.getQuery("examWise");
query.setLimit(limit);
dialog = new ProgressDialog(getActivity());
dialog.setTitle("Loading");
dialog.setMessage("Please Wait....");
dialog.show();
query.findInBackground(new FindCallback<ParseObject>() {
@Override
public void done(List<ParseObject> objects, ParseException e) {
dialog.dismiss();
if (e == null) {
for (int a = 0; a < objects.size(); a++) {
ParseObject object = objects.get(a);
ItemsModel model = new ItemsModel();
model.setImageFile(object.getParseFile("examImage").toString());
model.setTitle(object.getString("examName").toString());
data.add(model);
}
adapter.UpdateData(data);
} else if (e.getCode() == ParseException.CONNECTION_FAILED) {
Toast.makeText(getActivity(),"Error "+ e.getMessage(),Toast.LENGTH_LONG).show();
} else {
Toast.makeText(getActivity(),"No internet connection!" , Toast.LENGTH_LONG).show();
}
}
});
}
public void loadData() {
ParseQuery<ParseObject> query = ParseQuery.getQuery("examWise");
query.addAscendingOrder("date");
dialog = new ProgressDialog(getActivity());
dialog.setTitle("Loading");
dialog.setMessage("Please Wait....");
dialog.show();
query.findInBackground(new FindCallback<ParseObject>() {
@Override
public void done(List<ParseObject> objects, ParseException e) {
dialog.dismiss();
if (e == null) {
for (int a = 0; a < objects.size(); a++) {
ParseObject object = objects.get(a);
ItemsModel model = new ItemsModel();
model.setImageFile(object.getParseFile("examImage").toString()); model.setTitle(object.getString("examName").toString());
data.add(model);
}
adapter = new RecyclerViewAdapter(data, getActivity());
adapter.setClickListerner(ExamWise.this);
recyclerView.setAdapter(adapter);
} else if (e.getCode() == ParseException.CONNECTION_FAILED) {
Toast.makeText(getActivity(),"No internet connection!" , Toast.LENGTH_LONG).show();
} else {
Toast.makeText(getActivity(),"Error "+ e.getMessage(),Toast.LENGTH_LONG).show();
}
}
});
}
}
(我的RecyclerViewAdapter)
public class RecyclerViewAdapter extends RecyclerView.Adapter<RecyclerViewAdapter.ViewHolder> {
ClickListener clickListener;
ArrayList<ItemsModel> itemsArray = null;
Context context;
RecyclerViewAdapter(ArrayList<ItemsModel> itemsArray, Context context) {
this.itemsArray = itemsArray;
this.context = context;
}
public void UpdateData(ArrayList<ItemsModel> itemsArray){
this.itemsArray = itemsArray;
notifyDataSetChanged();
}
@Override
public RecyclerViewAdapter.ViewHolder onCreateViewHolder(ViewGroup viewGroup, int i) {
View view = LayoutInflater.from(viewGroup.getContext()).inflate(R.layout.exam_row,viewGroup,false);
return new ViewHolder(view);
}
@Override
public void onBindViewHolder(RecyclerViewAdapter.ViewHolder viewHolder, int i) {
viewHolder.Title.setText(itemsArray.get(i).getTitle());
Picasso.with(context).load(itemsArray.get(i).getImageFile()).into(viewHolder.imageView);
}
@Override
public int getItemCount() {
return itemsArray.size();
}
public void setClickListerner(ClickListener clickListerner) {
this.clickListener = clickListerner;
}
public class ViewHolder extends RecyclerView.ViewHolder implements View.OnClickListener{
public TextView Title;
public ImageView imageView;
public View view;
public ViewHolder(View itemView) {
super(itemView);
itemView.setOnClickListener(this);
this.view = itemView;
Title = (TextView) view.findViewById(R.id.homeTitle);
imageView = (ImageView) view.findViewById(R.id.homeImage);
}
@Override
public void onClick(View view) {
if(clickListener != null){
clickListener.OnItemClick(view, getPosition());
}
}
}
public interface ClickListener{
public void OnItemClick(View view, int position);
}
}
(我的ItemsModel)
public class ItemsModel implements Parcelable {
private String title,imageFile;
public String getTitle() {
return title;
}
public void setTitle(String title) {
this.title = title;
}
public String getImageFile() {
return imageFile;
}
public void setImageFile(String imageFile) {
this.imageFile = imageFile;
}
public ItemsModel() {
}
protected ItemsModel(Parcel in) {
String[] array = new String[2];
in.readStringArray(array);
title = array[0];
imageFile = array[1];
}
public static final Creator<ItemsModel> CREATOR = new Creator<ItemsModel>() {
@Override
public ItemsModel createFromParcel(Parcel in) {
return new ItemsModel(in);
}
@Override
public ItemsModel[] newArray(int size) {
return new ItemsModel[size];
}
};
@Override
public int describeContents() {
return 0;
}
@Override
public void writeToParcel(Parcel parcel, int i) {
parcel.writeStringArray(new String[] {this.title,this.imageFile});
}
}
(我的exam_row.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:layout_marginLeft="5dp"
android:layout_marginRight="5dp"
android:layout_marginTop="5dp"
android:layout_marginBottom="5dp"
android:background="@drawable/list_bg"
>
<com.parse.ParseImageView
android:id="@+id/homeImage"
android:layout_width="match_parent"
android:layout_height="200dp"
android:layout_margin="2dp"
android:src="@drawable/ic_launcher"
/>
<RelativeLayout
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignBaseline="@id/homeImage"
>
<TextView
android:layout_width="match_parent"
android:layout_height="30dp"
android:id="@+id/homeTitle"
android:gravity="start"
android:textAlignment="gravity"
android:paddingRight="5dp"
android:paddingLeft="3dp"
android:paddingBottom="5dp"
android:textSize="21sp"
android:text="abcdefghijklmnopqrstuvwxyz"
android:textColor="@color/colorBlack"
android:maxLines="2"
/>
</RelativeLayout>
(我的recycler_row)
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent" android:layout_height="match_parent">
<android.support.v7.widget.RecyclerView
android:id="@+id/my_recycler_view"
android:layout_height="match_parent"
android:layout_width="match_parent"
android:clickable="true"
android:background="@color/colorGrey" />
对不起社区我必须附上这么多的代码,但我的道德是逐渐下降的。这是我的第一个Android项目,我希望得到你的结果。
答案 0 :(得分:0)
您正在将RecyclerView
夸大为另一个变量
改变这个
rootView = (RecyclerView)rootView.findViewById(R.id.my_recycler_view);
到这个
recyclerView = (RecyclerView)rootView.findViewById(R.id.my_recycler_view);