java - 应用程序未启动,但可以正确编译
问题描述
我正在使用 YouTube 上的课程在智能手机上制作指南针。
应用程序没有为我打开,我在应用程序控制台中收到错误消息。
我刚开始学习 Android Studio 和 Java,所以我准备发送您的所有评论。
这是代码本身:
package stgamobil.com.compas;
import androidx.appcompat.app.AppCompatActivity;
import android.hardware.Sensor;
import android.hardware.SensorEvent;
import android.hardware.SensorEventListener;
import android.hardware.SensorManager;
import android.os.Bundle;
import android.view.animation.Animation;
import android.view.animation.RotateAnimation;
import android.widget.ImageView;
import android.widget.TextView;
public class MainActivity extends AppCompatActivity implements SensorEventListener {
private ImageView ivDinamic;
private TextView tvDegree;
private float current_degree = 0f;
private SensorManager sensorManager;
Sensor accelerometer;
Sensor magnetometer;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
sensorManager = (SensorManager)getSystemService(SENSOR_SERVICE);
accelerometer = sensorManager.getDefaultSensor(Sensor.TYPE_ACCELEROMETER);
magnetometer = sensorManager.getDefaultSensor(Sensor.TYPE_MAGNETIC_FIELD);
ivDinamic = findViewById(R.id.iveDinamic);
tvDegree = findViewById(R.id.tvDegree);
}
@Override
protected void onResume() {
super.onResume();
sensorManager.registerListener(this, accelerometer, SensorManager.SENSOR_DELAY_UI);
sensorManager.registerListener(this, magnetometer, SensorManager.SENSOR_DELAY_UI);
}
@Override
protected void onPause() {
super.onPause();
sensorManager.unregisterListener(this);
}
@Override
public void onAccuracyChanged(Sensor sensor, int accuracy) {
}
@Override
public void onSensorChanged(SensorEvent sensorEvent) {
float degree = Math.round(0);
SensorEvent event = null;
float[] mGravity= null;
float[] mGeomagnetic = null;
if (event.sensor.getType() == Sensor.TYPE_ACCELEROMETER)
mGravity = event.values;
if (event.sensor.getType() == Sensor.TYPE_MAGNETIC_FIELD)
mGeomagnetic = event.values;
if (mGravity != null && mGeomagnetic != null) {
float R[] = new float[9];
float I[] = new float[9];
boolean success = SensorManager.getRotationMatrix(R, I, mGravity, mGeomagnetic);
if (success) {
float orientation[] = new float[3];
SensorManager.getOrientation(R, orientation);
degree = orientation[0];
Float.toString(degree);
tvDegree.setText("Degrees from north: " + (degree));
}
}
RotateAnimation ra = new RotateAnimation(current_degree, -degree, Animation.RELATIVE_TO_SELF,
0.5f, Animation.RELATIVE_TO_SELF, 0.5f);
ra.setDuration(210);
ra.setFillAfter(true);
ivDinamic.startAnimation(ra);
current_degree = -degree;
}
}
最好点击链接,你可以在那里看到更好 这是我的应用程序控制台
09/30 22:02:42: Launching 'app' on Pixel 3 API 28.
Install successfully finished in 2 s 564 ms.
$ adb shell am start -n "stgamobil.com.compas/stgamobil.com.compas.MainActivity" -a android.intent.action.MAIN -c android.intent.category.LAUNCHER
Connected to process 5209 on device 'emulator-5554'.
Capturing and displaying logcat messages from application. This behavior can be disabled in the "Logcat output" section of the "Debugger" settings page.
W/obil.com.compa: Accessing hidden method Landroid/graphics/drawable/Drawable;->getOpticalInsets()Landroid/graphics/Insets; (light greylist, linking)
Accessing hidden field Landroid/graphics/Insets;->left:I (light greylist, linking)
Accessing hidden field Landroid/graphics/Insets;->right:I (light greylist, linking)
Accessing hidden field Landroid/graphics/Insets;->top:I (light greylist, linking)
Accessing hidden field Landroid/graphics/Insets;->bottom:I (light greylist, linking)
W/obil.com.compa: Accessing hidden field Landroid/view/WindowInsets;->CONSUMED:Landroid/view/WindowInsets; (light greylist, reflection)
W/obil.com.compa: Accessing hidden method Landroid/view/View;->getAccessibilityDelegate()Landroid/view/View$AccessibilityDelegate; (light greylist, linking)
W/obil.com.compa: Accessing hidden method Landroid/view/View;->computeFitSystemWindows(Landroid/graphics/Rect;Landroid/graphics/Rect;)Z (light greylist, reflection)
W/obil.com.compa: Accessing hidden method Landroid/view/ViewGroup;->makeOptionalFitsSystemWindows()V (light greylist, reflection)
D/OpenGLRenderer: HWUI GL Pipeline
E/SensorManager: Exception dispatching input event.
D/AndroidRuntime: Shutting down VM
--------- beginning of crash
E/AndroidRuntime: FATAL EXCEPTION: main
Process: stgamobil.com.compas, PID: 5209
java.lang.NullPointerException: Attempt to read from field 'android.hardware.Sensor android.hardware.SensorEvent.sensor' on a null object reference
at stgamobil.com.compas.MainActivity.onSensorChanged(MainActivity.java:62)
at
android.hardware.SystemSensorManager$SensorEventQueue.dispatchSensorEvent(SystemSensorManager.java:833)
at android.os.MessageQueue.nativePollOnce(Native Method)
at android.os.MessageQueue.next(MessageQueue.java:326)
at android.os.Looper.loop(Looper.java:160)
at android.app.ActivityThread.main(ActivityThread.java:6669)
at java.lang.reflect.Method.invoke(Native Method)
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:858)
I/Process: Sending signal. PID: 5209 SIG: 9
解决方案
如果您专注于您的代码,您会发现您正在onSensorChanged
方法中创建具有空值的事件变量
SensorEvent event = null;
在接下来的几行中,您将在 if 条件下使用它
if (event.sensor.getType() == Sensor.TYPE_ACCELEROMETER)
mGravity = event.values;
但你实际上需要使用sensorEvent
哪个是函数参数,你正在接收
@Override
public void onSensorChanged(SensorEvent sensorEvent){..}
推荐阅读
- python - 如何将 'dtype = object' NumPy 数组转换为 'dtype = float' 数组?
- python - 我正在尝试在我的新项目中添加新产品。但是为什么显示(没有这样的表:main.auth_user__old)
- javascript - 如何在 python 中调用 Javascript 函数?
- jquery - Summernote 不会加载工具栏面板
- timer - 设置一个重复的 24:00:00 倒计时计时器,该计时器在 00:00:00 停止并重置
- python - 我收到一个错误 KeyError Feature not in dictionay
- c# - 返回实例化对象时的空对象?
- c++ - 解析编译错误:没有匹配函数调用 'std::pair<,>::pair()'
- python - Python:numpy.arange 等效函数,具有最小值、最大值和“箱数”而不是“步数”?
- google-cloud-platform - google-cloud-cpp 的 CMake 失败