1

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

应用程序在没有给出任何输出的情况下运行和崩溃,我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;
}

}

而 forTextView则用于显示用户当前的标题。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>
4

1 回答 1

5

你的课是abstract

public abstract class MainActivity

抽象类不能直接实例化

于 2014-07-11T10:00:41.713 回答