setText不显示结果Java Android Studio

问题描述

我无法在应用程序上显示结果。它正在获取数据,但是当我使用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!

解决方法

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

result = findViewById(R.id.resut);

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

相关问答

Selenium Web驱动程序和Java。元素在(x,y)点处不可单击。其...
Python-如何使用点“。” 访问字典成员?
Java 字符串是不可变的。到底是什么意思?
Java中的“ final”关键字如何工作?(我仍然可以修改对象。...
“loop:”在Java代码中。这是什么,为什么要编译?
java.lang.ClassNotFoundException:sun.jdbc.odbc.JdbcOdbc...