我需要更新Firebase中的现有数据。编译代码时没有错误,但是不幸的是,当我按下Update按钮时应用程序停止了。在我的数据库中有两个子节点snakes和Habitat。用来存储数据。Logcat显示覆盖onclick方法时,String name=SciName.getSelectedItem().toString();
处的Null指针异常。
public void showUpdatedDialog(final String snakeID, String snakeName) // dialog box
{
AlertDialog.Builder dialogBuilder=new AlertDialog.Builder(this);
LayoutInflater inflater=getLayoutInflater();
final View dialogView=inflater.inflate(R.layout.update_dialog,null);
dialogBuilder.setView(dialogView);
final Button updatebutton=(Button)dialogView.findViewById(R.id.btn_upload_snake);
final Spinner SciName=(Spinner)findViewById(R.id.scientificName);
final EditText Snakename=(EditText)findViewById(R.id.etSnakeName);
final EditText Habitat1=(EditText)findViewById(R.id.etHabitat1);
final EditText Habitat2=(EditText)findViewById(R.id.etHabitat2);
final EditText Tretment=(EditText)findViewById(R.id.etTreatment);
final EditText Behaviour=(EditText)findViewById(R.id.etBehaviour);
final EditText Venomousness=(EditText)findViewById(R.id.etVenomousness);
final EditText Distribution=(EditText)findViewById(R.id.etDistribution);
final EditText Habit=(EditText)findViewById(R.id.etHabit);
final EditText Reproduction=(EditText)findViewById(R.id.etReproduction);
dialogBuilder.setTitle("Updating Snake details::"+snakeName);
final AlertDialog alertDialog=dialogBuilder.show();
alertDialog.show();
updatebutton.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
String name=SciName.getSelectedItem().toString();
String Sname=Snakename.getText().toString().trim();
String H1=Habitat1.getText().toString().trim();
String H2=Habitat2.getText().toString().trim();
String Treatment=Tretment.getText().toString().trim();
String Habit1=Habit.getText().toString().trim();
String Behaviour2=Behaviour.getText().toString().trim();
String Distribution2=Distribution.getText().toString().trim();
String Reproduction2=Reproduction.getText().toString().trim();
String Venom=Venomousness.getText().toString().trim();
if(TextUtils.isEmpty(Sname)){
Snakename.setError("Name Is Required");
return;
}
updatesnake(snakeID,name,Sname,Treatment,Behaviour2,Venom,Distribution2,Habit1,Reproduction2,H1,H2);
alertDialog.dismiss();
}
});
}
public void updatesnake( String id, String SCINAME,String snakename, String TREATMENT,String BEHAVIOUR,String VENOMOUSNESS,String DISTRIBUTION,String HABIT,String REPRODUCTION,String HABITAT1,String HABITAT2){ // update details to snakes.
DatabaseReference databaseSnake=FirebaseDatabase.getInstance().getReference("snake").child("id");
Snake snake=new Snake(id ,SCINAME,snakename,TREATMENT,BEHAVIOUR,VENOMOUSNESS,DISTRIBUTION,HABIT,REPRODUCTION);
databaseSnake.setValue(snake);
DatabaseReference databaseSnake2=FirebaseDatabase.getInstance().getReference("habitat");
Habitat habitat=new Habitat(HABITAT1,HABITAT2);
databaseSnake2.child(id).setValue(habitat);
Toast.makeText(this,"UPDATE IS SUCCESSFUL",Toast.LENGTH_LONG).show();
}
}