我对此代码有疑问。我试图从firebase数据库传递检索到的字符串图像,但它一直崩溃,Firebasye显示此错误:异常java.lang.NullPointerException:尝试调用虚方法' java.lang.String com。 example.vujeva.Task.getImage()'在null对象引用上 com.example.vujeva.MainActivity $ BlogViewHolder.onClick(MainActivity.java:161)
这是我的MainActivity:
public class MainActivity extends AppCompatActivity {
public static final String TASK_NAME = "com.example.vujeva.tutorshare.title";
public static final String TASK_DESC = "com.example.vujeva.tutorshare.desc";
public static final String TASK_IMAGE = "com.example.vujeva.tutorshare.image";
private RecyclerView mBlogList;
//private ListView mTaskList;
private DatabaseReference mDatabase;
List<Task> artists;
private FloatingActionButton fab;
//private ListView lv;
ArrayAdapter<String> adapter;
String key;
CardView card_view;
ImageView viewy;View inflatedView;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
//mTaskList = (ListView) findViewById(R.id.task_list);
mDatabase = FirebaseDatabase.getInstance().getReference().child("Task");
mBlogList = (RecyclerView)findViewById(R.id.task_list);
mBlogList.setHasFixedSize(true);
mBlogList.setLayoutManager(new LinearLayoutManager(this));
//mTaskList.setHasFixedSize(true);
//mTaskList.setLayoutManager(new LinearLayoutManager(this));
artists = new ArrayList<>();
fab = (FloatingActionButton) findViewById(R.id.fab);
fab.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view) {
Intent intent = new Intent(MainActivity.this, AddActivity.class);
startActivity(intent);
}
});
}
@Override
protected void onStart() {
super.onStart();
FirebaseRecyclerAdapter<Task, BlogViewHolder> firebaseRecyclerAdapter =
new FirebaseRecyclerAdapter<Task, BlogViewHolder>(
Task.class,
R.layout.design_row,
BlogViewHolder.class,
mDatabase){
protected void populateViewHolder(final BlogViewHolder viewHolder,final Task model, int position) {
ImageView post_image = (ImageView) findViewById(R.id.imageViewy);
viewHolder.setTitle(model.getTitle());
viewHolder.setImage(getApplicationContext(), model.getImage());
}
};
mBlogList.setAdapter(firebaseRecyclerAdapter);
}
public static class BlogViewHolder extends RecyclerView.ViewHolder implements View.OnClickListener {
View mView;
private ArrayList<Task> mPhotos;
Task model;
ImageView postCommentLayout;
public BlogViewHolder(View itemView) {
super(itemView);
mView= itemView;
postCommentLayout = (ImageView) itemView.findViewById(R.id.imageViewy);
itemView.setOnClickListener(this);
}
public void setTitle(String title){
TextView post_title = (TextView)mView.findViewById(R.id.titleText);
post_title.setText(title);
}
public void setImage(Context ctx , String image){
ImageView post_image = (ImageView)mView.findViewById(R.id.imageViewy);
// We Need TO pass Context
Picasso.with(ctx).load(image).into(post_image);
}
@Override
public void onClick(View itemView) {
Context context = itemView.getContext();
Intent showPhotoIntent = new Intent(context, ActionActivity.class);
showPhotoIntent.putExtra(TASK_IMAGE, model.getImage().toString());
context.startActivity(showPhotoIntent);
}
}
这是我的ActionActivity:
public class ActionActivity extends MainActivity {
private CollapsingToolbarLayout collapsingToolbarLayout = null;
private RecyclerView mImageTask;
private DatabaseReference mDatabase;
private ImageView mImage;
Intent intent;
public Uri mImageUri = null;
List<Task> artists;
String image;
private Task mPost;
public static final String TASK_IMAGE = "com.example.vujeva.tutorshare.image";
public static final String DATABASE_PATH_UPLOADS = "Tasks";
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_action);
Intent intent = getIntent();
image = intent.getStringExtra(MainActivity.TASK_IMAGE);
mImage = (ImageView) findViewById(R.id.profile_id);
Picasso.with(this).load(image).into(mImage);
}
private void toolbarTextAppernce() {
collapsingToolbarLayout.setCollapsedTitleTextAppearance(R.style.collapsedappbar);
collapsingToolbarLayout.setExpandedTitleTextAppearance(R.style.expandedappbar);
}
答案 0 :(得分:0)
在CommandLineRunner
中,您声明了BlogViewHolder
但从未初始化。在Task model;
,您拨打onClick()
,但model.getImage().toString()
仍为空。
答案 1 :(得分:0)
在protected void populateViewHolder
中 protected void populateViewHolder(final BlogViewHolder viewHolder,final Task model, int position) {
ImageView post_image = (ImageView) findViewById(R.id.imageViewy);
viewHolder.setTitle(model.getTitle());
// viewHolder.setImage(getApplicationContext(), model.getImage());
Picasso.with(viewHolder.imageView.getContext()).load(model.getUrl()).into(viewHolder.imageView); //use this
}
};