当我将recyclerview放入应用程序时,它在我运行时立即崩溃。 无论是在myAdapter类中还是在onCreate方法中调用它的方式,我都知道我做错了什么
任何人都可以指出我在代码中出错的地方
import android.content.Context;
import android.support.v7.widget.RecyclerView;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ImageView;
import android.widget.TextView;
import com.parse.GetCallback;
import com.parse.ParseFile;
import com.parse.ParseObject;
import com.parse.ParseQuery;
import java.text.ParseException;
import java.util.Collection;
import java.util.Collections;
import java.util.Date;
import java.util.List;
public class myAdapter extends RecyclerView.Adapter<myAdapter.ViewHolder> {
private LayoutInflater inflater;
List<Information> data = Collections.emptyList();
public static class ViewHolder extends RecyclerView.ViewHolder {
String t;
String p;
ParseFile i;
public ViewHolder(View itemView) {
super(itemView);
final ParseQuery<ParseObject> query = ParseQuery.getQuery("Ads");
query.getInBackground("xWMyZ4YEGZ", new GetCallback<ParseObject>() {
@Override
public void done(ParseObject parseObject, com.parse.ParseException e) {
if (e == null) {
// object will be your game score
t = parseObject.getString("title");
p = parseObject.getString("price");
i = parseObject.getParseFile("adPhoto");
} else {
// something went wrong
}
}
});
}
}
public myAdapter(Context context, List<Information> data) {
inflater = LayoutInflater.from(context);
this.data = data;
}
@Override
public myAdapter.ViewHolder onCreateViewHolder(ViewGroup parent,
int viewType) {
View view = inflater.inflate(R.layout.ads_listing, parent, false);
ViewHolder holder = new ViewHolder(view);
return holder;
}
@Override
public void onBindViewHolder(ViewHolder holder, int position) {
Information current = data.get(position);
holder.t = current.title;
holder.p = current.price;
}
@Override
public int getItemCount() {
return data.size();
}
}
这是在mainActivity中调用它的代码
recyclerView = (RecyclerView) findViewById(R.id.rec);
adapter = new myAdapter(getApplicationContext(), getData());
recyclerView.setAdapter(adapter);
recyclerView.setLayoutManager(new LinearLayoutManager(getApplicationContext()));
这是我的getData方法
public static List<Information> getData(){
final List<Information> data = new ArrayList<>();
ParseQuery<ParseObject> query = ParseQuery.getQuery("GameScore");
query.getInBackground("xWMyZ4YEGZ", new GetCallback<ParseObject>() {
@Override
public void done(ParseObject parseObject, com.parse.ParseException e) {
if (e == null) {
Information current = new Information();
current.title = parseObject.getString("title");
current.price = parseObject.getString("price");
current.image = parseObject.getParseFile("adPhoto");
data.add(current);
} else {
// something went wrong
}
}
});
return data;
}
这是logcat
04-24 12:52:35.227 5414-5414/com.example.cashmoney.barakah E/AndroidRuntime﹕ FATAL EXCEPTION: main
java.lang.RuntimeException: Unable to start activity ComponentInfo{com.example.cashmoney.barakah/com.example.cashmoney.barakah.MainActivity}: android.view.InflateException: Binary XML file line #28: Error inflating class RecyclerView
at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2225)
at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2286)
at android.app.ActivityThread.access$600(ActivityThread.java:144)
at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1259)
这是包含recyclerview
的xml文件 <LinearLayout
android:id="@+id/logo"
android:layout_width="match_parent"
android:layout_height="fill_parent"
android:weightSum="8"
android:orientation="vertical">
<RecyclerView
android:id="@+id/rec"
android:layout_width="match_parent"
android:layout_height="match_parent"></RecyclerView>
</LinearLayout>
请有人在这里指导我,我迷路了
答案 0 :(得分:1)
使用RecyclerView
在xml中以完整包名称查看:
<android.support.v7.widget.RecyclerView
android:id="@+id/rec"
android:layout_width="match_parent"
android:layout_height="match_parent"/>
答案 1 :(得分:0)
RecyclerView layout,
使用:
<android.support.v7.widget.RecyclerView
android:id="@+id/rec"
android:layout_width="match_parent"
android:layout_height="match_parent"
/>