我使用Parse查询来检索Recycler视图中的数据。每当我启动我的应用程序时,它都会崩溃。我的代码是:
主要Activity.java
public class MainActivity extends AppCompatActivity {
RecyclerView mRecyclerView;
RecyclerView.LayoutManager mLayoutManager;
RecyclerView.Adapter mAdapter;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
mRecyclerView = (RecyclerView)findViewById(R.id.recycler_view);
mRecyclerView.setHasFixedSize(true);
mLayoutManager = new LinearLayoutManager(this);
mRecyclerView.setLayoutManager(mLayoutManager);
mAdapter=new CardAdapter(new ArrayList<>(), new CardAdapter.OnItemClickListener() {
@Override
public void onItemClick(View view, int position) {
switch(position){
case 0: //Opening an Activity
}
}
});
mRecyclerView.setAdapter(mAdapter);
}
@Override
public boolean onCreateOptionsMenu(Menu menu) {
// Inflate the menu; this adds items to the action bar if it is present.
getMenuInflater().inflate(R.menu.menu_main, menu);
return true;
}
@Override
public boolean onOptionsItemSelected(MenuItem item) {
// Handle action bar item clicks here. The action bar will
// automatically handle clicks on the Home/Up button, so long
// as you specify a parent activity in AndroidManifest.xml.
int id = item.getItemId();
//noinspection SimplifiableIfStatement
if (id == R.id.action_settings) {
return true;
}
return super.onOptionsItemSelected(item);
}
}
NatureItem.java
@ParseClassName("Nature")
public class NatureItem extends ParseObject {
private String mName;
private String mDes;
private int mThumbnail;
public String getName() {
return mName;
}
public void setName(String name) {
this.mName = name;
}
public String getDes() {
return mDes;
}
public void setDes(String des) {
this.mDes = des;
}
public int getThumbnail() {
return mThumbnail;
}
public void setThumbnail(int thumbnail) {
this.mThumbnail = thumbnail;
}
}
CardAdapter.java
public class CardAdapter extends RecyclerView.Adapter<CardAdapter.ViewHolder> {
List<NatureItem> mItems;
private OnItemClickListener mOnItemClickListener;
public interface OnItemClickListener {
public void onItemClick(View view, int position);
}
public CardAdapter(ArrayList<Object> itemsData, OnItemClickListener onItemClickListener) {
super();
mOnItemClickListener = onItemClickListener;
//this.mData = itemsData;
mItems = new ArrayList<NatureItem>();
NatureItem nature = new NatureItem();
ParseQuery<NatureItem> query = new ParseQuery<NatureItem>("Nature");
query.findInBackground(new FindCallback<NatureItem>() {
@Override
public void done(List<NatureItem> list, ParseException e) {
if(e!= null){
// Toast.makeText(CardAdapter.this, "Error " + e, Toast.LENGTH_SHORT).show();
}
for (NatureItem nature: list){
NatureItem newNatureItem=new NatureItem();
newNatureItem.setName(nature.getName());
newNatureItem.setDes(nature.getDes());
newNatureItem.setThumbnail(nature.getThumbnail());
mItems.add(newNatureItem);
}
}
});
}
@Override
public ViewHolder onCreateViewHolder(ViewGroup viewGroup, int i) {
View v = LayoutInflater.from(viewGroup.getContext())
.inflate(R.layout.recycler_view_card_item, viewGroup, false);
ViewHolder viewHolder = new ViewHolder(v);
return viewHolder;
}
@Override
public void onBindViewHolder(ViewHolder viewHolder, final int i) {
NatureItem nature = mItems.get(i);
viewHolder.tvNature.setText(nature.getName());
viewHolder.tvDesNature.setText(nature.getDes());
viewHolder.imgThumbnail.setImageResource(nature.getThumbnail());
viewHolder.container.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
mOnItemClickListener.onItemClick(v, i);
}
});
}
@Override
public int getItemCount() {
return mItems.size();
}
class ViewHolder extends RecyclerView.ViewHolder {
public ImageView imgThumbnail;
public TextView tvNature;
public TextView tvDesNature;
public View container;
public ViewHolder(View itemView) {
super(itemView);
container = itemView;
imgThumbnail = (ImageView)
itemView.findViewById(R.id.img_thumbnail);
tvNature = (TextView) itemView.findViewById(R.id.tv_nature);
tvDesNature = (TextView) itemView.findViewById(R.id.tv_des_nature);
}
}
}
Android Manifest
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
package="com.aj.card_view" >
<permission android:protectionLevel="signature"
android:name="com.aj.parse_implementation.permission.C2D_MESSAGE" />
<uses-permission android:name="com.aj.parse_implementation.permission.C2D_MESSAGE" />
<application
android:name=".NoteAppApplication"
android:allowBackup="true"
android:icon="@mipmap/ic_launcher"
android:label="@string/app_name"
android:theme="@style/AppTheme" >
<activity
android:name=".MainActivity"
android:label="@string/app_name" >
<intent-filter>
<action android:name="android.intent.action.MAIN" />
<category android:name="android.intent.category.LAUNCHER" />
</intent-filter>
</activity>
<activity
android:name=".splash"
android:label="@string/app_name" >
<intent-filter>
<action android:name="android.intent.action.MAIN" />
<category android:name="android.intent.category.DEFAULT" />
</intent-filter>
</activity>
</application>
XML文件
activity_main.xml中
<RelativeLayout 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" android:paddingLeft="@dimen/activity_horizontal_margin"
android:paddingRight="@dimen/activity_horizontal_margin"
android:paddingTop="@dimen/activity_vertical_margin"
android:paddingBottom="@dimen/activity_vertical_margin" tools:context=".MainActivity">
<android.support.v7.widget.RecyclerView
android:id="@+id/recycler_view"
android:layout_width="match_parent"
android:layout_height="match_parent"/>
</RelativeLayout>
recycler_view_card_item.xml
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:card_view="http://schemas.android.com/apk/res-auto"
android:orientation="vertical"
android:layout_width="match_parent"
android:layout_height="match_parent">
<android.support.v7.widget.CardView
android:layout_width="fill_parent"
android:layout_height="wrap_content"
card_view:cardCornerRadius="3dp"
android:layout_marginTop="9dp"
android:layout_marginLeft="9dp"
android:layout_marginRight="9dp"
card_view:cardElevation="0.01dp"
android:layout_marginBottom="0dp"
android:foreground="?android:attr/selectableItemBackground">
<RelativeLayout
android:id="@+id/top_layout"
android:layout_width="fill_parent"
android:layout_height="338dp">
<ImageView
android:id="@+id/img_thumbnail"
android:layout_width="fill_parent"
android:layout_height="250dp"
android:scaleType="fitXY"
android:layout_marginBottom="50dp"
android:background="#FFCA28"
/>
<TextView
android:id="@+id/tv_nature"
android:layout_width="fill_parent"
android:layout_height="50dp"
android:paddingLeft="@dimen/activity_horizontal_margin"
android:paddingRight="@dimen/activity_horizontal_margin"
android:layout_gravity="bottom"
android:gravity="center_vertical"
android:background="#5c1b1b1b"
android:alpha="0.8"
android:textColor="#fff"
android:textSize="26sp"
android:text="Test"
android:layout_alignBottom="@+id/img_thumbnail"
android:layout_alignParentLeft="true"
android:layout_alignParentStart="true" />
<TextView
android:id="@+id/tv_des_nature"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:paddingLeft="@dimen/activity_horizontal_margin"
android:paddingRight="@dimen/activity_horizontal_margin"
android:paddingTop="5dp"
android:paddingBottom="2dp"
android:alpha="0.8"
android:textColor="#ff272727"
android:textSize="15dp"
android:text="Test Test Test Test Test Test Test Test Test"
android:layout_below="@+id/tv_nature"
android:layout_alignParentLeft="true"
android:layout_alignParentStart="true"
android:layout_alignParentBottom="true"
android:background="#EEEEEE"/>
</RelativeLayout>
</android.support.v7.widget.CardView>
日志报告如下:
11-03 17:19:46.982 2227-2227/com.aj.card_view I/art﹕ Late-enabling -Xcheck:jni
11-03 17:19:47.033 2227-2227/com.aj.card_view W/System﹕ ClassLoader referenced unknown path: /data/app/com.aj.card_view-2/lib/arm
11-03 17:19:47.140 2227-2255/com.aj.card_view D/OpenGLRenderer﹕ Use EGL_SWAP_BEHAVIOR_PRESERVED: true
11-03 17:19:47.151 2227-2227/com.aj.card_view D/AndroidRuntime﹕ Shutting down VM
11-03 17:19:47.151 2227-2227/com.aj.card_view E/AndroidRuntime﹕ FATAL EXCEPTION: main
Process: com.aj.card_view, PID: 2227
java.lang.NullPointerException: Attempt to invoke interface method 'java.util.Iterator java.util.List.iterator()' on a null object reference
at com.aj.card_view.CardAdapter$1.done(CardAdapter.java:46)
at com.aj.card_view.CardAdapter$1.done(CardAdapter.java:40)
at com.parse.ParseTaskUtils$2$1.run(ParseTaskUtils.java:115)
at android.os.Handler.handleCallback(Handler.java:739)
at android.os.Handler.dispatchMessage(Handler.java:95)
at android.os.Looper.loop(Looper.java:148)
at android.app.ActivityThread.main(ActivityThread.java:5417)
at java.lang.reflect.Method.invoke(Native Method)
at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:726)
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:616)
NoteAppApplication.java
package com.aj.card_view;
import android.app.Application;
import com.parse.Parse;
import com.parse.ParseInstallation;
import com.parse.ParseObject;
/**
* Created by Anubhav on 11/3/2015.
*/
public class NoteAppApplication extends Application {
@Override
public void onCreate() {
super.onCreate();
Parse.enableLocalDatastore(this);
ParseObject.registerSubclass(NatureItem.class);
Parse.initialize(this, "key",
"key2");
ParseInstallation.getCurrentInstallation().saveInBackground();
}
}