简体   繁体   English

newInstance失败:否 <init> android程序开发中的错误

[英]newInstance failed: no <init> error in android program development

I am writing a sample code to use android orientation sensor to view 3D scenes. 我正在编写示例代码以使用android方向传感器查看3D场景。

The error i got is : 我得到的错误是:

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

I am absolutely confused, what could possibly be wrong? 我完全感到困惑,可能是什么问题?

Thank you 谢谢

bitxue 比特雪

I have solved this problem by adding 我已经通过添加解决了这个问题

public Vortex() {
        super();
    }

method in "Vortex.java" file. “ Vortex.java”文件中的方法。

To elaborate on the answer added by the original post author, Fragment requires that a default, no-args constructor be present for any Fragments. 为了详细说明原始帖子作者添加的答案, Fragment要求为任何Fragment提供默认的无参数构造函数。 If the Android framework is unable to find such a constructor, it will throw the InstantiationException mentioned above. 如果Android框架找不到这样的构造函数,它将抛出上面提到的InstantiationException。

There are a couple reasons Android could be unable to find the default constructor. Android可能无法找到默认构造函数的原因有两个。 The first is obvious: it doesn't exist (the Fragment implementation has added it's own constructor with arguments, for instance). 第一个很明显:它不存在(例如Fragment实现添加了它自己的带有参数的构造函数)。 If your Fragment implementation follow the Android guidelines and doesn't define a constructor, you can avoid this problem. 如果您的Fragment实现遵循Android准则且未定义构造函数,则可以避免此问题。

A second reason that Android will be unable to find the constructor is due to access modifiers. Android无法找到构造函数的第二个原因是由于访问修饰符。 For instance, a constructor defined in a private inner class will be inaccessible to the Android framework, and the result will be an InstantiationException. 例如,在私有内部类中定义的构造函数对于Android框架是不可访问的,结果将是InstantiationException。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM