【发布时间】:2019-03-28 12:58:56
【问题描述】:
我正在将 JSON 数据从服务器转换为字符串并存储在 ArrayList 中,以便可以在单独的活动页面上显示数据。使用我的字符串值,这工作得很好。但是,当我尝试使用整数值时,我的应用程序崩溃并出现致命错误。
这是我将 JSON 数据转换为字符串并添加到数组的循环。
for (int i=0; i < jsonArray.length(); i++) {
String make = jsonArray.getJSONObject(i).get("make").toString();
String model = jsonArray.getJSONObject(i).get("model").toString();
String reg = jsonArray.getJSONObject(i).get("license_number").toString();
int year = Integer.parseInt(jsonArray.getJSONObject(i).get("year").toString());
int price = Integer.parseInt(jsonArray.getJSONObject(i).get("price").toString());
String colour = jsonArray.getJSONObject(i).get("colour").toString();
int number_doors = Integer.parseInt(jsonArray.getJSONObject(i).get("number_doors").toString());
String transmission = jsonArray.getJSONObject(i).get("transmission").toString();
int mileage = Integer.parseInt(jsonArray.getJSONObject(i).get("mileage").toString());
String fuel_type = jsonArray.getJSONObject(i).get("fuel_type").toString();
int engine_size = Integer.parseInt(jsonArray.getJSONObject(i).get("engine_size").toString());
String body_style = jsonArray.getJSONObject(i).get("body_style").toString();
String condition = jsonArray.getJSONObject(i).get("condition").toString();
String notes = jsonArray.getJSONObject(i).get("notes").toString();
Vehicle V = new Vehicle(make,model, year, price, reg, colour, number_doors, transmission, mileage, fuel_type,engine_size, body_style, condition, notes);
vehicleArrayList.add(V);
}
I then have an on click listener so when an object is selected it will take you to another activity with fully populated details on each vehicle.
vehicleList.setOnItemClickListener(new AdapterView.OnItemClickListener() {
@Override
public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
Intent intent = new Intent(getApplicationContext(), DetailsActivity.class);
intent.putExtra("license_number", vehicleArrayList.get(position));
startActivity(intent);
}
});
这是我的 DetailsActivity.class 中的代码,点击一个对象应该带你去。
Bundle extras = getIntent().getExtras();
Vehicle vehicle = (Vehicle) extras.get("license_number");
System.out.println("received from the intent: " + vehicle.getLicense_number());
TextView reg = findViewById(R.id.vecReg);
TextView make = findViewById(R.id.make);
TextView model = findViewById(R.id.model);
// TextView year = findViewById(R.id.year);
// TextView price = findViewById(R.id.price2);
TextView colour = findViewById(R.id.colour2);
TextView transmission = findViewById(R.id.transmission2);
// TextView mileage = findViewById(R.id.mileage2);
TextView fuel = findViewById(R.id.fuel2);
// TextView engine = findViewById(R.id.engine2);
// TextView doors = findViewById(R.id.doors2);
TextView body = findViewById(R.id.body2);
TextView condition = findViewById(R.id.condition2);
TextView notes = findViewById(R.id.notes2);
reg.setText(vehicle.getLicense_number());
make.setText(vehicle.getMake());
model.setText(vehicle.getModel());
// year.setText(vehicle.getYear());
// price.setText(vehicle.getPrice());
colour.setText(vehicle.getColour());
transmission.setText(vehicle.getTransmission());
// mileage.setText(vehicle.getMileage());
fuel.setText(vehicle.getFuel_type());
// engine.setText(vehicle.getEngine_size());
// doors.setText(vehicle.getNumber_doors());
body.setText(vehicle.getBody_style());
condition.setText(vehicle.getCondition());
notes.setText(vehicle.getNotes());
如您所见,我已经注释掉了整数值,因为只要我没有注释掉它们,我就会收到致命错误。这是来自我的 logcat。
03-28 12:40:56.959 16172-16172/com.example.vehicledatabaseapp E/AndroidRuntime: FATAL EXCEPTION: main
Process: com.example.vehicledatabaseapp, PID: 16172
java.lang.RuntimeException: Unable to start activity ComponentInfo{com.example.vehicledatabaseapp/com.example.vehicledatabaseapp.DetailsActivity}: android.content.res.Resources$NotFoundException: String resource ID #0x7df
at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2325)
at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2387)
at android.app.ActivityThread.access$800(ActivityThread.java:151)
at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1303)
at android.os.Handler.dispatchMessage(Handler.java:102)
at android.os.Looper.loop(Looper.java:135)
at android.app.ActivityThread.main(ActivityThread.java:5254)
at java.lang.reflect.Method.invoke(Native Method)
at java.lang.reflect.Method.invoke(Method.java:372)
at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:903)
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:698)
Caused by: android.content.res.Resources$NotFoundException: String resource ID #0x7df
at android.content.res.Resources.getText(Resources.java:299)
at android.widget.TextView.setText(TextView.java:4132)
at com.example.vehicledatabaseapp.DetailsActivity.onCreate(DetailsActivity.java:42)
at android.app.Activity.performCreate(Activity.java:5990)
at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1106)
at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2278)
at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2387)
at android.app.ActivityThread.access$800(ActivityThread.java:151)
at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1303)
at android.os.Handler.dispatchMessage(Handler.java:102)
at android.os.Looper.loop(Looper.java:135)
at android.app.ActivityThread.main(ActivityThread.java:5254)
at java.lang.reflect.Method.invoke(Native Method)
at java.lang.reflect.Method.invoke(Method.java:372)
at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:903)
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:698)
03-28 12:40:59.856 16172-16172/com.example.vehicledatabaseapp I/Process: Sending signal. PID: 16172 SIG: 9
"com.example.vehicledatabaseapp.DetailsActivity.onCreate(DetailsActivity.java:42)" 行是指"year.setText(vehicle.getYear());" 行
【问题讨论】:
-
在 xml 布局文件中检查年份 textview 的 id