setText方法未在Java Android Studio中显示结果

huangapple go评论78阅读模式
英文:

setText not displaying results Java Android Studio

问题

以下是代码部分的翻译:

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);
    result = findViewById(R.id.result);

    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();
            }
        }
    });
}
<?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/resut"
        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/resut"
        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" />

</RelativeLayout>

希望对你有所帮助。如有其他问题,请随时问我。

英文:

I am having trouble with displaying the results on my app. It's getting the data, but it just does not display on the textView when i use setText.There's no error when I ran the project. It's just not showing anything on the textView. Is the onclicklistener not working properly?

here is the code:

 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(&quot;https://api.openweathermap.org/data/2.5/weather?q=&quot;+ value+&quot;&amp;appid=b4354fdfa8e6e42d96861d10c448094e&quot;).get();
Log.i(&quot;content&quot;,content);
JSONObject jsonObject = new JSONObject(content);
String weatherData = jsonObject.getString(&quot;Weather&quot;);
Log.i(&quot;weatherData&quot;, weatherData);
JSONArray array = new JSONArray(weatherData);
String main=&quot;&quot;;
String description=&quot;&quot;;
for (int i = 0; i&lt;array.length(); i++){
JSONObject weatherPart = array.getJSONObject(i);
main = weatherPart.getString(&quot;main&quot;);
description= weatherPart.getString(&quot;description&quot;);
}
Log.i(&quot;main&quot;, main);
Log.i(&quot;description&quot;, description);
String resultText = &quot;Main:&quot;+main+&quot;\nDescription:&quot; +description;
result.setText(resultText);
} catch (Exception e) {
e.printStackTrace();
}
}
});

Here is the activity_main.xml file. I used relative layout not sure if that has to do anything with it.

&lt;?xml version=&quot;1.0&quot; encoding=&quot;utf-8&quot;?&gt;
&lt;RelativeLayout xmlns:android=&quot;http://schemas.android.com/apk/res/android&quot;
xmlns:app=&quot;http://schemas.android.com/apk/res-auto&quot;
xmlns:tools=&quot;http://schemas.android.com/tools&quot;
android:layout_width=&quot;match_parent&quot;
android:layout_height=&quot;match_parent&quot;
android:gravity=&quot;start&quot;
android:padding=&quot;8dp&quot;
tools:context=&quot;.MainActivity&quot;&gt;````
&lt;EditText
android:id=&quot;@+id/inputValue&quot;
android:layout_width=&quot;209dp&quot;
android:layout_height=&quot;wrap_content&quot;
android:ems=&quot;10&quot;
android:gravity=&quot;center&quot;
android:hint=&quot;Enter a City, State or Zip Code&quot;
android:inputType=&quot;textPersonName&quot;
android:paddingLeft=&quot;10dp&quot;
android:paddingTop=&quot;10dp&quot;
android:paddingRight=&quot;10dp&quot;
android:paddingBottom=&quot;10dp&quot;
android:textAppearance=&quot;@style/TextAppearance.AppCompat.Body1&quot; /&gt;
&lt;Button
android:id=&quot;@+id/searchBtn&quot;
android:layout_width=&quot;wrap_content&quot;
android:layout_height=&quot;wrap_content&quot;
android:layout_toRightOf=&quot;@id/inputValue&quot;
android:gravity=&quot;center&quot;
android:paddingLeft=&quot;5dp&quot;
android:paddingTop=&quot;5dp&quot;
android:paddingRight=&quot;5dp&quot;
android:paddingBottom=&quot;5dp&quot;
android:text=&quot;@string/search&quot; /&gt;
&lt;TextView
android:id=&quot;@+id/textName&quot;
android:layout_width=&quot;wrap_content&quot;
android:layout_height=&quot;wrap_content&quot;
android:layout_below=&quot;@id/searchBtn&quot;
android:paddingLeft=&quot;5dp&quot;
android:paddingTop=&quot;5dp&quot;
android:paddingRight=&quot;5dp&quot;
android:paddingBottom=&quot;5dp&quot;
android:text=&quot;@string/txtbox&quot; /&gt;
&lt;Button
android:id=&quot;@+id/gpsBtn&quot;
android:layout_width=&quot;wrap_content&quot;
android:layout_height=&quot;wrap_content&quot;
android:layout_toRightOf=&quot;@id/searchBtn&quot;
android:gravity=&quot;center&quot;
android:paddingLeft=&quot;5dp&quot;
android:paddingTop=&quot;5dp&quot;
android:paddingRight=&quot;5dp&quot;
android:paddingBottom=&quot;5dp&quot;
android:text=&quot;@string/gps&quot; /&gt;
&lt;TextView
android:id=&quot;@+id/textView3&quot;
android:layout_width=&quot;144dp&quot;
android:layout_height=&quot;100dp&quot;
android:layout_below=&quot;@id/textName&quot;
android:layout_alignParentStart=&quot;true&quot;
android:layout_alignParentLeft=&quot;true&quot;
android:layout_alignParentEnd=&quot;true&quot;
android:layout_alignParentRight=&quot;true&quot;
android:layout_marginStart=&quot;120dp&quot;
android:layout_marginLeft=&quot;120dp&quot;
android:layout_marginTop=&quot;22dp&quot;
android:layout_marginEnd=&quot;139dp&quot;
android:layout_marginRight=&quot;139dp&quot;
android:text=&quot;TextView&quot; /&gt;
Any help will be appreciated. Thank you!
</details>
# 答案1
**得分**: 2
我看到你正在尝试使用以下代码行设置一个名为"result"的变量:
result = findViewById(R.id.resut);
但是在XML中我找不到具有该ID的TextView。请确保你的XML中实际存在一个ID为"result"的TextView,我认为这可能是唯一的问题。
<details>
<summary>英文:</summary>
I see that you&#39;re trying to set a variable called result with this line:
result = findViewById(R.id.resut);
But I can&#39;t see a TextView with that ID in the XML. Just make sure that a TextView with the id result actually exists in your xml, I think that might be the only issue.
</details>

huangapple
  • 本文由 发表于 2020年10月25日 05:41:44
  • 转载请务必保留本文链接:https://go.coder-hub.com/64518264.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定