无法在文本视图中设置检索到的数据

时间:2018-04-18 10:02:16

标签: php android mysql

我是android中的新手。在我的应用程序中,我想在不同的textview中设置从mysql数据库中检索的数据。我尝试了很多方法,但我无法做到。总是出错。I also tried that.请帮助我想在textview中设置这些数据。

我从onCreate方法

调用此类
/*ERROR LINE*/private class GetData extends AsyncTask<String, String, String> {
    public Context context;

    public GetData(Context context) {
    }

    @Override
    protected String doInBackground(String... params) {
        HttpClient myClient = new DefaultHttpClient();
        HttpPost myConnection = new HttpPost("http://www.mybusket.com/webapi/carrental/car/get_car_details.php?vcarid="+Id); //this Id is a int variable

        try {
            response = myClient.execute(myConnection);
            str = EntityUtils.toString(response.getEntity(), "UTF-8");
        } catch (ClientProtocolException e) {
            e.printStackTrace();
        }
            catch (IOException e) {
            e.printStackTrace();
        }
        try {
            JSONObject obj = new JSONObject(str);
            JSONArray jsonArray = obj.getJSONArray("car");

                JSONObject json=jsonArray.getJSONObject(0);
                brand.setText(json.getString("brand"));//ERROR LINE
                fuel.setText(json.getString("fueltype"));
                carno.setText(json.getString("carno"));
                seat.setText(json.getString("seat"));
                feature.setText(json.getString("feature"));

        } catch (JSONException e) {
            e.printStackTrace();
        }

        return null;
    }

}
  

错误:E / AndroidRuntime:致命异常:主要       过程:com.example.arpan.carrental,PID:15029       java.lang.NullPointerException:尝试调用虚方法&#39; void android.widget.TextView.setText(java.lang.CharSequence)&#39;在null对象引用上           在com.example.arpan.carrental.CarView $ 1.onResponse(CarView.java:130)           在com.example.arpan.carrental.CarView $ 1.onResponse(CarView.java:115)

这是我的php

  <?php
  $response = array();

  // include db connect class

  require_once("../connectdb.php");
  // connecting to db
  $db = new DB_CONNECT();
  // check for post data
 if (isset($_GET["carid"])) {
 $carid = $_GET['carid'];
 // check for post data

 if (isset($_GET["carid"])) {
 $carid = $_GET['carid'];
 $result = mysql_query("SELECT * FROM carinfo WHERE carid = $carid");

if (!empty($result)) {
    // check for empty result
    if (mysql_num_rows($result) > 0) {

        $row = mysql_fetch_array($result);

        $car = array();
        $car["brand"] = $row["brand"]; 
        $car["fueltype"] = $row["fueltype"]; 
        $car["carno"] = $row["carno"]; 
        $car["seat"] = $row["seat"]; 
        $car["feature"] = $row["feature"];      
        // success
        $response["success"] = 1;

        // user node
        $response["car"] = array();

        array_push($response["car"], $car);

        // echoing JSON response
        echo json_encode($response);
    } else {
        // no employer found
        $response["success"] = 0;
        $response["message"] = "No Car found";

        // echo no users JSON
        echo json_encode($response);
    }
} else {
    // no employer found
    $response["success"] = 0;
    $response["message"] = "No Car found";

    // echo no users JSON
    echo json_encode($response);
}
} else {
// required field is missing
$response["success"] = 0;
$response["message"] = "Required field(s) is missing";
// echoing JSON response
echo json_encode($response);
}
?>

我希望所有数据都来自数据库,并以不同的文本视图显示。

2 个答案:

答案 0 :(得分:0)

在使用之前,您可能无法启动文本视图。

brand = findViewById(R.id.textView);

并在将文本设置为textview之前检查文本是否为null。

如果您正在处理json,可以将其作为

进行测试
if(json.has("brand")){
           //brand fild is availble in json
          //set it to brand textview 
        }

答案 1 :(得分:0)

您在后台线程中更改UI,这可能是问题的原因。 ,应用此代码。假设您已初始化Textview

 @Override
        protected String doInBackground(String... params) {
            HttpClient myClient = new DefaultHttpClient();
            HttpPost myConnection = new HttpPost("url");  

            try {
                response = myClient.execute(myConnection);
                str = EntityUtils.toString(response.getEntity(), "UTF-8");
            } catch (ClientProtocolException e) {
                e.printStackTrace();
            }
                catch (IOException e) {
                e.printStackTrace();
            }

            return str ;
        }


      @Override
       protected void onPostExecute(String result) {
              if(str!=null)
              {
                try {
                JSONObject obj = new JSONObject(str);
                JSONArray jsonArray = obj.getJSONArray("car");
                if(jsonArray.length()>0)
                  {
                    JSONObject json=jsonArray.getJSONObject(0);
                    brand.setText(json.getString("brand"));
                    fuel.setText(json.getString("fueltype"));
                    carno.setText(json.getString("carno"));
                    seat.setText(json.getString("seat"));
                    feature.setText(json.getString("feature"));
    }
            } catch (JSONException e) {
                e.printStackTrace();
            }

}
                }