英文:
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("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();
}
}
});
Here is the activity_main.xml file. I used relative layout not sure if that has to do anything with it.
<?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!
</details>
# 答案1
**得分**: 2
我看到你正在尝试使用以下代码行设置一个名为"result"的变量:
result = findViewById(R.id.resut);
但是在XML中我找不到具有该ID的TextView。请确保你的XML中实际存在一个ID为"result"的TextView,我认为这可能是唯一的问题。
<details>
<summary>英文:</summary>
I see that you're trying to set a variable called result with this line:
result = findViewById(R.id.resut);
But I can'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>
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论