【发布时间】:2023-05-07 21:16:02
【问题描述】:
我无法在我的应用上显示结果。它正在获取数据,但是当我使用 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!
【问题讨论】:
-
请同时发布 xml
-
@UmairMubeen 你好,我编辑并包含了它。谢谢
-
此代码将导致 NullPointerException ,因为您的 xml 中没有任何带有 result id 的 TextView。因此,当您按结果运行 setText 方法时,实际上是在尝试使用等于 null 的引用运行方法
标签: java android-studio onclicklistener settext