setText() 不显示任何内容


setText() not displaying anything

setText() 没有在我的文本字段中显示任何数据。我也尝试只显示这样的文本:nameView.setText("temp");但这也没有用。因此,我将我的代码粘贴到下面,该代码从PHP脚本获取数据并接收JSON响应。另外,我确实检查过,我收到了 JSON 响应。另外,我已经在清单.xml文件中添加了互联网权限

爪哇文件 :

package com.example.shreyastripathy.fetchtest;
import android.app.Activity;
import android.os.Bundle;
import android.os.StrictMode;
import android.util.Log;
import android.widget.TextView;
import org.apache.http.HttpEntity;
import org.apache.http.HttpResponse;
import org.apache.http.client.HttpClient;
import org.apache.http.client.methods.HttpPost;
import org.apache.http.impl.client.DefaultHttpClient;
import org.json.JSONArray;
import org.json.JSONObject;
import java.io.BufferedReader;
import java.io.InputStream;
import java.io.InputStreamReader;

public class MainActivity extends Activity {
TextView nameView;
TextView ageView;
TextView jobView;
@Override
public void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_main);
    StrictMode.enableDefaults(); //STRICT MODE ENABLED
    nameView = (TextView) findViewById(R.id.nametxt);
    ageView = (TextView) findViewById(R.id.agetxt);
    jobView = (TextView) findViewById(R.id.jobtxt);
    getData();
}
public void getData(){
    String result = "";
    InputStream isr = null;
    try{
        HttpClient httpclient = new DefaultHttpClient();
        HttpPost httppost = new HttpPost("http://192.168.5.240/work/test.php"); // PHP SCRIPT ADDRESS
        HttpResponse response = httpclient.execute(httppost);
        HttpEntity entity = response.getEntity();
        isr = entity.getContent();
    }
    catch(Exception e){
        Log.e("log_tag", "Error in http connection "+e.toString());
        nameView.setText("Couldn't connect to database");
    }
    //convert response to string
    try{
        assert isr != null;
        BufferedReader reader = new BufferedReader(new InputStreamReader(isr,"iso-8859-1"),8);
        StringBuilder sb = new StringBuilder();
        String line;
        while ((line = reader.readLine()) != null) {
            sb.append(line).append("'n");
        }
        isr.close();
        result=sb.toString();
    }
    catch(Exception e){
        Log.e("log_tag", "Error converting result " + e.toString());
    }
    //parse json data
    try {
        String n = "";
        String a="";
        String j="";
        JSONArray jArray = new JSONArray(result);
        for(int i=0; i<jArray.length();i++){
            JSONObject json = jArray.getJSONObject(i);
            n = n + "Name : "+json.getString("FirstName")+" "+json.getString("LastName")+"'n";
            a= a + "Age : "+json.getInt("Age")+"'n";
            j= j + "Job : "+json.getString("Job")+"'n";
        }
        nameView.setText(n);
        ageView.setText(a);
        jobView.setText(j);
    }
    catch (Exception e) {
        Log.e("log_tag", "Error Parsing Data "+e.toString());
    }
}

}

XML 文件 :

<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="wrap_content"
android:layout_height="match_parent"
android:paddingLeft="@dimen/activity_horizontal_margin"
android:paddingRight="@dimen/activity_horizontal_margin"
android:paddingTop="@dimen/activity_vertical_margin"
android:paddingBottom="@dimen/activity_vertical_margin" tools:context=".MainActivity"
android:id="@+id/background">
<LinearLayout
    android:orientation="vertical"
    android:layout_width="fill_parent"
    android:layout_height="fill_parent"
    android:layout_alignParentTop="true"
    android:layout_centerHorizontal="true"
    android:id="@+id/container">
    <TextView
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:id="@+id/nametxt"
        android:layout_gravity="center_horizontal" />
    <TextView
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:id="@+id/agetxt"
        android:layout_gravity="center_horizontal" />
    <TextView
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:id="@+id/jobtxt"
        android:layout_gravity="center_horizontal" />
</LinearLayout>

我该怎么办?

感谢大家的帮助。我终于弄清楚了代码中的问题是什么。我从此处输入的数据库中获取的列名称是错误的。只是一个错字。

我认为你应该在线程中运行getData()。

new Thread(){
    public void run() {
        getData();
   };
}.start();`

您还检查文本颜色。也许文本颜色是相同的背景颜色。