gpt4 book ai didi

java - 无法将数据从 firebase 添加到字符串数组

转载 作者:行者123 更新时间:2023-12-02 08:52:57 25 4
gpt4 key购买 nike

因此,我尝试在其每个索引的字符串数组中传递来自 firebase 实时数据库的数据,但我的 Adapter 类的 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 类上尝试一下

String[] mCost = new String[5];

5是字符串数组的长度

关于java - 无法将数据从 firebase 添加到字符串数组,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/60668307/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com