没有编译错误,但Android应用程序没有在设备上启动

时间:2018-04-20 12:06:55

标签: android sensor

我是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.access$800(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.access$800(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.

3 个答案:

答案 0 :(得分:2)

错误说

  

MainActivity无法强制转换为android.hardware.SensorEventListener

这意味着您没有使用SenserEventListener实现MainActivity。错误来自下面一行。

  

msen.registerListener((SensorEventListener)this,sensor,SensorManager.SENSOR_DELAY_NORMAL);

此处您将MainActivity投射到SenserEventListener,而这两者之间没有任何关系。这些应该是IS-A关系。 因此,解决方案是在SenserEventListener中实现MainActivity并覆盖抽象方法。

public class MainActivity extends AppCompatActivity implements SenserEventListener

然后您可以直接使用this

msen.registerListener(this,sensor, SensorManager.SENSOR_DELAY_NORMAL);

答案 1 :(得分:1)

SensorEventListenerinterface,您无法将Activity投射到SensorEventListener interface

  

引起:java.lang.ClassCastException:com.example.jasinthpremkumar.speed.MainActivity无法强制转换为android.hardware.SensorEventListener

您已超越异常,因为您在implements SensorEventListener中没有MainActivity界面并且您正在使用 msen.registerListener((SensorEventListener) this,sensor, SensorManager.SENSOR_DELAY_NORMAL); inisde onResume()

试试这个您需要implements SensorEventListener

中的MainActivity界面
  

像这样更改您的活动代码

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);
    }


}

答案 2 :(得分:0)

您需要对您的活动实施SensorEventListener,如下所示:

public class MainActivity extends AppCompatActivity implements SensorEventListener{

      ----- your code ----- 
      ----- your code -----
}

这就是你需要做的一切。希望它会有所帮助。