我目前正在努力实施fetching
我在class
创建的数据parse.com
。我想要检索一个名为" flyerDataFetch "的对象。及其所有元素都显示在Recyclerview
。
当我运行并编译程序时,我收到错误:
error:java.lang.NullPointerException at android.view.LayoutInflater.from(LayoutInflater.java:211)
Error: RecyclerView﹕ No adapter attached; skipping layout.
在尝试填充RecyclerView
时,您能否查看我的代码并帮助确定我出错的地方?
MainActivity
:
public class HomeScreen extends AppCompatActivity {
private RecyclerView mRecyclerDrawer;
private customAdapter mAdapter;
private Context context;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_home_screen);
setupDrawer();
final List<Information> data = new ArrayList<>();
Parse.initialize(this, "xxxxxxxxxxxxxxxxxxxxx", "xxxxxxxxxxxxxxxxxxxxx");
ParseQuery<ParseObject> query = ParseQuery.getQuery("flyerDataFetch");
query.getInBackground("xWMyZ4YEGZ", new GetCallback<ParseObject>() {
@Override
public void done(ParseObject parseObject, com.parse.ParseException e) {
if (e == null) {
Information information = new Information();
information.partyName = parseObject.getString("partyName");
information.partyPromoterName = parseObject.getString("partyCost");
information.partyCost = parseObject.getString("partyName");
information.flyerPic = parseObject.getParseFile("partyFlyerImage");
information.partyPromoterPic = parseObject.getParseFile("partyPromoterImage");
data.add(information);
} else {
// something went wrong
}
mRecyclerDrawer = (RecyclerView) findViewById(R.id.drawerList);
mRecyclerDrawer.setLayoutManager(new LinearLayoutManager(context));
mAdapter = new customAdapter(context, data);
mRecyclerDrawer.setAdapter(mAdapter);
}
});
}
MyApplication
:
@Override
public void onCreate() {
super.onCreate();
sInstance = this;
ParseCrashReporting.enable(this);
Parse.enableLocalDatastore(this);
Parse.initialize(this, "xxxxxxxxxxxxxxxx", "xxxxxxxxxxxxxxxx");
}
CustomAdapter
:
package com.example.jbobo_000.prac;
import android.content.Context;
import android.content.Intent;
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.ParseImageView;
import java.util.Collections;
import java.util.List;
public class customAdapter extends RecyclerView.Adapter<customAdapter.MyViewHolder> {
List<Information> data = Collections.emptyList();
private LayoutInflater inflater;
private Context context;
public customAdapter (Context context,List<Information>data){
this.context=context;
inflater=LayoutInflater.from(context);
this.data= data;
}
@Override
public MyViewHolder onCreateViewHolder(ViewGroup parent, int viewType){
View view= inflater.inflate(R.layout.custom_row, parent, false);
MyViewHolder holder = new MyViewHolder(view);
return holder;
}
@Override
public void onBindViewHolder(MyViewHolder holder,int position){
Information current= data.get(position);
holder.promoterNameText.setText(current.partyPromoterName);
holder.costText.setText(current.partyCost);
holder.partyNameText.setText(current.partyName);
holder.flyerImage.setParseFile(current.flyerPic);
holder.promoterImage.setParseFile(current.partyPromoterPic);
}
@Override
public int getItemCount(){
return data.size();
}
class MyViewHolder extends RecyclerView.ViewHolder implements View.OnClickListener {
TextView promoterNameText;
TextView costText;
TextView partyNameText;
ParseImageView flyerImage;
ParseImageView promoterImage;
public MyViewHolder(View itemView) {
super(itemView);
itemView.setOnClickListener(this);
promoterImage = (ParseImageView) itemView.findViewById(R.id.promoterPicImage);
flyerImage = (ParseImageView) itemView.findViewById(R.id.flyertImage);
costText = (TextView) itemView.findViewById(R.id.costText);
promoterNameText = (TextView) itemView.findViewById(R.id.promoterNameText);
partyNameText = (TextView) itemView.findViewById(R.id.partyNameText);
}
@Override
public void onClick(View v) {
context.startActivity(new Intent(context, partyInformation.class));
}
}
}
Information
:
package com.example.jbobo_000.prac;
import com.parse.ParseFile;
import com.parse.ParseImageView;
public class Information {
public ParseFile flyerPic;
public ParseFile partyPromoterPic;
public String partyCost;
public String partyName;
public String partyPromoterName;
}
Logcat
:
06-12 01:52:37.511 5432-5432/com.example.jbobo_000.prac E/RecyclerView﹕ No adapter attached; skipping layout
06-12 01:52:37.766 5432-5432/com.example.jbobo_000.prac D/AndroidRuntime﹕ Shutting down VM
06-12 01:52:37.766 5432-5432/com.example.jbobo_000.prac E/CrashReporting﹕ ParseCrashReporting caught a NullPointerException exception for com.example.jbobo_000.prac. Building report.
06-12 01:52:37.768 5432-5432/com.example.jbobo_000.prac E/CrashReporting﹕ Handling exception for crash
java.lang.NullPointerException: Attempt to invoke virtual method 'java.lang.Object android.content.Context.getSystemService(java.lang.String)' on a null object reference
at android.view.LayoutInflater.from(LayoutInflater.java:219)
at com.example.jbobo_000.prac.customAdapter.<init>(customAdapter.java:25)
at com.example.jbobo_000.prac.HomeScreen$1.done(HomeScreen.java:57)
at com.example.jbobo_000.prac.HomeScreen$1.done(HomeScreen.java:38)
at com.parse.ParseTaskUtils$2$1.run(ParseTaskUtils.java:107)
at android.os.Handler.handleCallback(Handler.java:739)
at android.os.Handler.dispatchMessage(Handler.java:95)
at android.os.Looper.loop(Looper.java:135)
at android.app.ActivityThread.main(ActivityThread.java:5254)
at java.lang.reflect.Method.invoke(Native Method)
at java.lang.reflect.Method.invoke(Method.java:372)
at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:903)
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:698)
06-12 01:52:37.768 5432-5432/com.example.jbobo_000.prac D/CrashReporting﹕ Generating report file for crash
06-12 01:52:37.843 5432-5432/com.example.jbobo_000.prac D/CrashReporting﹕ Retrieving logcat output...
06-12 01:52:37.871 5432-5432/com.example.jbobo_000.prac D/CrashReporting﹕ Retrieving logcat output...
06-12 01:52:37.889 5432-5432/com.example.jbobo_000.prac D/CrashReporting﹕ Retrieving logcat output...
06-12 01:52:37.965 5432-5432/com.example.jbobo_000.prac W/Settings﹕ Setting stay_on_always has moved from android.provider.Settings.System to android.provider.Settings.Global, returning read-only value.
06-12 01:52:38.164 5432-5432/com.example.jbobo_000.prac V/CrashReporting﹕ About to start ReportSenderWorker from #handleException
06-12 01:52:38.172 5432-5603/com.example.jbobo_000.prac D/CrashReporting﹕ #checkAndSendReports - start
06-12 01:52:38.172 5432-5603/com.example.jbobo_000.prac D/CrashReporting﹕ Looking for error files in /data/data/com.example.jbobo_000.prac/files/com.parse/cr/reports
06-12 01:52:38.175 5432-5603/com.example.jbobo_000.prac D/CrashReporting﹕ Loading file 1434088357768-NullPointerException-1.stacktrace
06-12 01:52:38.185 5432-5603/com.example.jbobo_000.prac I/CrashReporting﹕ Sending file 1434088357768-NullPointerException-1.stacktrace
06-12 01:52:38.186 5432-5603/com.example.jbobo_000.prac D/CrashReporting﹕ Sending crash report to Parse...
06-12 01:52:38.193 5432-5603/com.example.jbobo_000.prac D/CrashReporting﹕ #checkAndSendReports - finish
06-12 01:52:38.265 5432-5432/com.example.jbobo_000.prac E/AndroidRuntime﹕ FATAL EXCEPTION: main
Process: com.example.jbobo_000.prac, PID: 5432
java.lang.NullPointerException: Attempt to invoke virtual method 'java.lang.Object android.content.Context.getSystemService(java.lang.String)' on a null object reference
at android.view.LayoutInflater.from(LayoutInflater.java:219)
at com.example.jbobo_000.prac.customAdapter.<init>(customAdapter.java:25)
at com.example.jbobo_000.prac.HomeScreen$1.done(HomeScreen.java:57)
at com.example.jbobo_000.prac.HomeScreen$1.done(HomeScreen.java:38)
at com.parse.ParseTaskUtils$2$1.run(ParseTaskUtils.java:107)
at android.os.Handler.handleCallback(Handler.java:739)
at android.os.Handler.dispatchMessage(Handler.java:95)
at android.os.Looper.loop(Looper.java:135)
at android.app.ActivityThread.main(ActivityThread.java:5254)
at java.lang.reflect.Method.invoke(Native Method)
at java.lang.reflect.Method.invoke(Method.java:372)
at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:903)
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:698)
06-12 01:52:38.291 5432-5442/com.example.jbobo_000.prac W/art﹕ Suspending all threads took: 5.494ms
答案 0 :(得分:2)
这里是您应该更改的所有内容..首先您没有获得table(Object)flyerDataFetch的所有数据,您只是获取ID为“xWMyZ4YEGZ”的一个特定列数据,您的查询应该是
ParseQuery<ParseObject> query = ParseQuery.getQuery("flyerDataFetch");
query.findInBackgroud(new FindCallBack<TableClass>(){
@Override
public void done(ParseObject List<TableName> list, com.parse.ParseException e) {
if (e == null) {
// GENERATE LOOP HERE AND GET ALL DATA OF LIST INTO YOUR LOCAL LIST WHICH YOU ARE PASSING TO ADAPTER OF RECYCLER VIEW
//I am telling you to add loop so you can learn..
Information information = new Information();
information.partyName = parseObject.getString("partyName");
information.partyPromoterName = parseObject.getString("partyCost");
information.partyCost = parseObject.getString("partyName");
information.flyerPic = parseObject.getParseFile("partyFlyerImage");
information.partyPromoterPic = parseObject.getParseFile("partyPromoterImage");
data.add(information);
} else {
// something went wrong
}
这将获取表中的所有数据并在List中设置,您将传递给适配器。这可能是你的NullPointerException的原因!
另一件事是你获得图像的方式是错误的 你实际上需要执行查询并获取bytes []数组,然后你可以在ParseImageView
中设置Bitmap bmp = BitmapFactory.decodeByteArray(data, 0, data.length, options);
myViewHolder.myImageView.setBitmapImage(bmp);
你也不需要在customAdapter中使用上下文..只需写下这个
private LayoutInflater inflater;
public customAdapter (Context context,List<Information>data){
inflater=LayoutInflater.from(context);
this.data= data;
}
也在你的MainActivity类中......改变这个
mRecyclerDrawer.setLayoutManager(new LinearLayoutManager(context));
mAdapter = new customAdapter(context, data);
到
mRecyclerDrawer.setLayoutManager(new LinearLayoutManager(this));
mAdapter = new customAdapter(getApplicationContext(), data);
..声明您的以下数据
final List<Information> data = new ArrayList<>();
在onCreate()方法之前的MainActivity中..
希望它有所帮助!答案 1 :(得分:2)
context为null。请在调用适配器之前将上下文值设置为活动。 添加
setContentView(R.layout.activity_home_screen);
context=this;