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

brvekthn  于 2021-06-25  发布在  Mysql
关注(0)|答案(2)|浏览(255)

我是android新手。在我的应用程序中,我想在不同的文本视图中设置从mysql数据库检索到的数据。我尝试了很多方法,但都做不到。总是出现错误。我也尝试过。请帮助我。我想在文本视图中设置该数据。
我从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;
    }

}

error:e/androidruntime:致命异常:主进程:com.example.arpan.carrental,pid:15029 java.lang.nullpointerexception:尝试对com.example.arpan.carrental.carview$1.onresponse(carview)上的空对象引用调用虚拟方法“void android.widget.textview.settext(java.lang.charsequence)”。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);
}
?>

我想所有的数据来自数据库和显示在不同的文本视图。

ckocjqey

ckocjqey1#

您在后台线程中更改了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();
            }

}
                }
lokaqttq

lokaqttq2#

在使用文本视图之前,不能初始化它。

brand = findViewById(R.id.textView);

在将文本设置为textview之前,还要检查文本是否为null。
如果您处理的是json,那么可以将其作为

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

相关问题