0
下面檢索數據是我的數據在火力地堡圖片:顯示java.lang.NullPointerException而從火力地堡
所以我試圖找回DoctorsName
和Email
在Recyclerview時,當他們是兩個醫生recyclerView的項目列表。但我不知道如何使用get()
方法在我的Recycleriew中檢索它們。 這是錯誤我得到
java.lang.NullPointerException: Attempt to invoke interface method 'java.lang.Object java.util.List.get(int)' on a null object reference
下面是我的MainActiviy.java
。
import android.app.Dialog;
import android.content.Context;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.support.v7.widget.LinearLayoutManager;
import android.support.v7.widget.RecyclerView;
import android.view.View;
import android.widget.Button;
import com.google.android.gms.common.GoogleApiAvailability;
import com.google.android.gms.tasks.Task;
import com.google.firebase.database.ChildEventListener;
import com.google.firebase.database.DataSnapshot;
import com.google.firebase.database.DatabaseError;
import com.google.firebase.database.DatabaseReference;
import com.google.firebase.database.FirebaseDatabase;
import com.google.firebase.database.ValueEventListener;
import java.util.ArrayList;
import java.util.List;
import java.util.Map;
public class MainActivity extends AppCompatActivity {
private Button but;
private DatabaseReference mRef;
private MyAdapter adapter;
public RecyclerView recyclerView;
public List<ModelClass> movies_;
public Context c;
private LinearLayoutManager linearLayoutManager;
public DatabaseReference mMovieRef ;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
movies_ = new ArrayList<ModelClass>();
recyclerView =(RecyclerView) findViewById(R.id.recyclerview);
linearLayoutManager = new LinearLayoutManager(this);
recyclerView.setLayoutManager(linearLayoutManager);
recyclerView.setHasFixedSize(true);
mRef = FirebaseDatabase.getInstance().getReferenceFromUrl("https://askdoctor-546bf.firebaseio.com/Doctors");
// mMovieRef = mRef.child("Doctors");
mRef.addChildEventListener(new ChildEventListener() {
@Override
public void onChildAdded(DataSnapshot dataSnapshot, String s) {
LoadData(dataSnapshot);
}
@Override
public void onChildChanged(DataSnapshot dataSnapshot, String s) {
LoadData(dataSnapshot);
}
@Override
public void onChildRemoved(DataSnapshot dataSnapshot) {
}
@Override
public void onChildMoved(DataSnapshot dataSnapshot, String s) {
}
@Override
public void onCancelled(DatabaseError databaseError) {
}
});
}
public void LoadData (DataSnapshot dataSnapshot){
for(DataSnapshot singlesnapshot :dataSnapshot.getChildren()){
String taskTitle =singlesnapshot.getValue(String.class);
movies_.add(new ModelClass(taskTitle));
adapter = new MyAdapter(MainActivity.this, movies_);
recyclerView.setAdapter(adapter);
}
}
}
這是我的Adapter
類。
import android.content.Context;
import android.support.v7.widget.RecyclerView;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import com.google.firebase.FirebaseApp;
import com.google.firebase.storage.FirebaseStorage;
import com.google.firebase.storage.StorageReference;
import com.squareup.picasso.Picasso;
import java.util.ArrayList;
import java.util.List;
/**
* Created by HUZY_KAMZ on 9/16/2016.
*/
public class MyAdapter extends RecyclerView.Adapter<MyViewHolder> {
protected Context c;
List<ModelClass> movies;
FirebaseStorage storage = FirebaseStorage.getInstance();
StorageReference url =storage.getReferenceFromUrl("gs://askdoctor-546bf.appspot.com/");
private List<ModelClass> itemList = null;
public MyAdapter(Context c, List<ModelClass> movies) {
this.c = c;
this.movies = movies;
}
@Override
public MyViewHolder onCreateViewHolder(ViewGroup parent, int viewType) {
View v = LayoutInflater.from(parent.getContext()).inflate(R.layout.item_activity,parent,false);
MyViewHolder holder = new MyViewHolder(v);
return holder;
}
@Override
public void onBindViewHolder(MyViewHolder holder, int position) {
final ModelClass feedItems = itemList.get(position);
holder.name.setText(movies.get(position).getName());
holder.email_txt.setText(movies.get(position).getEmail());
// PicassoClient.downloadImage(c,movies.get(position).getUrl(),holder.img);
//Download image using picasso library
Picasso.with(c).load(url+ feedItems.getName())
.error(R.mipmap.ic_launcher).
resize(120, 50)
.placeholder(R.mipmap.ic_launcher).into(holder.img);
}
@Override
public int getItemCount() {
return movies.size();
}
}
我認爲你永遠不會使用該列表,所以你可以簡單地刪除該行,也將它從頂部初始化爲空。名爲「movies」的列表是要使用的正確列表,因爲您在創建適配器時使用了該列表。 –
我試過了,但仍然是@Tony的錯誤,我認爲這個問題是關於從Firebase檢索那種數據的語法,你檢查我上傳的圖片。所以如果你知道它,請幫助我。 –
您是否嘗試過記錄快照的結果?在我們檢查我們如何將它們設置爲文本之前,如果您獲得了您想要的正確值,請首先檢查 –