java - 单击天气按钮时应用程序不断崩溃
问题描述
我正在尝试单击主课上的“天气”按钮,但是当我单击它时,它会崩溃。主类就像一个空项目,只有几个按钮功能。第二个天气类是可以显示天气的功能。
如何解决问题...谢谢!
日志猫
2020-11-23 00:22:19.342 24196-24196/? I/example.weathe: Not late-enabling -Xcheck:jni (already on)
2020-11-23 00:22:19.435 24196-24196/? W/example.weathe: Unexpected CPU variant for X86 using defaults: x86_64
2020-11-23 00:22:20.020 24196-24196/com.example.weather W/example.weathe: Accessing hidden method Landroid/graphics/drawable/Drawable;->getOpticalInsets()Landroid/graphics/Insets; (light greylist, linking)
2020-11-23 00:22:20.021 24196-24196/com.example.weather W/example.weathe: Accessing hidden field Landroid/graphics/Insets;->left:I (light greylist, linking)
2020-11-23 00:22:20.021 24196-24196/com.example.weather W/example.weathe: Accessing hidden field Landroid/graphics/Insets;->right:I (light greylist, linking)
2020-11-23 00:22:20.021 24196-24196/com.example.weather W/example.weathe: Accessing hidden field Landroid/graphics/Insets;->top:I (light greylist, linking)
2020-11-23 00:22:20.021 24196-24196/com.example.weather W/example.weathe: Accessing hidden field Landroid/graphics/Insets;->bottom:I (light greylist, linking)
2020-11-23 00:22:20.080 24196-24196/com.example.weather W/example.weathe: Accessing hidden field Landroid/view/WindowInsets;->CONSUMED:Landroid/view/WindowInsets; (light greylist, reflection)
2020-11-23 00:22:20.094 24196-24196/com.example.weather W/example.weathe: Accessing hidden method Landroid/view/View;->getAccessibilityDelegate()Landroid/view/View$AccessibilityDelegate; (light greylist, linking)
2020-11-23 00:22:20.154 24196-24196/com.example.weather W/example.weathe: Accessing hidden method Landroid/view/View;->computeFitSystemWindows(Landroid/graphics/Rect;Landroid/graphics/Rect;)Z (light greylist, reflection)
2020-11-23 00:22:20.155 24196-24196/com.example.weather W/example.weathe: Accessing hidden method Landroid/view/ViewGroup;->makeOptionalFitsSystemWindows()V (light greylist, reflection)
2020-11-23 00:22:20.206 24196-24196/com.example.weather D/OpenGLRenderer: Skia GL Pipeline
2020-11-23 00:22:20.244 24196-24196/com.example.weather W/example.weathe: Accessing hidden method Landroid/graphics/Insets;->of(IIII)Landroid/graphics/Insets; (light greylist, linking)
2020-11-23 00:22:20.257 24196-24276/com.example.weather D/HostConnection: HostConnection::get() New Host Connection established 0x729280ed9bc0, tid 24276
2020-11-23 00:22:20.381 24196-24276/com.example.weather D/HostConnection: HostComposition ext ANDROID_EMU_CHECKSUM_HELPER_v1 ANDROID_EMU_native_sync_v2 ANDROID_EMU_native_sync_v3 ANDROID_EMU_native_sync_v4 ANDROID_EMU_dma_v1 ANDROID_EMU_YUV420_888_to_NV21 ANDROID_EMU_YUV_Cache ANDROID_EMU_async_unmap_buffer GL_OES_EGL_image_external_essl3 GL_OES_vertex_array_object GL_KHR_texture_compression_astc_ldr ANDROID_EMU_gles_max_version_3_0
2020-11-23 00:22:20.385 24196-24276/com.example.weather I/ConfigStore: android::hardware::configstore::V1_0::ISurfaceFlingerConfigs::hasWideColorDisplay retrieved: 0
2020-11-23 00:22:20.385 24196-24276/com.example.weather I/ConfigStore: android::hardware::configstore::V1_0::ISurfaceFlingerConfigs::hasHDRDisplay retrieved: 0
2020-11-23 00:22:20.385 24196-24276/com.example.weather I/OpenGLRenderer: Initialized EGL, version 1.4
2020-11-23 00:22:20.385 24196-24276/com.example.weather D/OpenGLRenderer: Swap behavior 1
2020-11-23 00:22:20.385 24196-24276/com.example.weather W/OpenGLRenderer: Failed to choose config with EGL_SWAP_BEHAVIOR_PRESERVED, retrying without...
2020-11-23 00:22:20.385 24196-24276/com.example.weather D/OpenGLRenderer: Swap behavior 0
2020-11-23 00:22:20.389 24196-24276/com.example.weather D/eglCodecCommon: setVertexArrayObject: set vao to 0 (0) 0 0
2020-11-23 00:22:20.389 24196-24276/com.example.weather D/EGL_emulation: eglCreateContext: 0x729280fe3b40: maj 3 min 0 rcv 3
2020-11-23 00:22:20.390 24196-24276/com.example.weather D/EGL_emulation: eglMakeCurrent: 0x729280fe3b40: ver 3 0 (tinfo 0x729280e59ca0)
2020-11-23 00:22:20.409 24196-24276/com.example.weather D/HostConnection: createUnique: call
2020-11-23 00:22:20.409 24196-24276/com.example.weather D/HostConnection: HostConnection::get() New Host Connection established 0x729280fe4040, tid 24276
2020-11-23 00:22:20.442 24196-24276/com.example.weather D/HostConnection: HostComposition ext ANDROID_EMU_CHECKSUM_HELPER_v1 ANDROID_EMU_native_sync_v2 ANDROID_EMU_native_sync_v3 ANDROID_EMU_native_sync_v4 ANDROID_EMU_dma_v1 ANDROID_EMU_YUV420_888_to_NV21 ANDROID_EMU_YUV_Cache ANDROID_EMU_async_unmap_buffer GL_OES_EGL_image_external_essl3 GL_OES_vertex_array_object GL_KHR_texture_compression_astc_ldr ANDROID_EMU_gles_max_version_3_0
2020-11-23 00:22:20.442 24196-24276/com.example.weather E/eglCodecCommon: GoldfishAddressSpaceHostMemoryAllocator: ioctl_ping failed for device_type=5, ret=-1
2020-11-23 00:22:20.449 24196-24276/com.example.weather D/EGL_emulation: eglMakeCurrent: 0x729280fe3b40: ver 3 0 (tinfo 0x729280e59ca0)
2020-11-23 00:22:20.455 24196-24276/com.example.weather D/eglCodecCommon: setVertexArrayObject: set vao to 0 (0) 3 2
2020-11-23 00:22:25.904 24196-24196/com.example.weather W/ActivityThread: handleWindowVisibility: no activity for token android.os.BinderProxy@798fca3
2020-11-23 00:22:25.945 24196-24196/com.example.weather D/AndroidRuntime: Shutting down VM
2020-11-23 00:22:25.946 24196-24196/com.example.weather E/AndroidRuntime: FATAL EXCEPTION: main
Process: com.example.weather, PID: 24196
java.lang.RuntimeException: Unable to start activity ComponentInfo{com.example.weather/com.example.groupproject.weather}: java.lang.NullPointerException: Attempt to invoke virtual method 'void android.widget.ImageView.setOnClickListener(android.view.View$OnClickListener)' on a null object reference
at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2913)
at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:3048)
at android.app.servertransaction.LaunchActivityItem.execute(LaunchActivityItem.java:78)
at android.app.servertransaction.TransactionExecutor.executeCallbacks(TransactionExecutor.java:108)
at android.app.servertransaction.TransactionExecutor.execute(TransactionExecutor.java:68)
at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1808)
at android.os.Handler.dispatchMessage(Handler.java:106)
at android.os.Looper.loop(Looper.java:193)
at android.app.ActivityThread.main(ActivityThread.java:6669)
at java.lang.reflect.Method.invoke(Native Method)
at com.android.internal.os.RuntimeInit$MethodAndArgsCaller.run(RuntimeInit.java:493)
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:858)
Caused by: java.lang.NullPointerException: Attempt to invoke virtual method 'void android.widget.ImageView.setOnClickListener(android.view.View$OnClickListener)' on a null object reference
at com.example.groupproject.weather.onCreate(weather.java:37)
at android.app.Activity.performCreate(Activity.java:7136)
at android.app.Activity.performCreate(Activity.java:7127)
at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1271)
at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2893)
at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:3048)
at android.app.servertransaction.LaunchActivityItem.execute(LaunchActivityItem.java:78)
at android.app.servertransaction.TransactionExecutor.executeCallbacks(TransactionExecutor.java:108)
at android.app.servertransaction.TransactionExecutor.execute(TransactionExecutor.java:68)
at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1808)
at android.os.Handler.dispatchMessage(Handler.java:106)
at android.os.Looper.loop(Looper.java:193)
at android.app.ActivityThread.main(ActivityThread.java:6669)
at java.lang.reflect.Method.invoke(Native Method)
at com.android.internal.os.RuntimeInit$MethodAndArgsCaller.run(RuntimeInit.java:493)
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:858)
2020-11-23 00:22:25.962 24196-24196/com.example.weather I/Process: Sending signal. PID: 24196 SIG: 9
main.java
package com.example.groupproject;
import androidx.appcompat.app.AppCompatActivity;
import android.content.Intent;
import android.os.Bundle;
import android.view.View;
import android.widget.Button;
public class MainActivity extends AppCompatActivity {
Button btn;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
btn = findViewById(R.id.button);
btn.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
Intent intent = new Intent(MainActivity.this, weather.class);
startActivity(intent);
}
});
}
}
主.xml
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout 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"
tools:context=".MainActivity">
<Button
android:id="@+id/button"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginLeft="150dp"
android:layout_marginTop="64dp"
android:text="Weather"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintHorizontal_bias="0.498"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toTopOf="parent" />
</LinearLayout>
天气.java
package com.example.groupproject;
import android.os.Bundle;
import android.view.View;
import android.widget.EditText;
import android.widget.ImageView;
import android.widget.TextView;
import androidx.appcompat.app.AppCompatActivity;
import com.example.groupproject.Retrofit.ApiInterface;
import com.example.groupproject.Retrofit.ApiClient;
import com.example.groupproject.Retrofit.Example;
import retrofit2.Call;
import retrofit2.Callback;
import retrofit2.Response;
public class weather extends AppCompatActivity {
ImageView search;
TextView tempText, descText, humidityText;
EditText textField;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
search = findViewById(R.id.search);
tempText = findViewById(R.id.tempText);
descText = findViewById(R.id.descText);
humidityText = findViewById(R.id.humidityText);
textField = findViewById(R.id.textField);
search.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
//Here will call the API
getWeatherData(textField.getText().toString().trim());
}
});
}
private void getWeatherData(String name) {
ApiInterface apiInterface = ApiClient.getClient().create(ApiInterface.class);
Call<Example> call = apiInterface.getWeatherData(name);
call.enqueue(new Callback<Example>() {
@Override
public void onResponse(Call<Example> call, Response<Example> response) {
tempText.setText("Temp" + " " + response.body().getMain().getTemp() + "°C");
descText.setText("Feel Like" + " " + response.body().getMain().getFeels_like() + "°C");
humidityText.setText("Humidity" + " " + response.body().getMain().getHumidity() + "°C");
}
@Override
public void onFailure(Call<Example> call, Throwable t) {
}
});
}
}
天气.xml
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout 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"
tools:context=".MainActivity"
android:orientation="vertical"
android:background = "@drawable/background">
<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:orientation="horizontal"
android:padding="10dp"
android:weightSum="3">
<EditText
android:layout_width="0dp"
android:layout_height="wrap_content"
android:layout_weight="2.5"
android:textColor="#000"
android:textSize="16dp"
android:background="#fff"
android:padding="10dp"
android:gravity="center"
android:layout_gravity="center"
android:id="@+id/textField"/>
<ImageView
android:layout_width="0dp"
android:layout_height="wrap_content"
android:layout_weight="0.5"
android:src="@drawable/search"
android:id="@+id/search"/>
</LinearLayout>
<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:orientation="vertical"
android:padding="10dp">
<TextView
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:text="Text Temp"
android:textColor="#fff"
android:textSize="50dp"
android:id="@+id/tempText"
/>
<TextView
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:text="Description"
android:textColor="#fff"
android:textSize="25dp"
android:id="@+id/descText"
/>
<TextView
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:text="Humidity"
android:textColor="#fff"
android:textSize="25dp"
android:id="@+id/humidityText"
/>
</LinearLayout>
</LinearLayout>
解决方案
In weather Activity you are setting content view of activity_main.xml instead of weather.xml
In weather Activity change this line
setContentView(R.layout.activity_main);
to
setContentView(R.layout.weather);
推荐阅读
- php - 是否需要在本地安装 MongoDB 才能将 PHP 项目与远程 MongoDB 服务器连接起来?
- php - PHP 从外部的匿名函数回调内部访问变量?
- android-studio - 我可以在 Android Studio 3.6.2 中自定义 simpleUML 吗?
- php - 如何修复错误的方法调用“方法 App\Http\Controllers\TransactionsController::Transfer 不存在。” 在 laravel 中
- linux - Azure 发布管道发布到 NetCore 3.1 linux 应用服务失败,退出代码为 145 - 日志中“无法找到任何 .NET Core SDK”
- python - 如何将本地图像从 Python 上传到 Firebase?
- c++ - 制作一个使用静态库的动态库,但使用主程序中的函数
- java - 构建和运行 JavaFX .jar 文件时出现问题
- c++ - const char* 在哪里获得指向内存地址的指针?
- excel - 通过将活动单元格的范围提供给列的最后一行来显示自动填充命令范围中的错误