使用AsyncTask将json数据从活动传递到另一个活动

时间:2017-11-19 16:44:18

标签: android json android-asynctask

我正在尝试使用JSON文件并将其数据传递给另一个Activity,因此我将构建一个ListView。但我不知道在哪里设置Intent。这是SplashScreen,我得到了JSON

public class SplashScreen extends AppCompatActivity implements Runnable {

private final  int DELAY = 3000; //3 seconds

private static String url = "https://dl.dropboxusercontent.com/s/d24im9i7e3tczls/carros.json";

ArrayList<HashMap<String, String>> carList;

@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_splash_screen);
    getSupportActionBar().hide();
    //Toast.makeText(getApplicationContext(), "Wait for app loading...", Toast.LENGTH_SHORT).show();
    new GetCars().execute();
    Handler h = new Handler();
    h.postDelayed(this, DELAY);
}

@Override
public void run() {
    new GetCars().execute();
}

private class GetCars extends AsyncTask<Void, Void, Void> {
    Intent it = new Intent(SplashScreen.this, MasterActivity.class);
    Bundle params = new Bundle();

    @Override
    protected Void doInBackground(Void... arg0) {
        HttpHandler sh = new HttpHandler();

        //Do the request and receives answer
        String jsonStr = sh.makeServiceCall(url);

        if(jsonStr != null) {
            try {
                JSONObject jsonObj = new JSONObject(jsonStr);

                JSONArray cars = jsonObj.getJSONArray("carros");

                //running array and getting cars
                for (int i = 0; i < cars.length(); i++) {
                    JSONObject c = cars.getJSONObject(i);

                    String id = c.getString("id");
                    String model = c.getString("modelo");
                    String image = c.getString("foto");
                    String manufacturer = c.getString("fabricante");
                    String year = c.getString("ano");
                    String color = c.getString("cor");
                    String price = c.getString("preco");

                    HashMap<String, String> car = new HashMap<>();

                    car.put("id", id);
                    car.put("model", model);
                    car.put("image", image);
                    car.put("manufacturer", manufacturer);
                    car.put("year", year);
                    car.put("color", color);
                    car.put("price", price);

                    carList.add(car);
                }
                it.putExtra("carList",carList);
                startActivity(it);
                finish();
            } catch (final JSONException e) {
                e.printStackTrace();
                runOnUiThread(new Runnable() {
                    @Override
                    public void run() {
                        Toast.makeText(getApplicationContext(), "JSON Parsing Error: " + e.getMessage(), Toast.LENGTH_LONG).show();
                    }
                });
            }
        } else {
            runOnUiThread(new Runnable() {
                @Override
                public void run() {
                    Toast.makeText(getApplicationContext(), "Couldn't get JSON from server." , Toast.LENGTH_LONG).show();
                }
            });
        }
        return null;
    }
}

使用此代码,当我将carList放入it

时,我收到NullPointerException

1 个答案:

答案 0 :(得分:0)

你得到空指针异常,因为你永远不会初始化carList对象intialize对象然后向它添加数据

更改

ArrayList<HashMap<String, String>> carList;

ArrayList<HashMap<String, String>> carList = new ArrayList<>();