【问题标题】:Passing json data from an activity to another with AsyncTask使用 AsyncTask 将 json 数据从一个活动传递到另一个活动
【发布时间】:2017-11-19 16:44:17
【问题描述】:

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

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

【问题讨论】:

    标签: android json android-asynctask


    【解决方案1】:

    您得到空指针异常,因为您从未初始化 carList 对象初始化对象然后向其添加数据

    改变

    ArrayList<HashMap<String, String>> carList;
    

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

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 2015-12-31
      • 2020-05-18
      • 1970-01-01
      • 1970-01-01
      • 2013-01-12
      • 2017-12-10
      • 2012-07-05
      相关资源
      最近更新 更多