无法将数据从 firebase 添加到字符串数组
Unable to add data from firebase to String Array
因此,我尝试在其每个索引处以字符串数组的形式从 firebase 实时数据库传递数据,但我的适配器 class get View 方法显示尝试读取空数组,但我将数据添加到数组。
这是我启动阵列 mCost
的 ACTIVITY
public class Department extends AppCompatActivity {
ListView listView;
String[] mItem = {"Vehicle Dynamics", "Chassis", "Power Train", "Brakes", "Electronics","Miscellaneous"};
int[] mImage = {R.drawable.vehicledynamics, R.drawable.chassis, R.drawable.powertrain,R.drawable.brakes,
R.drawable.electronics, R.drawable.miscellaneous};
String[] mCost;
int sumofdcost=0;
String sumofdcostvalue;
private DatabaseReference vdDbcost,cDbcost,ptDbcost,bDbcost,eDbcost,mDbcost, dDb,nDb;
private FirebaseDatabase database;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_department);
listView = findViewById(R.id.listView);
Objects.requireNonNull(getSupportActionBar()).setDisplayHomeAsUpEnabled(true);
database=FirebaseDatabase.getInstance();
nDb=database.getReference().child("Navigation Activity").child("Cost");
dDb=database.getReference().child("Department");
vdDbcost=database.getReference().child("Department").child("Vehicle Dynamics").child("Cost");
cDbcost=database.getReference().child("Department").child("Chassis").child("Cost");
ptDbcost=database.getReference().child("Department").child("Brakes").child("Cost");
bDbcost=database.getReference().child("Department").child("Power Train").child("Cost");
eDbcost=database.getReference().child("Department").child("Electronics").child("Cost");
mDbcost=database.getReference().child("Department").child("Miscellaneous").child("Cost");
vdDbcost.addValueEventListener(new ValueEventListener() {
@Override
public void onDataChange(@NonNull DataSnapshot dataSnapshot) {
Long cost=dataSnapshot.getValue(Long.class);
mCost[0]=String.valueOf(cost);
}
@Override
public void onCancelled(@NonNull DatabaseError databaseError) { }
});
cDbcost.addValueEventListener(new ValueEventListener() {
@Override
public void onDataChange(@NonNull DataSnapshot dataSnapshot) {
Long cost=dataSnapshot.getValue(Long.class);
mCost[1]=String.valueOf(cost);
}
@Override
public void onCancelled(@NonNull DatabaseError databaseError) { }
});
ptDbcost.addValueEventListener(new ValueEventListener() {
@Override
public void onDataChange(@NonNull DataSnapshot dataSnapshot) {
Long cost=dataSnapshot.getValue(Long.class);
mCost[2]=String.valueOf(cost);
}
@Override
public void onCancelled(@NonNull DatabaseError databaseError) { }
});
bDbcost.addValueEventListener(new ValueEventListener() {
@Override
public void onDataChange(@NonNull DataSnapshot dataSnapshot) {
Long cost=dataSnapshot.getValue(Long.class);
mCost[3]=String.valueOf(cost);
}
@Override
public void onCancelled(@NonNull DatabaseError databaseError) { }
});
eDbcost.addValueEventListener(new ValueEventListener() {
@Override
public void onDataChange(@NonNull DataSnapshot dataSnapshot) {
Long cost=dataSnapshot.getValue(Long.class);
mCost[4]=String.valueOf(cost);
}
@Override
public void onCancelled(@NonNull DatabaseError databaseError) { }
});
mDbcost.addValueEventListener(new ValueEventListener() {
@Override
public void onDataChange(@NonNull DataSnapshot dataSnapshot) {
Long cost=dataSnapshot.getValue(Long.class);
mCost[5]=String.valueOf(cost);
}
@Override
public void onCancelled(@NonNull DatabaseError databaseError) { }
});
dDb.addValueEventListener(new ValueEventListener() {
@Override
public void onDataChange(@NonNull DataSnapshot dataSnapshot) {
for(DataSnapshot ds: dataSnapshot.getChildren()){
Map<String,Object> map=(Map<String,Object>) ds.getValue();
Object totalcost=map.get("Cost");
int totalcostvalue=Integer.parseInt(String.valueOf(totalcost));
sumofdcost+=totalcostvalue;
sumofdcostvalue=String.valueOf(sumofdcost);
}
nDb.setValue(sumofdcostvalue);
}
@Override
public void onCancelled(@NonNull DatabaseError databaseError) {
}
});
//Creating ADAPTER class
MainAdapter adapter = new MainAdapter(this, mItem, mImage, mCost);
listView.setAdapter(adapter);
listView.setOnItemClickListener(new AdapterView.OnItemClickListener() {
@Override
public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
if (position == 0) {
//A//
//sub department, vehicle dynamics//
Intent intent = new Intent(Department.this, VehicleDynamicsActivity.class);
startActivity(intent);
}
if (position == 1) {
//B//
//Sub department,chassis//
Intent intent = new Intent(Department.this, ChassisItemActivity.class);
startActivity(intent);
}
if (position == 2) {
//C//
//Sub department,power train//
Intent intent = new Intent(Department.this, PowerTrainActivity.class);
startActivity(intent);
}
if (position == 3) {
//D//
//Sub department, brakes//
Intent intent = new Intent(Department.this, BrakesItemActivity.class);
startActivity(intent);
}
if (position == 4) {
//E//
//sub department,electronics//
Intent intent = new Intent(Department.this, ElectronicsItemActivity.class);
startActivity(intent);
}
if (position == 5) {
//F//
//sub department, miscellaneous//
Intent intent = new Intent(Department.this, MiscellaneousItemActivity.class);
startActivity(intent);
}
}
});
}
}
这是适配器getView
方法
@Override
public View getView(int position, View convertView, ViewGroup parent) {
if(inflater==null){
inflater=(LayoutInflater) context.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
}
if(convertView==null){
convertView = inflater.inflate(R.layout.row, null);
}
TextView itemName=convertView.findViewById(R.id.item_name);
itemName.setText(mItem[position]);
ImageView imageView = convertView.findViewById(R.id.d_image);
imageView.setImageResource(mImage[position]);
TextView cost1 = convertView.findViewById(R.id.cost_value);
cost1.setText(mCost[position]);
return convertView ;
这是日志猫
2020-03-12 15:13:49.032 29730-29730/com.example.android.zfr E/AndroidRuntime: FATAL EXCEPTION: main
Process: com.example.android.zfr, PID: 29730
java.lang.NullPointerException: Attempt to read from null array
at com.example.android.zfr.MainAdapter.getView(MainAdapter.java:56)
at android.widget.AbsListView.obtainView(AbsListView.java:2397)
at android.widget.ListView.makeAndAddView(ListView.java:2052)
at android.widget.ListView.fillDown(ListView.java:786)
at android.widget.ListView.fillFromTop(ListView.java:847)
at android.widget.ListView.layoutChildren(ListView.java:1826)
at android.widget.AbsListView.onLayout(AbsListView.java:2193)
at android.view.View.layout(View.java:19693)
at android.view.ViewGroup.layout(ViewGroup.java:6079)
at android.widget.LinearLayout.setChildFrame(LinearLayout.java:1791)
at android.widget.LinearLayout.layoutVertical(LinearLayout.java:1635)
at android.widget.LinearLayout.onLayout(LinearLayout.java:1544)
at android.view.View.layout(View.java:19693)
at android.view.ViewGroup.layout(ViewGroup.java:6079)
at android.widget.FrameLayout.layoutChildren(FrameLayout.java:323)
at android.widget.FrameLayout.onLayout(FrameLayout.java:261)
at android.view.View.layout(View.java:19693)
at android.view.ViewGroup.layout(ViewGroup.java:6079)
at androidx.appcompat.widget.ActionBarOverlayLayout.onLayout(ActionBarOverlayLayout.java:446)
at android.view.View.layout(View.java:19693)
at android.view.ViewGroup.layout(ViewGroup.java:6079)
at android.widget.FrameLayout.layoutChildren(FrameLayout.java:323)
at android.widget.FrameLayout.onLayout(FrameLayout.java:261)
at android.view.View.layout(View.java:19693)
at android.view.ViewGroup.layout(ViewGroup.java:6079)
at android.widget.LinearLayout.setChildFrame(LinearLayout.java:1791)
at android.widget.LinearLayout.layoutVertical(LinearLayout.java:1635)
at android.widget.LinearLayout.onLayout(LinearLayout.java:1544)
at android.view.View.layout(View.java:19693)
at android.view.ViewGroup.layout(ViewGroup.java:6079)
at android.widget.FrameLayout.layoutChildren(FrameLayout.java:323)
at android.widget.FrameLayout.onLayout(FrameLayout.java:261)
at com.android.internal.policy.DecorView.onLayout(DecorView.java:764)
at android.view.View.layout(View.java:19693)
at android.view.ViewGroup.layout(ViewGroup.java:6079)
at android.view.ViewRootImpl.performLayout(ViewRootImpl.java:2541)
at android.view.ViewRootImpl.performTraversals(ViewRootImpl.java:2257)
at android.view.ViewRootImpl.doTraversal(ViewRootImpl.java:1437)
at android.view.ViewRootImpl$TraversalRunnable.run(ViewRootImpl.java:6886)
at android.view.Choreographer$CallbackRecord.run(Choreographer.java:1029)
at android.view.Choreographer.doCallbacks(Choreographer.java:841)
at android.view.Choreographer.doFrame(Choreographer.java:772)
at android.view.Choreographer$FrameDisplayEventReceiver.run(Choreographer.java:1015)
at android.os.Handler.handleCallback(Handler.java:794)
at android.os.Handler.dispatchMessage(Handler.java:99)
at android.os.Looper.loop(Looper.java:176)
at android.app.ActivityThread.main(ActivityThread.java:6651)
at java.lang.reflect.Method.invoke(Native Method)
at com.android.internal.os.RuntimeInit$MethodAndArgsCaller.run(RuntimeInit.java:547)
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:824)
这个错误是因为你的字符串数组是空的,那是因为它还没有被初始化。
在你的 Department
class
上试试这个
String[] mCost = new String[5];
5
是字符串数组的长度
因此,我尝试在其每个索引处以字符串数组的形式从 firebase 实时数据库传递数据,但我的适配器 class get View 方法显示尝试读取空数组,但我将数据添加到数组。
这是我启动阵列 mCost
的 ACTIVITYpublic class Department extends AppCompatActivity {
ListView listView;
String[] mItem = {"Vehicle Dynamics", "Chassis", "Power Train", "Brakes", "Electronics","Miscellaneous"};
int[] mImage = {R.drawable.vehicledynamics, R.drawable.chassis, R.drawable.powertrain,R.drawable.brakes,
R.drawable.electronics, R.drawable.miscellaneous};
String[] mCost;
int sumofdcost=0;
String sumofdcostvalue;
private DatabaseReference vdDbcost,cDbcost,ptDbcost,bDbcost,eDbcost,mDbcost, dDb,nDb;
private FirebaseDatabase database;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_department);
listView = findViewById(R.id.listView);
Objects.requireNonNull(getSupportActionBar()).setDisplayHomeAsUpEnabled(true);
database=FirebaseDatabase.getInstance();
nDb=database.getReference().child("Navigation Activity").child("Cost");
dDb=database.getReference().child("Department");
vdDbcost=database.getReference().child("Department").child("Vehicle Dynamics").child("Cost");
cDbcost=database.getReference().child("Department").child("Chassis").child("Cost");
ptDbcost=database.getReference().child("Department").child("Brakes").child("Cost");
bDbcost=database.getReference().child("Department").child("Power Train").child("Cost");
eDbcost=database.getReference().child("Department").child("Electronics").child("Cost");
mDbcost=database.getReference().child("Department").child("Miscellaneous").child("Cost");
vdDbcost.addValueEventListener(new ValueEventListener() {
@Override
public void onDataChange(@NonNull DataSnapshot dataSnapshot) {
Long cost=dataSnapshot.getValue(Long.class);
mCost[0]=String.valueOf(cost);
}
@Override
public void onCancelled(@NonNull DatabaseError databaseError) { }
});
cDbcost.addValueEventListener(new ValueEventListener() {
@Override
public void onDataChange(@NonNull DataSnapshot dataSnapshot) {
Long cost=dataSnapshot.getValue(Long.class);
mCost[1]=String.valueOf(cost);
}
@Override
public void onCancelled(@NonNull DatabaseError databaseError) { }
});
ptDbcost.addValueEventListener(new ValueEventListener() {
@Override
public void onDataChange(@NonNull DataSnapshot dataSnapshot) {
Long cost=dataSnapshot.getValue(Long.class);
mCost[2]=String.valueOf(cost);
}
@Override
public void onCancelled(@NonNull DatabaseError databaseError) { }
});
bDbcost.addValueEventListener(new ValueEventListener() {
@Override
public void onDataChange(@NonNull DataSnapshot dataSnapshot) {
Long cost=dataSnapshot.getValue(Long.class);
mCost[3]=String.valueOf(cost);
}
@Override
public void onCancelled(@NonNull DatabaseError databaseError) { }
});
eDbcost.addValueEventListener(new ValueEventListener() {
@Override
public void onDataChange(@NonNull DataSnapshot dataSnapshot) {
Long cost=dataSnapshot.getValue(Long.class);
mCost[4]=String.valueOf(cost);
}
@Override
public void onCancelled(@NonNull DatabaseError databaseError) { }
});
mDbcost.addValueEventListener(new ValueEventListener() {
@Override
public void onDataChange(@NonNull DataSnapshot dataSnapshot) {
Long cost=dataSnapshot.getValue(Long.class);
mCost[5]=String.valueOf(cost);
}
@Override
public void onCancelled(@NonNull DatabaseError databaseError) { }
});
dDb.addValueEventListener(new ValueEventListener() {
@Override
public void onDataChange(@NonNull DataSnapshot dataSnapshot) {
for(DataSnapshot ds: dataSnapshot.getChildren()){
Map<String,Object> map=(Map<String,Object>) ds.getValue();
Object totalcost=map.get("Cost");
int totalcostvalue=Integer.parseInt(String.valueOf(totalcost));
sumofdcost+=totalcostvalue;
sumofdcostvalue=String.valueOf(sumofdcost);
}
nDb.setValue(sumofdcostvalue);
}
@Override
public void onCancelled(@NonNull DatabaseError databaseError) {
}
});
//Creating ADAPTER class
MainAdapter adapter = new MainAdapter(this, mItem, mImage, mCost);
listView.setAdapter(adapter);
listView.setOnItemClickListener(new AdapterView.OnItemClickListener() {
@Override
public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
if (position == 0) {
//A//
//sub department, vehicle dynamics//
Intent intent = new Intent(Department.this, VehicleDynamicsActivity.class);
startActivity(intent);
}
if (position == 1) {
//B//
//Sub department,chassis//
Intent intent = new Intent(Department.this, ChassisItemActivity.class);
startActivity(intent);
}
if (position == 2) {
//C//
//Sub department,power train//
Intent intent = new Intent(Department.this, PowerTrainActivity.class);
startActivity(intent);
}
if (position == 3) {
//D//
//Sub department, brakes//
Intent intent = new Intent(Department.this, BrakesItemActivity.class);
startActivity(intent);
}
if (position == 4) {
//E//
//sub department,electronics//
Intent intent = new Intent(Department.this, ElectronicsItemActivity.class);
startActivity(intent);
}
if (position == 5) {
//F//
//sub department, miscellaneous//
Intent intent = new Intent(Department.this, MiscellaneousItemActivity.class);
startActivity(intent);
}
}
});
}
}
这是适配器getView
方法
@Override
public View getView(int position, View convertView, ViewGroup parent) {
if(inflater==null){
inflater=(LayoutInflater) context.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
}
if(convertView==null){
convertView = inflater.inflate(R.layout.row, null);
}
TextView itemName=convertView.findViewById(R.id.item_name);
itemName.setText(mItem[position]);
ImageView imageView = convertView.findViewById(R.id.d_image);
imageView.setImageResource(mImage[position]);
TextView cost1 = convertView.findViewById(R.id.cost_value);
cost1.setText(mCost[position]);
return convertView ;
这是日志猫
2020-03-12 15:13:49.032 29730-29730/com.example.android.zfr E/AndroidRuntime: FATAL EXCEPTION: main
Process: com.example.android.zfr, PID: 29730
java.lang.NullPointerException: Attempt to read from null array
at com.example.android.zfr.MainAdapter.getView(MainAdapter.java:56)
at android.widget.AbsListView.obtainView(AbsListView.java:2397)
at android.widget.ListView.makeAndAddView(ListView.java:2052)
at android.widget.ListView.fillDown(ListView.java:786)
at android.widget.ListView.fillFromTop(ListView.java:847)
at android.widget.ListView.layoutChildren(ListView.java:1826)
at android.widget.AbsListView.onLayout(AbsListView.java:2193)
at android.view.View.layout(View.java:19693)
at android.view.ViewGroup.layout(ViewGroup.java:6079)
at android.widget.LinearLayout.setChildFrame(LinearLayout.java:1791)
at android.widget.LinearLayout.layoutVertical(LinearLayout.java:1635)
at android.widget.LinearLayout.onLayout(LinearLayout.java:1544)
at android.view.View.layout(View.java:19693)
at android.view.ViewGroup.layout(ViewGroup.java:6079)
at android.widget.FrameLayout.layoutChildren(FrameLayout.java:323)
at android.widget.FrameLayout.onLayout(FrameLayout.java:261)
at android.view.View.layout(View.java:19693)
at android.view.ViewGroup.layout(ViewGroup.java:6079)
at androidx.appcompat.widget.ActionBarOverlayLayout.onLayout(ActionBarOverlayLayout.java:446)
at android.view.View.layout(View.java:19693)
at android.view.ViewGroup.layout(ViewGroup.java:6079)
at android.widget.FrameLayout.layoutChildren(FrameLayout.java:323)
at android.widget.FrameLayout.onLayout(FrameLayout.java:261)
at android.view.View.layout(View.java:19693)
at android.view.ViewGroup.layout(ViewGroup.java:6079)
at android.widget.LinearLayout.setChildFrame(LinearLayout.java:1791)
at android.widget.LinearLayout.layoutVertical(LinearLayout.java:1635)
at android.widget.LinearLayout.onLayout(LinearLayout.java:1544)
at android.view.View.layout(View.java:19693)
at android.view.ViewGroup.layout(ViewGroup.java:6079)
at android.widget.FrameLayout.layoutChildren(FrameLayout.java:323)
at android.widget.FrameLayout.onLayout(FrameLayout.java:261)
at com.android.internal.policy.DecorView.onLayout(DecorView.java:764)
at android.view.View.layout(View.java:19693)
at android.view.ViewGroup.layout(ViewGroup.java:6079)
at android.view.ViewRootImpl.performLayout(ViewRootImpl.java:2541)
at android.view.ViewRootImpl.performTraversals(ViewRootImpl.java:2257)
at android.view.ViewRootImpl.doTraversal(ViewRootImpl.java:1437)
at android.view.ViewRootImpl$TraversalRunnable.run(ViewRootImpl.java:6886)
at android.view.Choreographer$CallbackRecord.run(Choreographer.java:1029)
at android.view.Choreographer.doCallbacks(Choreographer.java:841)
at android.view.Choreographer.doFrame(Choreographer.java:772)
at android.view.Choreographer$FrameDisplayEventReceiver.run(Choreographer.java:1015)
at android.os.Handler.handleCallback(Handler.java:794)
at android.os.Handler.dispatchMessage(Handler.java:99)
at android.os.Looper.loop(Looper.java:176)
at android.app.ActivityThread.main(ActivityThread.java:6651)
at java.lang.reflect.Method.invoke(Native Method)
at com.android.internal.os.RuntimeInit$MethodAndArgsCaller.run(RuntimeInit.java:547)
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:824)
这个错误是因为你的字符串数组是空的,那是因为它还没有被初始化。
在你的 Department
class
String[] mCost = new String[5];
5
是字符串数组的长度