【问题标题】:NullPointer when using asyncTask使用 asyncTask 时的 NullPointer
【发布时间】:2014-05-12 01:58:31
【问题描述】:

嗯,我开始在 Android 中编程,我正在做一个应用程序,它连接到服务器中的 PHP 文件,然后以 JSON 格式读取数据。该应用程序几乎可以正常工作。它从文件中读取 JSON 数据,我可以在“onPostExecute”处理程序中插入带有 JSON 值的文本。

protected void onPostExecute(String string) {
        pDialog.dismiss();
        runOnUiThread(new Runnable() {
            public void run() {
                /**
                 * Updating parsed JSON data into ListView
                 * */

                txt1.setText(jo.toString() + "  " + jo.length());
                try {
                    //Creating an array from the JSON Object
                    jarray = jo.getJSONArray("Anunciante");
                } catch (JSONException e) {
                    // TODO Auto-generated catch block
                    e.printStackTrace();
                }


它可以工作,但是在第一次执行之后,我无法使用 JSONObject 或 JSONArray,因为它们始终为空。

    try {
        if (jarray.equals(null)) {

        } else {

        }
    } catch (Exception e) {
        Toast.makeText(this, "Null Value", Toast.LENGTH_LONG).show();
    }

我不知道问题是什么。

public class Main extends Activity {
public ProgressDialog pDialog;
JSONParser jParser = new JSONParser();
public static String urlPoblaciones = "http://192.168.1.168/Prueba/multitabla.php";
TextView txt1;
public static JSONObject jo;
public static JSONArray jarray;

@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_main);

    txt1 = (TextView) findViewById(R.id.txt1);

    new CargarPoblaciones().execute();

    try {
        if (jarray.equals(null)) {

        } else {

        }
    } catch (Exception e) {
        Toast.makeText(this, "Null Value", Toast.LENGTH_LONG).show();
    }

}

@Override
public boolean onCreateOptionsMenu(Menu menu) {
    // Inflate the menu; this adds items to the action bar if it is present.
    getMenuInflater().inflate(R.menu.main, menu);
    return true;
}

class CargarPoblaciones extends AsyncTask<String, String, String> {

    @Override
    protected void onPreExecute() {
        super.onPreExecute();
        pDialog = new ProgressDialog(Main.this);
        pDialog.setMessage("Cargando poblaciones");
        pDialog.setIndeterminate(false);
        pDialog.setCancelable(false);
        pDialog.show();
        Log.d("Cargando: ", "Aun se esta cargando");
    }

    @Override
    protected String doInBackground(String... params) {
        // TODO Auto-generated method stub
        Log.d("Cargando: ", "Vamos a cargar los productos");
        jo = jParser.makeHttpRequest(urlPoblaciones);
        Log.d("JSON", jo.toString());
        return "Hola";
    }

    @Override
    protected void onPostExecute(String string) {
        pDialog.dismiss();
        runOnUiThread(new Runnable() {
            public void run() {
                /**
                 * Updating parsed JSON data into ListView
                 * */

                txt1.setText(jo.toString() + "  " + jo.length());
                try {
                    //Creating an array from the JSON Object
                    jarray = jo.getJSONArray("Anunciante");
                } catch (JSONException e) {
                    // TODO Auto-generated catch block
                    e.printStackTrace();
                }
            }
        });
    }
}

}

【问题讨论】:

  • onPostExecuted 在 UI 线程上运行

标签: php android json android-asynctask


【解决方案1】:

该数组仅在 onPostExecute 和您在 onCreate 中调用它的方式之后才被填充,响应尚未解析,因此您在那里得到 null。

改变你的代码:

  try {
        if (jarray.equals(null)) {

        } else {

        }
    } catch (Exception e) {
        Toast.makeText(this, "Null Value", Toast.LENGTH_LONG).show();
    }

onPostExecute之后:

 jarray = jo.getJSONArray("Anunciante");  

你已经有一个你关闭的进度条。只需在 onPostExecute 之后调用它,或者在填充 jarray 之后设置一个标志。

【讨论】:

  • 它在其他活动中使用 json 对象工作,非常感谢,你太快了 :D
  • :) ,关于快 - 每个人都会在一段时间后到达那里,你也会的,欢迎,很高兴它有帮助
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2019-08-09
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多