微信公众号搜"智元新知"关注
微信扫一扫可直接关注哦!

在搜索城市之前使显示数据文本视图不可见

如何解决在搜索城市之前使显示数据文本视图不可见

我在我的天气应用程序上设置了我的文本视图,以在我的搜索面板上搜索显示城市的数据(即 temp_out.setText(response.body().getMain().getTemp() + " ℃"););。它工作得很好,只是用于显示这些数据的文本视图仍然反映在应用程序上(即temp_out)。我想以这样的方式设置它,直到搜索城市时,文本视图才会不可见,然后这样一个城市的数据将显示在文本视图部分。

这些是我正在努力实现的目标的说明:

当应用程序打开时,这应该是不可见的(带有红色勾号的部分):

enter image description here

然后在网上搜索一个城市后应该显示这个数据(带红色勾号的部分):

enter image description here

到目前为止,我已经尝试使用此代码 findViewById(R.id.textView9).setVisibility(View.GONE);在我的一个文本视图 (time_field) 的活动中。

搜索城市之前和之后完全不可见,也给出了这个例外:

java.lang.NullPointerException: Attempt to invoke virtual method 'void android.widget.TextView.setText(java.lang.CharSequence)' on a null object reference
        at com.viz.lightweatherforecast.Activity.HomeActivity$2$1.onResponse(HomeActivity.java:112)
        at retrofit2.DefaultCallAdapterFactory$ExecutorCallbackCall$1.lambda$onResponse$0$DefaultCallAdapterFactory$ExecutorCallbackCall$1(DefaultCallAdapterFactory.java:89)
        at retrofit2.-$$Lambda$DefaultCallAdapterFactory$ExecutorCallbackCall$1$3wC8FyV4pyjrzrYL5U0mlYiviZw.run(UnkNown Source:6)
        at android.os.Handler.handleCallback(Handler.java:873)
        at android.os.Handler.dispatchMessage(Handler.java:99)
        at android.os.Looper.loop(Looper.java:193)
        at android.app.ActivityThread.main(ActivityThread.java:6819)
        at java.lang.reflect.Method.invoke(Native Method)
        at com.android.internal.os.RuntimeInit$MethodAndArgsCaller.run(RuntimeInit.java:497)
        at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:912)

然后我尝试在 layout.xml android:visibility="gone"android:visibility="invisible" 上使用它来实现相同的文本视图。在搜索城市之前和之后也同样看不见,但也不例外。

我别无选择,只能问问推荐的做法是什么?

以下是用于显示数据的文本视图:

这是给我的片段:

public void onResponse(@NonNull Call<Example> call,@NonNull Response<Example> response) {

                try {
                    assert response.body() != null;
                    current_temp.setText(response.body().getMain().getTemp() + " ℃");
                    current_output.setText(response.body().getWeather().get(0).getDescription());
                    rise_time.setText(response.body().getSys().getSunrise() + " ");
                    set_time.setText(response.body().getSys().getSunset() + " ");
                    temp_out.setText(response.body().getMain().getTemp() + " ℃");
                    Press_out.setText(response.body().getMain().getPressure() + " hpa");
                    Humid_out.setText(response.body().getMain().getHumidity() + " %");
                    Ws_out.setText(response.body().getwind().getSpeed() + " Km/h");
                    Visi_out.setText(response.body().getVisibility() + " m");
                    Cloud_out.setText(response.body().getClouds().getAll() + " %");
                } catch (Exception e) {
                    Log.e("TAG","No City found");
                    Toast.makeText(getActivity(),"No City found",Toast.LENGTH_SHORT).show();
                }

            }

这是我的活动:

public void onResponse(@NonNull Call<Example> call,@NonNull Response<Example> response) {

                try {
                    assert response.body() != null;
                    time_field.setText("Last Updated:" + " " + response.body().getDt());
                } catch (Exception e) {
                    time_field.setText("Last Updated: UnkNown");
                    Log.e("TAG","No City found");
                    Toast.makeText(HomeActivity.this,Toast.LENGTH_SHORT).show();
                }
            }

我不知道是否需要发布完整代码,因为我正在尽最大努力遵循 https://stackoverflow.com/help/minimal-reproducible-example,但是如果 需要,请告诉我。

解决方法

XML 文件中添加 android:visibility="gone" 和你的 java 类

public void onResponse(@NonNull Call<Example> call,@NonNull Response<Example> response) {

                try {
                    assert response.body() != null;
                    current_temp.setVisibility(View.VISIBLE);
                    current_temp.setText(response.body().getMain().getTemp() + " ℃");
//all other stuffs


                } catch (Exception e) {
                    Log.e("TAG","No City found");
                    current_temp.setVisibility(View.GONE);
                    Toast.makeText(getActivity(),"No City found",Toast.LENGTH_SHORT).show();
                }

            }

版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 dio@foxmail.com 举报,一经查实,本站将立刻删除。