我正在经历一段时间以来一直在努力的崩溃。
所以这是我的应用程序,这里有一个片段并删除了汽车列表
这是用于在第一个片段中打印数据的方法:
void printData(View view){
mDatabase = FirebaseDatabase.getInstance().getReference();
firebaseAuth = FirebaseAuth.getInstance();
final FirebaseUser FireUser = firebaseAuth.getCurrentUser();
if(FireUser != null)
{
String uid = FireUser.getUid().toString();
mDatabase.child("cars").child(uid).addValueEventListener(new ValueEventListener() {
@Override
public void onDataChange(DataSnapshot dataSnapshot) {
Car car_db1 = dataSnapshot.child("car1").getValue(Car.class);
Car car_db2 = dataSnapshot.child("car2").getValue(Car.class);
Car car_db3 = dataSnapshot.child("car3").getValue(Car.class);
Car car_db4 = dataSnapshot.child("car4").getValue(Car.class);
mCarView1.setText(car_db1.getBrand());
mCarView10.setText(car_db1.getColor()+ ", " + car_db1.getFuelType() + ", " + car_db1.getNumberPlate());
mCarView2.setText(car_db2.getBrand());
mCarView20.setText(car_db2.getColor()+ ", " + car_db2.getFuelType() + ", " + car_db2.getNumberPlate());
mCarView3.setText(car_db3.getBrand());
mCarView30.setText(car_db3.getColor()+ ", " + car_db3.getFuelType() + ", " + car_db3.getNumberPlate());
mCarView4.setText(car_db4.getBrand());
mCarView40.setText(car_db4.getColor()+ ", " + car_db4.getFuelType() + ", " + car_db4.getNumberPlate());
if(car_db1.getBrand().equals("none") && car_db2.getBrand().equals("none") && car_db3.getBrand().equals("none") && car_db4.getBrand().equals("none") )
{
mAddCar1.setText("Add");
mAddCar2.setText("Add");
mAddCar3.setText("Add");
mAddCar4.setText("Add");
mDelCar1.setVisibility(View.VISIBLE);
mDelCar2.setVisibility(View.VISIBLE);
mDelCar3.setVisibility(View.VISIBLE);
car10layout.setVisibility(View.GONE);
blueLine1.setVisibility(View.GONE);
car2layout.setVisibility(View.GONE);
car20layout.setVisibility(View.GONE);
blueLine2.setVisibility(View.GONE);
car3layout.setVisibility(View.GONE);
car30layout.setVisibility(View.GONE);
blueLine3.setVisibility(View.GONE);
car4layout.setVisibility(View.GONE);
car40layout.setVisibility(View.GONE);
}
else if(car_db2.getBrand().equals("none") && car_db3.getBrand().equals("none") && car_db4.getBrand().equals("none") )
{
mAddCar1.setText("Edit");
mAddCar2.setText("Add");
mAddCar3.setText("Add");
mAddCar4.setText("Add");
mDelCar1.setVisibility(View.VISIBLE);
mDelCar2.setVisibility(View.VISIBLE);
mDelCar3.setVisibility(View.VISIBLE);
car10layout.setVisibility(View.VISIBLE);
blueLine1.setVisibility(View.VISIBLE);
car2layout.setVisibility(View.VISIBLE);
car20layout.setVisibility(View.GONE);
blueLine2.setVisibility(View.GONE);
car3layout.setVisibility(View.GONE);
car30layout.setVisibility(View.GONE);
blueLine3.setVisibility(View.GONE);
car4layout.setVisibility(View.GONE);
car40layout.setVisibility(View.GONE);
}
else if( car_db3.getBrand().equals("none") && car_db4.getBrand().equals("none") )
{
mAddCar1.setText("Edit");
mAddCar2.setText("Edit");
mAddCar3.setText("Add");
mAddCar4.setText("Add");
mDelCar1.setVisibility(View.GONE);
mDelCar2.setVisibility(View.VISIBLE);
mDelCar3.setVisibility(View.VISIBLE);
car10layout.setVisibility(View.VISIBLE);
blueLine1.setVisibility(View.VISIBLE);
car2layout.setVisibility(View.VISIBLE);
car20layout.setVisibility(View.VISIBLE);
blueLine2.setVisibility(View.VISIBLE);
car3layout.setVisibility(View.VISIBLE);
car30layout.setVisibility(View.GONE);
blueLine3.setVisibility(View.GONE);
car4layout.setVisibility(View.GONE);
car40layout.setVisibility(View.GONE);
}
else if( car_db4.getBrand().equals("none"))
{
mAddCar1.setText("Edit");
mAddCar2.setText("Edit");
mAddCar3.setText("Edit");
mAddCar4.setText("Add");
mDelCar1.setVisibility(View.GONE);
mDelCar2.setVisibility(View.GONE);
mDelCar3.setVisibility(View.VISIBLE);
car10layout.setVisibility(View.VISIBLE);
blueLine1.setVisibility(View.VISIBLE);
car2layout.setVisibility(View.VISIBLE);
car20layout.setVisibility(View.VISIBLE);
blueLine2.setVisibility(View.VISIBLE);
car3layout.setVisibility(View.VISIBLE);
car30layout.setVisibility(View.VISIBLE);
blueLine3.setVisibility(View.VISIBLE);
car4layout.setVisibility(View.VISIBLE);
car40layout.setVisibility(View.GONE);
}
else
{
mAddCar1.setText("Edit");
mAddCar2.setText("Edit");
mAddCar3.setText("Edit");
mAddCar4.setText("Edit");
mDelCar1.setVisibility(View.GONE);
mDelCar2.setVisibility(View.GONE);
mDelCar3.setVisibility(View.GONE);
car10layout.setVisibility(View.VISIBLE);
blueLine1.setVisibility(View.VISIBLE);
car2layout.setVisibility(View.VISIBLE);
car20layout.setVisibility(View.VISIBLE);
blueLine2.setVisibility(View.VISIBLE);
car3layout.setVisibility(View.VISIBLE);
car30layout.setVisibility(View.VISIBLE);
blueLine3.setVisibility(View.VISIBLE);
car4layout.setVisibility(View.VISIBLE);
car40layout.setVisibility(View.VISIBLE);
}
}
@Override
public void onCancelled(DatabaseError databaseError) {
// Failed to read value
}
});
}
}
您可以在这里添加汽车(从对话框警告框中)或删除一个汽车(
mAddCar4.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
car4 = chooseCar(mCarView4, "car4");
}
});
mDelCar4.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
// Save to database user informations
firebaseUser = firebaseAuth.getCurrentUser();
String uID = firebaseUser.getUid();
// set to none all data
car4 = new Car("none", "none", "none", "none");
mDatabase.child("cars").child(uID).child("car4").setValue(car4);
}
});
到目前为止它运作良好。 screenshot of this fragment in app 但是现在我正在实施一种方法来选择新片段(片段)中前一片段中添加的其中一辆车。
以下是我打印和选择合适车型的方法,
package xxxxxxxxxxx.fragmentClasses;
import android.content.DialogInterface;
import android.content.Intent;
import android.os.Bundle;
import android.app.Fragment;
import android.support.v7.app.AlertDialog;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.Button;
import com.google.firebase.auth.FirebaseAuth;
import com.google.firebase.auth.FirebaseUser;
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.lang.reflect.Array;
import java.util.Arrays;
import java.util.Map;
import xxxxxxxxxxx.Car;
import xxxxxxxxxxx.LoginActivity;
import xxxxxxxxxxx.MapActivity;
import xxxxxxxxxxx.R;
public class FragmentFuel extends Fragment implements View.OnClickListener {
private DatabaseReference mDatabase;
private FirebaseAuth firebaseAuth;
private FirebaseUser firebaseUser;
private Button buttonBook;
private Button buttonEditCar;
@Override
public View onCreateView(LayoutInflater inflater,
ViewGroup container,
Bundle savedInstanceState) {
// Inflate the layout for this fragment
View view = inflater.inflate(R.layout.fragment_fuel, container, false);
buttonBook =(Button) view.findViewById(R.id.button_book);
buttonBook.setOnClickListener(this);
buttonEditCar =(Button) view.findViewById(R.id.button_edit_car);
buttonEditCar.setOnClickListener(this);
return view;
}
public void onClick(View v) {
// TODO Auto-generated method stub
switch(v.getId()){
//book with informations stocked
case R.id.button_book :
getActivity().finish();
Intent intent = new Intent(getActivity(), MapActivity.class);
startActivity(intent);
break;
// open a dialog box that shows differents car registered and made the user choose one
case R.id.button_edit_car :
mDatabase = FirebaseDatabase.getInstance().getReference();
firebaseAuth = FirebaseAuth.getInstance();
final FirebaseUser FireUser = firebaseAuth.getCurrentUser();
if(FireUser != null)
{
String uid = FireUser.getUid().toString();
mDatabase.child("cars").child(uid).addValueEventListener(new ValueEventListener() {
@Override
public void onDataChange(DataSnapshot dataSnapshot) {
final String[] listCars;
final Car car = new Car();
int j;
Car car_db1 = dataSnapshot.child("car1").getValue(Car.class);
Car car_db2 = dataSnapshot.child("car2").getValue(Car.class);
Car car_db3 = dataSnapshot.child("car3").getValue(Car.class);
Car car_db4 = dataSnapshot.child("car4").getValue(Car.class);
if (car_db1.getBrand().equals("none"))
{
listCars = new String[0];
}
else if (car_db2.getBrand().equals("none"))
{
listCars = new String[1];
listCars[0] = car_db1.getBrand() + ", " + car_db1.getColor() + ", " + car_db1.getFuelType() + ", " + car_db1.getNumberPlate();
}
else if (car_db3.getBrand().equals("none"))
{
listCars = new String[2];
listCars[0] = car_db1.getBrand() + ", " + car_db1.getColor() + ", " + car_db1.getFuelType() + ", " + car_db1.getNumberPlate();
listCars[1] = car_db2.getBrand() + ", " + car_db2.getColor() + ", " + car_db2.getFuelType() + ", " + car_db2.getNumberPlate();
}
else if (car_db4.getBrand().equals("none"))
{
listCars = new String[3];
listCars[0] = car_db1.getBrand() + ", " + car_db1.getColor() + ", " + car_db1.getFuelType() + ", " + car_db1.getNumberPlate();
listCars[1] = car_db2.getBrand() + ", " + car_db2.getColor() + ", " + car_db2.getFuelType() + ", " + car_db2.getNumberPlate();
listCars[2] = car_db3.getBrand() + ", " + car_db3.getColor() + ", " + car_db3.getFuelType() + ", " + car_db3.getNumberPlate();
}
else
{
listCars = new String[4];
listCars[0] = car_db1.getBrand() + ", " + car_db1.getColor() + ", " + car_db1.getFuelType() + ", " + car_db1.getNumberPlate();
listCars[1] = car_db2.getBrand() + ", " + car_db2.getColor() + ", " + car_db2.getFuelType() + ", " + car_db2.getNumberPlate();
listCars[2] = car_db3.getBrand() + ", " + car_db3.getColor() + ", " + car_db3.getFuelType() + ", " + car_db3.getNumberPlate();
listCars[3] = car_db4.getBrand() + ", " + car_db4.getColor() + ", " + car_db4.getFuelType() + ", " + car_db4.getNumberPlate();
}
final AlertDialog myDialog1;
AlertDialog.Builder builderCar = new AlertDialog.Builder(getActivity());
builderCar.setTitle("Choose your car");
builderCar.setIcon(R.drawable.ic_menu_car);
builderCar.setCancelable(false);
builderCar.setSingleChoiceItems(listCars, -1, new DialogInterface.OnClickListener() {
@Override
public void onClick(DialogInterface dialogInterface, int i) {
}
});
//Button to decide what to do next
builderCar.setPositiveButton("Next", new DialogInterface.OnClickListener() {
@Override
public void onClick(DialogInterface dialogInterface, int i) {
//nothing here, overrided later
}
});
myDialog1 = builderCar.create();
myDialog1.show();
}
@Override
public void onCancelled(DatabaseError databaseError) {
// Failed to read value
}
});
}
break;
default:
break;
}
}
}
它也在工作,现在什么都不做,但是打印出存放在firebase中的不同车,并让我在其中一个之间做出选择(之后的恶意工具)。
screenshot of the second fragment in app
但这是我的问题,当我回到第一个片段,并删除或添加新车时,应用程序崩溃。当我重新启动应用程序时,一切正常,汽车被删除或添加。我认为问题是在第二个片段上使用onDataChange,而不是停止它,以便在应用程序与数据库交互时继续读取。但我不知道该怎么做。
这是我的logcat
Shutting down VM
FATAL EXCEPTION: main
Process: xxxxxxxx, PID: 16656
java.lang.NullPointerException: Attempt to invoke virtual method 'android.content.res.Resources$Theme android.content.Context.getTheme()' on a null object reference
at android.support.v7.app.AlertDialog.resolveDialogTheme(AlertDialog.java:113)
at android.support.v7.app.AlertDialog$Builder.<init>(AlertDialog.java:291)
at xxxxxxxx.fragmentClasses.FragmentFuel$1.onDataChange(FragmentFuel.java:121)
at com.google.android.gms.internal.tl.zza(Unknown Source)
at com.google.android.gms.internal.vg.zzHW(Unknown Source)
at com.google.android.gms.internal.vm.run(Unknown Source)
at android.os.Handler.handleCallback(Handler.java:751)
at android.os.Handler.dispatchMessage(Handler.java:95)
at android.os.Looper.loop(Looper.java:154)
at android.app.ActivityThread.main(ActivityThread.java:6290)
at java.lang.reflect.Method.invoke(Native Method)
at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:886)
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:776)
Event: crash
糟糕的是我在使用onDataChange时无法使用调试器...
如果有人知道这对我有帮助!抱歉我的英语;)