没有编译错误,但 android 应用未在设备上启动
no compilation error but android app not launching on device
我对 android 编程很陌生
今天我编写了一个应用程序来访问加速度计,从此处提供在线示例1 & 2
我已经完成了我的程序,但问题是应用程序没有启动。
我认为我的程序有错误但找不到它
这是代码
import android.hardware.Sensor;
import android.hardware.SensorEvent;
import android.hardware.SensorListener;
import android.hardware.SensorManager;
import android.hardware.SensorEventListener;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.view.Gravity;
import android.widget.TextView;
import static android.content.Context.SENSOR_SERVICE;
public class MainActivity extends AppCompatActivity{
public SensorManager msen;
public Sensor sensor;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
msen = (SensorManager) getSystemService(SENSOR_SERVICE);
if (msen != null) {
sensor=msen.getDefaultSensor(Sensor.TYPE_ACCELEROMETER);
}
}
protected void onResume() {
super.onResume();
msen.registerListener((SensorEventListener) this,sensor, SensorManager.SENSOR_DELAY_NORMAL);
}
public final void onSensorChanged(SensorEvent event) {
float x = event.values[0];
float y=event.values[1];
float z=event.values[2];
float ans=x*x+y*y+z*z;
ans= (float) Math.sqrt(ans);
TextView value;
value=(TextView) findViewById(R.id.val);
value.setText((int) ans);
value.setHeight(100);
value.setWidth(100);
value.setGravity(Gravity.CENTER);
}
@Override
protected void onPause() {
super.onPause();
msen.unregisterListener((SensorEventListener) this);
}
}
我得到的输出是
不幸的是速度已经停止
错误日志
E/AndroidRuntime: FATAL EXCEPTION: main
Process: com.example.jasinthpremkumar.speed, PID: 13210
java.lang.RuntimeException: Unable to resume activity {com.example.jasinthpremkumar.speed/com.example.jasinthpremkumar.speed.MainActivity}: java.lang.ClassCastException: com.example.jasinthpremkumar.speed.MainActivity cannot be cast to android.hardware.SensorEventListener
at android.app.ActivityThread.performResumeActivity(ActivityThread.java:2974)
at android.app.ActivityThread.handleResumeActivity(ActivityThread.java:3005)
at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2371)
at android.app.ActivityThread.access0(ActivityThread.java:149)
at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1284)
at android.os.Handler.dispatchMessage(Handler.java:102)
at android.os.Looper.loop(Looper.java:135)
at android.app.ActivityThread.main(ActivityThread.java:5290)
at java.lang.reflect.Method.invoke(Native Method)
at java.lang.reflect.Method.invoke(Method.java:372)
at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:908)
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:703)
Caused by: java.lang.ClassCastException: com.example.jasinthpremkumar.speed.MainActivity cannot be cast to android.hardware.SensorEventListener
at com.example.jasinthpremkumar.speed.MainActivity.onResume(MainActivity.java:32)
at android.app.Instrumentation.callActivityOnResume(Instrumentation.java:1241)
at android.app.Activity.performResume(Activity.java:6106)
at android.app.ActivityThread.performResumeActivity(ActivityThread.java:2963)
at android.app.ActivityThread.handleResumeActivity(ActivityThread.java:3005)
at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2371)
at android.app.ActivityThread.access0(ActivityThread.java:149)
at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1284)
at android.os.Handler.dispatchMessage(Handler.java:102)
at android.os.Looper.loop(Looper.java:135)
at android.app.ActivityThread.main(ActivityThread.java:5290)
at java.lang.reflect.Method.invoke(Native Method)
at java.lang.reflect.Method.invoke(Method.java:372)
at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:908)
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:703)
Application terminated.
SensorEventListener
是一个 interface
并且您不能将 Activity
转换为 SensorEventListener
interface
Caused by: java.lang.ClassCastException: com.example.jasinthpremkumar.speed.MainActivity cannot be cast to android.hardware.SensorEventListener
您遇到异常是因为您在 MainActivity
中没有 implements SensorEventListener
界面并且您正在使用
msen.registerListener((SensorEventListener) this,sensor, SensorManager.SENSOR_DELAY_NORMAL);
在里面 onResume()
试试这个 你需要 implements SensorEventListener
界面在你的 MainActivity
Change your activity code like this
public class MainActivity extends AppCompatActivity implements SensorEventListener {
public SensorManager msen;
public Sensor sensor;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
msen = (SensorManager) getSystemService(SENSOR_SERVICE);
if (msen != null) {
sensor=msen.getDefaultSensor(Sensor.TYPE_ACCELEROMETER);
}
}
protected void onResume() {
super.onResume();
msen.registerListener(this,sensor, SensorManager.SENSOR_DELAY_NORMAL);
}
public final void onSensorChanged(SensorEvent event) {
float x = event.values[0];
float y=event.values[1];
float z=event.values[2];
float ans=x*x+y*y+z*z;
ans= (float) Math.sqrt(ans);
TextView value;
value=(TextView) findViewById(R.id.val);
value.setText((int) ans);
value.setHeight(100);
value.setWidth(100);
value.setGravity(Gravity.CENTER);
}
@Override
public void onAccuracyChanged(Sensor sensor, int accuracy) {
}
@Override
protected void onPause() {
super.onPause();
msen.unregisterListener((SensorEventListener) this);
}
}
错误提示
MainActivity cannot be cast to android.hardware.SensorEventListener
这意味着您没有使用 SenserEventListener
实现 MainActivity。错误来自下面的行。
msen.registerListener((SensorEventListener) this,sensor, SensorManager.SENSOR_DELAY_NORMAL);
此处您将 MainActivity
转换为 SenserEventListener
,而这两者之间没有任何关系。这些应该是 IS-A
关系。
所以解决方案是在 MainActivity
中实现 SenserEventListener
并覆盖抽象方法。
public class MainActivity extends AppCompatActivity implements SenserEventListener
然后就可以直接用this
了。
msen.registerListener(this,sensor, SensorManager.SENSOR_DELAY_NORMAL);
您需要对您的 activity 实施 SensorEventListener
,如下所示:
public class MainActivity extends AppCompatActivity implements SensorEventListener{
----- your code -----
----- your code -----
}
这就是您需要做的全部。希望对你有帮助。
我对 android 编程很陌生
今天我编写了一个应用程序来访问加速度计,从此处提供在线示例1 & 2
我已经完成了我的程序,但问题是应用程序没有启动。 我认为我的程序有错误但找不到它
这是代码
import android.hardware.Sensor;
import android.hardware.SensorEvent;
import android.hardware.SensorListener;
import android.hardware.SensorManager;
import android.hardware.SensorEventListener;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.view.Gravity;
import android.widget.TextView;
import static android.content.Context.SENSOR_SERVICE;
public class MainActivity extends AppCompatActivity{
public SensorManager msen;
public Sensor sensor;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
msen = (SensorManager) getSystemService(SENSOR_SERVICE);
if (msen != null) {
sensor=msen.getDefaultSensor(Sensor.TYPE_ACCELEROMETER);
}
}
protected void onResume() {
super.onResume();
msen.registerListener((SensorEventListener) this,sensor, SensorManager.SENSOR_DELAY_NORMAL);
}
public final void onSensorChanged(SensorEvent event) {
float x = event.values[0];
float y=event.values[1];
float z=event.values[2];
float ans=x*x+y*y+z*z;
ans= (float) Math.sqrt(ans);
TextView value;
value=(TextView) findViewById(R.id.val);
value.setText((int) ans);
value.setHeight(100);
value.setWidth(100);
value.setGravity(Gravity.CENTER);
}
@Override
protected void onPause() {
super.onPause();
msen.unregisterListener((SensorEventListener) this);
}
}
我得到的输出是
不幸的是速度已经停止
错误日志
E/AndroidRuntime: FATAL EXCEPTION: main
Process: com.example.jasinthpremkumar.speed, PID: 13210
java.lang.RuntimeException: Unable to resume activity {com.example.jasinthpremkumar.speed/com.example.jasinthpremkumar.speed.MainActivity}: java.lang.ClassCastException: com.example.jasinthpremkumar.speed.MainActivity cannot be cast to android.hardware.SensorEventListener
at android.app.ActivityThread.performResumeActivity(ActivityThread.java:2974)
at android.app.ActivityThread.handleResumeActivity(ActivityThread.java:3005)
at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2371)
at android.app.ActivityThread.access0(ActivityThread.java:149)
at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1284)
at android.os.Handler.dispatchMessage(Handler.java:102)
at android.os.Looper.loop(Looper.java:135)
at android.app.ActivityThread.main(ActivityThread.java:5290)
at java.lang.reflect.Method.invoke(Native Method)
at java.lang.reflect.Method.invoke(Method.java:372)
at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:908)
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:703)
Caused by: java.lang.ClassCastException: com.example.jasinthpremkumar.speed.MainActivity cannot be cast to android.hardware.SensorEventListener
at com.example.jasinthpremkumar.speed.MainActivity.onResume(MainActivity.java:32)
at android.app.Instrumentation.callActivityOnResume(Instrumentation.java:1241)
at android.app.Activity.performResume(Activity.java:6106)
at android.app.ActivityThread.performResumeActivity(ActivityThread.java:2963)
at android.app.ActivityThread.handleResumeActivity(ActivityThread.java:3005)
at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2371)
at android.app.ActivityThread.access0(ActivityThread.java:149)
at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1284)
at android.os.Handler.dispatchMessage(Handler.java:102)
at android.os.Looper.loop(Looper.java:135)
at android.app.ActivityThread.main(ActivityThread.java:5290)
at java.lang.reflect.Method.invoke(Native Method)
at java.lang.reflect.Method.invoke(Method.java:372)
at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:908)
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:703)
Application terminated.
SensorEventListener
是一个 interface
并且您不能将 Activity
转换为 SensorEventListener
interface
Caused by: java.lang.ClassCastException: com.example.jasinthpremkumar.speed.MainActivity cannot be cast to android.hardware.SensorEventListener
您遇到异常是因为您在 MainActivity
中没有 implements SensorEventListener
界面并且您正在使用
msen.registerListener((SensorEventListener) this,sensor, SensorManager.SENSOR_DELAY_NORMAL);
在里面 onResume()
试试这个 你需要 implements SensorEventListener
界面在你的 MainActivity
Change your activity code like this
public class MainActivity extends AppCompatActivity implements SensorEventListener {
public SensorManager msen;
public Sensor sensor;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
msen = (SensorManager) getSystemService(SENSOR_SERVICE);
if (msen != null) {
sensor=msen.getDefaultSensor(Sensor.TYPE_ACCELEROMETER);
}
}
protected void onResume() {
super.onResume();
msen.registerListener(this,sensor, SensorManager.SENSOR_DELAY_NORMAL);
}
public final void onSensorChanged(SensorEvent event) {
float x = event.values[0];
float y=event.values[1];
float z=event.values[2];
float ans=x*x+y*y+z*z;
ans= (float) Math.sqrt(ans);
TextView value;
value=(TextView) findViewById(R.id.val);
value.setText((int) ans);
value.setHeight(100);
value.setWidth(100);
value.setGravity(Gravity.CENTER);
}
@Override
public void onAccuracyChanged(Sensor sensor, int accuracy) {
}
@Override
protected void onPause() {
super.onPause();
msen.unregisterListener((SensorEventListener) this);
}
}
错误提示
MainActivity cannot be cast to android.hardware.SensorEventListener
这意味着您没有使用 SenserEventListener
实现 MainActivity。错误来自下面的行。
msen.registerListener((SensorEventListener) this,sensor, SensorManager.SENSOR_DELAY_NORMAL);
此处您将 MainActivity
转换为 SenserEventListener
,而这两者之间没有任何关系。这些应该是 IS-A
关系。
所以解决方案是在 MainActivity
中实现 SenserEventListener
并覆盖抽象方法。
public class MainActivity extends AppCompatActivity implements SenserEventListener
然后就可以直接用this
了。
msen.registerListener(this,sensor, SensorManager.SENSOR_DELAY_NORMAL);
您需要对您的 activity 实施 SensorEventListener
,如下所示:
public class MainActivity extends AppCompatActivity implements SensorEventListener{
----- your code -----
----- your code -----
}
这就是您需要做的全部。希望对你有帮助。