我正在尝试从Firebase数据库中检索数据到RecyclerView但遇到此错误java.lang.NullPointerException:尝试调用虚方法。 'com.google.firebase.database.DatabaseReference com.google.firebase.database.DatabaseReference.child(java.lang.String)' on a null object reference
以下是我的主要活动课程。
package oerrride.we.huzykamz.testingappfirebase;
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;
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;
private DatabaseReference mMovieRef =mRef.child("Name");
@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("xxxxxxxxxxxxxxxxxxxxxx");
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);
}
}
}
And this is my Adapter class .
public class MyAdapter extends RecyclerView.Adapter<MyViewHolder> {
protected Context c;
List<ModelClass> movies;
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) {
holder.name.setText(movies.get(position).getName());
// PicassoClient.downloadImage(c,movies.get(position).getUrl(),holder.img);
}
@Override
public int getItemCount() {
return movies.size();
}
}
以下是我的Firebase数据库控制台
答案 0 :(得分:4)
private DatabaseReference mMovieRef =mRef.child("Name");
mRef尚未创建,因此它为null并将抛出NullPointerException
<强>解决方案:强>
将您的声明更改为:
private DatabaseReference mMovieRef;
并在创建mRef
后为其赋值mRef = FirebaseDatabase.getInstance().getReferenceFromUrl("xxxxxxxxxxxxxxxxxxxxxx");
mMovieRef = mRef.child("Name");
答案 1 :(得分:0)
可能没有初始化DatabaseReference:尝试使用:
private DatabaseReference mMovieRef =new DatabaseReference()
mRef.child("Name");
或者它可以是:
private DatabaseReference mMovieRef =FirebaseDatabase.getInstance().getReferenceFromUrl("xxxxxxxxxxxxxxxxxxxxxx");
mRef.child("Name");
而不是
private DatabaseReference mMovieRef =mRef.child("Name");
答案 2 :(得分:0)
您不需要在主体外部初始化或声明<script src="https://unpkg.com/javascript-lp-solver/prod/solver.js"></script>
<script>
var model = {
"optimize": "cost",
"opType": "min",
"constraints": {
"Zr02": {"min": 0.32, "max": 0.34},
"Al203": {"min": 0.49, "max": 0.52},
"Si02": {"min": 0.15, "max": 0.17},
"Na20": {"min": 0.013, "max": 0.02}
},
"variables": {
"AZS": {
"Zr02": 0.30,
"Al203": 0.50,
"Si02": 0.13,
"Na20": 0.018,
"cost": 328.95
},
"Fine Salt": {
"Zr02": 0.25,
"Al203": 0.30,
"Si02": 0.43,
"Na20": 0.002,
"cost": 203.95
},
"ZDCF": {
"Zr02": 0.63,
"Al203": 0.01,
"Si02": 0.33,
"Na20": 0.004,
"cost": 789.47
},
"AB Fines": {
"Zr02": 0.00,
"Al203": 0.95,
"Si02": 0.01,
"Na20": 0.04,
"cost": 263.16
},
"HJO": {
"Zr02": 0.90,
"Al203": 0.01,
"Si02": 0.01,
"Na20": 0.002,
"cost": 1315.79
}
},
};
var results = solver.Solve( model );
console.log( results );
let constraintResults = {};
for ( let v in results ) {
if ( !( v === 'bounded' || v === 'feasible' || v === 'result' ) ) {
for ( let c in model.constraints ) {
constraintResults[ c ] = ( constraintResults[ c ] || 0.0 ) + model.variables[ v ][ c ] * results[ v ];
}
}
}
console.log( constraintResults );
</script>
。我的意思是不要在外部将其声明为私有变量!
DatabaseReference
您可以在此处添加您的听众