简体   繁体   中英

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

I am writing a sample code to use android orientation sensor to view 3D scenes.

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 :

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.

To elaborate on the answer added by the original post author, Fragment requires that a default, no-args constructor be present for any Fragments. If the Android framework is unable to find such a constructor, it will throw the InstantiationException mentioned above.

There are a couple reasons Android could be unable to find the default constructor. The first is obvious: it doesn't exist (the Fragment implementation has added it's own constructor with arguments, for instance). If your Fragment implementation follow the Android guidelines and doesn't define a constructor, you can avoid this problem.

A second reason that Android will be unable to find the constructor is due to access modifiers. For instance, a constructor defined in a private inner class will be inaccessible to the Android framework, and the result will be an InstantiationException.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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