setText 不显示结果 Java Android Studio

问题描述 投票:0回答:2

我在应用程序上显示结果时遇到问题。它正在获取数据,但当我使用 setText 时它不会显示在 textView 上。运行项目时没有错误。它只是在 textView 上没有显示任何内容。是不是onclicklistener工作不正常?

这是代码:

 protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_main);
    inputValue=(EditText)findViewById(R.id.inputValue);
    searchBtn=(Button)findViewById(R.id.searchBtn);
    //gpsBtn=(Button)findViewById(R.id.gpsBtn);
    result = findViewById(R.id.resut);

 searchBtn.setOnClickListener(new View.OnClickListener() {
       @Override
       public void onClick(View v) {
           String value=inputValue.getText().toString();
           String content;
           Weather weather = new Weather();
           try {
               content = weather.execute("https://api.openweathermap.org/data/2.5/weather?q="+ value+"&appid=b4354fdfa8e6e42d96861d10c448094e").get();
               Log.i("content",content);

               JSONObject jsonObject = new JSONObject(content);
               String weatherData = jsonObject.getString("Weather");
               Log.i("weatherData", weatherData);
               JSONArray array = new JSONArray(weatherData);

               String main="";
               String description="";

               for (int i = 0; i<array.length(); i++){
                   JSONObject weatherPart = array.getJSONObject(i);
                   main = weatherPart.getString("main");
                   description= weatherPart.getString("description");
               }

               Log.i("main", main);
               Log.i("description", description);

               String resultText = "Main:"+main+"\nDescription:" +description;

               result.setText(resultText);

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

这是activity_main.xml 文件。我使用了相对布局,不确定这是否与它有任何关系。

<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:app="http://schemas.android.com/apk/res-auto"
    xmlns:tools="http://schemas.android.com/tools"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:gravity="start"
    android:padding="8dp"
    tools:context=".MainActivity">````

    <EditText
        android:id="@+id/inputValue"
        android:layout_width="209dp"
        android:layout_height="wrap_content"
        android:ems="10"
        android:gravity="center"
        android:hint="Enter a City, State or Zip Code"
        android:inputType="textPersonName"
        android:paddingLeft="10dp"
        android:paddingTop="10dp"
        android:paddingRight="10dp"
        android:paddingBottom="10dp"
        android:textAppearance="@style/TextAppearance.AppCompat.Body1" />

    <Button
        android:id="@+id/searchBtn"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_toRightOf="@id/inputValue"
        android:gravity="center"
        android:paddingLeft="5dp"
        android:paddingTop="5dp"
        android:paddingRight="5dp"
        android:paddingBottom="5dp"
        android:text="@string/search" />
    <TextView
        android:id="@+id/textName"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_below="@id/searchBtn"
        android:paddingLeft="5dp"
        android:paddingTop="5dp"
        android:paddingRight="5dp"
        android:paddingBottom="5dp"
        android:text="@string/txtbox" />
    <Button
        android:id="@+id/gpsBtn"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_toRightOf="@id/searchBtn"
        android:gravity="center"
        android:paddingLeft="5dp"
        android:paddingTop="5dp"
        android:paddingRight="5dp"
        android:paddingBottom="5dp"
        android:text="@string/gps" />
    <TextView
        android:id="@+id/textView3"
        android:layout_width="144dp"
        android:layout_height="100dp"
        android:layout_below="@id/textName"
        android:layout_alignParentStart="true"
        android:layout_alignParentLeft="true"
        android:layout_alignParentEnd="true"
        android:layout_alignParentRight="true"
        android:layout_marginStart="120dp"
        android:layout_marginLeft="120dp"
        android:layout_marginTop="22dp"
        android:layout_marginEnd="139dp"
        android:layout_marginRight="139dp"
        android:text="TextView" />


Any help will be appreciated. Thank you!
java android-studio onclicklistener settext
2个回答
2
投票

我看到您正在尝试使用此行设置一个名为 result 的变量:

结果 = findViewById(R.id.resut);

但是我在 XML 中看不到具有该 ID 的 TextView。只需确保您的 xml 中确实存在具有 id 结果的 TextView,我认为这可能是唯一的问题。


0
投票

在 setText 上尝试一下:

result.setText(""+resultText);

© www.soinside.com 2019 - 2024. All rights reserved.