无法实例化Compass活动的活动

时间:2014-07-11 09:56:37

标签: java android android-activity android-sensors sensormanager

我正在研究指南针应用程序, SensorManager已在onCreate()方法中进行了初始化,并使用onSensorChanged()方法对其进行了移动或动画处理。

App运行并崩溃而没有提供任何输出,我也检查了LogCat但无法解决主要问题,需要指导。

MainActivity.JAVA

package com.jdev.truenorthcompass;

import android.hardware.Sensor;
import android.hardware.SensorEvent;
import android.hardware.SensorEventListener;
import android.hardware.SensorManager;
import android.os.Bundle;
import android.app.Activity;
import android.view.Menu;
import android.view.animation.Animation;
import android.view.animation.RotateAnimation;
import android.widget.ImageView;
import android.widget.TextView;

public abstract class MainActivity extends Activity implements SensorEventListener{

private ImageView image;

private float currentDegree = 0f;

private SensorManager mSensorManager;

TextView tvHeading;

@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_main);

    image = (ImageView)findViewById(R.id.imageViewCompass);

    tvHeading = (TextView) findViewById(R.id.tvHeading);

    mSensorManager = (SensorManager) getSystemService(SENSOR_SERVICE);
}

@SuppressWarnings("deprecation")
@Override
protected void onResume(){
    super.onResume();

    mSensorManager.registerListener(this, mSensorManager.getDefaultSensor(Sensor.TYPE_ORIENTATION), SensorManager.SENSOR_DELAY_GAME);
}

@Override
protected void onPause(){
    super.onPause();
    mSensorManager.unregisterListener(this);
}

public void OnSensorChanged(SensorEvent event){
    float degree = Math.round(event.values[0]);

    tvHeading.setText("Heading:" + Float.toString(degree)+ "degrees");

    RotateAnimation ra = new RotateAnimation(
            currentDegree,
            -degree,
            Animation.RELATIVE_TO_SELF, 0.5f,
            Animation.RELATIVE_TO_SELF, 0.5f);

    ra.setDuration(210);

    ra.setFillAfter(true);

    image.startAnimation(ra);
    currentDegree = -degree;
}

@Override
public void onAccuracyChanged(Sensor sensor, int accuracy){

}

@Override
public boolean onCreateOptionsMenu(Menu menu) {
    // Inflate the menu; this adds items to the action bar if it is present.
    getMenuInflater().inflate(R.menu.main, menu);
    return true;
}

}

并且TextView用于显示用户的当前标题。 ImageView包含罗盘图像,该图像使用我们MainActivity.java中的代码像罗盘一样动画。 的 activity_main.xml中

<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:background="#fff" >

<TextView 
    android:id="@+id/tvHeading"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:layout_centerHorizontal="true"
    android:layout_marginBottom="40dp"
    android:layout_marginTop="20dp"
    android:text="Heading: 0.0" />

<ImageView
    android:id="@+id/imageViewCompass"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:layout_alignParentLeft="true"
    android:layout_alignTop="@+id/tvHeading"
    android:layout_marginTop="20dp"
    android:src="@drawable/img_compass" />

以下是错误报告LogCat

07-11 05:27:26.203: D/dalvikvm(1195): newInstance failed: p0 i0 [0 a1
07-11 05:27:26.203: D/AndroidRuntime(1195): Shutting down VM
07-11 05:27:26.213: W/dalvikvm(1195): threadid=1: thread exiting with uncaught exception (group=0xb4abab90)
07-11 05:27:26.243: E/AndroidRuntime(1195): FATAL EXCEPTION: main
07-11 05:27:26.243: E/AndroidRuntime(1195): Process: com.jdev.truenorthcompass, PID: 1195
07-11 05:27:26.243: E/AndroidRuntime(1195): java.lang.RuntimeException: Unable to instantiate activity ComponentInfo{com.jdev.truenorthcompass/com.jdev.truenorthcompass.MainActivity}: java.lang.InstantiationException: can't instantiate class com.jdev.truenorthcompass.MainActivity
07-11 05:27:26.243: E/AndroidRuntime(1195):     at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2102)
07-11 05:27:26.243: E/AndroidRuntime(1195):     at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2226)
07-11 05:27:26.243: E/AndroidRuntime(1195):     at android.app.ActivityThread.access$700(ActivityThread.java:135)
07-11 05:27:26.243: E/AndroidRuntime(1195):     at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1397)
07-11 05:27:26.243: E/AndroidRuntime(1195):     at android.os.Handler.dispatchMessage(Handler.java:102)
07-11 05:27:26.243: E/AndroidRuntime(1195):     at android.os.Looper.loop(Looper.java:137)
07-11 05:27:26.243: E/AndroidRuntime(1195):     at android.app.ActivityThread.main(ActivityThread.java:4998)
07-11 05:27:26.243: E/AndroidRuntime(1195):     at java.lang.reflect.Method.invokeNative(Native Method)
07-11 05:27:26.243: E/AndroidRuntime(1195):     at java.lang.reflect.Method.invoke(Method.java:515)
07-11 05:27:26.243: E/AndroidRuntime(1195):     at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:777)
07-11 05:27:26.243: E/AndroidRuntime(1195):     at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:593)
07-11 05:27:26.243: E/AndroidRuntime(1195):     at dalvik.system.NativeStart.main(Native Method)
07-11 05:27:26.243: E/AndroidRuntime(1195): Caused by: java.lang.InstantiationException: can't instantiate class com.jdev.truenorthcompass.MainActivity
07-11 05:27:26.243: E/AndroidRuntime(1195):     at java.lang.Class.newInstanceImpl(Native Method)
07-11 05:27:26.243: E/AndroidRuntime(1195):     at java.lang.Class.newInstance(Class.java:1208)
07-11 05:27:26.243: E/AndroidRuntime(1195):     at android.app.Instrumentation.newActivity(Instrumentation.java:1061)
07-11 05:27:26.243: E/AndroidRuntime(1195):     at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2093)
07-11 05:27:26.243: E/AndroidRuntime(1195):     ... 11 more

任何帮助将不胜感激。谢谢!

Manifest.XML

<uses-sdk
    android:minSdkVersion="14"
    android:targetSdkVersion="19" />

<uses-permission android:name="android.permission.ACCESS_COARSE_LOCATION"/>
<uses-permission android:name="android.permission.ACCESS_MOCK_LOCATION"/>
<uses-permission android:name="android.permission.ACCESS_FINE_LOCATION"/>


<application
    ...
    <activity
        android:name="com.jdev.truenorthcompass.MainActivity"
        ...
        <intent-filter>
            <action android:name="android.intent.action.MAIN" />

            <category android:name="android.intent.category.LAUNCHER" />
        </intent-filter>
    </activity>
</application>

1 个答案:

答案 0 :(得分:5)

您的课程为abstract

public abstract class MainActivity

抽象类无法直接实例化