从JSON检索整数数据并在单独的活动中显示

时间:2019-03-28 12:58:57

标签: java android arrays json

我正在将JSON数据从服务器转换为String并存储在ArrayList中,以便可以在单独的活动页面上显示数据。用我的字符串值,这工作得很好。但是,当我尝试使用整数值时,我的应用程序崩溃并出现致命错误。

这是我将JSON数据转换为字符串并添加到Array的循环。

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);
}

然后我有了一个点击监听器,因此当选择一个对象时,它将带您进入另一活动,其中每辆车都有完整的详细信息。

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());

如您所见,我注释掉了整数值,因为只要不注释掉它们,就会收到致命错误。这是我的日志猫。

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());"

1 个答案:

答案 0 :(得分:1)

您必须先将int的值转换为String,然后再将其设置为TextView

year.setText(String.valueOf(vehicle.getYear()))