newInstance失败:android程序开发中没有<init>错误</init>

时间:2012-03-22 08:39:32

标签: android

我正在编写一个示例代码,以使用Android方向传感器来查看3D场景。

我得到的错误是:

03-22 16:12:48.939: D/dalvikvm(9532): newInstance failed: no <init>()
03-22 16:12:48.939: D/AndroidRuntime(9532): Shutting down VM
03-22 16:12:48.939: W/dalvikvm(9532): threadid=1: thread exiting with uncaught exception (group=0x40018560)
03-22 16:12:48.949: E/AndroidRuntime(9532): FATAL EXCEPTION: main
03-22 16:12:48.949: E/AndroidRuntime(9532): java.lang.RuntimeException: Unable to instantiate activity ComponentInfo{com.droidnova.android.games.vortex/com.droidnova.android.games.vortex.Vortex}: java.lang.InstantiationException: com.droidnova.android.games.vortex.Vortex
03-22 16:12:48.949: E/AndroidRuntime(9532):     at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:1680)
03-22 16:12:48.949: E/AndroidRuntime(9532):     at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:1784)
03-22 16:12:48.949: E/AndroidRuntime(9532):     at android.app.ActivityThread.access$1500(ActivityThread.java:123)
03-22 16:12:48.949: E/AndroidRuntime(9532):     at android.app.ActivityThread$H.handleMessage(ActivityThread.java:939)
03-22 16:12:48.949: E/AndroidRuntime(9532):     at android.os.Handler.dispatchMessage(Handler.java:99)
03-22 16:12:48.949: E/AndroidRuntime(9532):     at android.os.Looper.loop(Looper.java:130)
03-22 16:12:48.949: E/AndroidRuntime(9532):     at android.app.ActivityThread.main(ActivityThread.java:3835)
03-22 16:12:48.949: E/AndroidRuntime(9532):     at java.lang.reflect.Method.invokeNative(Native Method)
03-22 16:12:48.949: E/AndroidRuntime(9532):     at java.lang.reflect.Method.invoke(Method.java:507)
03-22 16:12:48.949: E/AndroidRuntime(9532):     at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:864)
03-22 16:12:48.949: E/AndroidRuntime(9532):     at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:622)
03-22 16:12:48.949: E/AndroidRuntime(9532):     at dalvik.system.NativeStart.main(Native Method)
03-22 16:12:48.949: E/AndroidRuntime(9532): Caused by: java.lang.InstantiationException: com.droidnova.android.games.vortex.Vortex
03-22 16:12:48.949: E/AndroidRuntime(9532):     at java.lang.Class.newInstanceImpl(Native Method)
03-22 16:12:48.949: E/AndroidRuntime(9532):     at java.lang.Class.newInstance(Class.java:1409)
03-22 16:12:48.949: E/AndroidRuntime(9532):     at android.app.Instrumentation.newActivity(Instrumentation.java:1021)
03-22 16:12:48.949: E/AndroidRuntime(9532):     at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:1672)
03-22 16:12:48.949: E/AndroidRuntime(9532):     ... 11 more

Vortex.java:

public class Vortex extends Activity {
    private static final String LOG_TAG = Vortex.class.getSimpleName();
    private VortexView _vortexView;
    SensorManager sm;
    private float headingAngle,pitchAngle,rollAngle;

    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);

        _vortexView = new VortexView(this);

        setContentView(_vortexView);        
        //setContentView(R.layout.main);
        SensorManager sm = (SensorManager)getSystemService(Context.SENSOR_SERVICE);
    }

    //////////////////

    private final Context context;
    public Vortex(Context context) {
        this.context = context;
    }


    int sensorType = Sensor.TYPE_ORIENTATION;

    private final SensorEventListener myOrientationListener = new SensorEventListener(){
        public void onSensorChanged(SensorEvent sensorEvent) {
            if(sensorEvent.sensor.getType() == Sensor.TYPE_ORIENTATION) {
                headingAngle = sensorEvent.values[0];
                pitchAngle = sensorEvent.values[1];
                rollAngle = sensorEvent.values[2];

                //TODO: 对应用程序应用方向变化
                _vortexView.updateAngles(headingAngle, pitchAngle, rollAngle);

            }
        }
        public void onAccuracyChanged(Sensor sensor, int Accuracy){}
    };

    //////////////////


    @Override
    protected void onResume() {
        // TODO Auto-generated method stub
        super.onResume();

        Sensor orentation = sm.getDefaultSensor(Sensor.TYPE_ORIENTATION);
        sm.registerListener(myOrientationListener, orentation, SensorManager.SENSOR_DELAY_FASTEST);
    }

    @Override
    protected void onStop() {
        // TODO Auto-generated method stub
        super.onStop();
        sm.unregisterListener(myOrientationListener);
    }
    }

我绝对感到困惑,可能出现什么问题?

谢谢

bitxue

我已经解决了这个问题 加入

public Vortex() {
        super();
    }
“Vortex.java”文件中的

方法。

1 个答案:

答案 0 :(得分:0)

详细说明原作者Fragment requires添加的答案,即任何片段都存在默认的no-args构造函数。如果Android框架无法找到这样的构造函数,它将抛出上面提到的InstantiationException。

有几个原因Android可能无法找到默认构造函数。第一个是显而易见的:它不存在(例如,Fragment实现已添加了自己的带有参数的构造函数)。如果您的Fragment实现遵循Android准则并且没有定义构造函数,则可以避免此问题。

Android无法找到构造函数的第二个原因是访问修饰符。例如,私有内部类中定义的构造函数将无法访问Android框架,结果将是InstantiationException。