samedi 28 octobre 2017

ClassNotFound Exception in Reflection created by Class name

I am facing difficulty with the below code. I need to create object through reflection by class name in Java but it throws below error while compiling.

// Class where package name with class is coming

public class PickAdapter<T> {


    Object mObject;

    public T read(Element element, String classOfName) {

        try {

            Class mClass = Class.forName(classOfName);


           mObject = mClass.newInstance();



            Field[] fd = mClass.getDeclaredFields();

 } catch (ClassNotFoundException e) {
            e.printStackTrace();
        } catch (IllegalAccessException e) {
            e.printStackTrace();
        } catch (InstantiationException e) {
            e.printStackTrace();
        }


        return (T) mObject;

    }

// Error in Logcat

 10-28 17:12:54.619 20743-20743/com.harpz.htmlee D/NetworkSecurityConfig: No Network Security Config specified, using platform default
    10-28 17:12:55.771 20743-20743/com.harpz.htmlee W/System.err: java.lang.ClassNotFoundException: Invalid name: class com.harpz.htmlee.model.MUser
    10-28 17:12:55.771 20743-20743/com.harpz.htmlee W/System.err:     at java.lang.Class.classForName(Native Method)
    10-28 17:12:55.771 20743-20743/com.harpz.htmlee W/System.err:     at java.lang.Class.forName(Class.java:400)
    10-28 17:12:55.771 20743-20743/com.harpz.htmlee W/System.err:     at java.lang.Class.forName(Class.java:326)
    10-28 17:12:55.772 20743-20743/com.harpz.htmlee W/System.err:     at com.harpz.htmleetim.reflection.PickAdapter.read(PickAdapter.java:22)
    10-28 17:12:55.772 20743-20743/com.harpz.htmlee W/System.err:     at com.harpz.htmleetim.Htmlee.fromHtml(Htmlee.java:25)
    10-28 17:12:55.772 20743-20743/com.harpz.htmlee W/System.err:     at com.harpz.htmleetim.Htmlee.fromHtml(Htmlee.java:19)
    10-28 17:12:55.772 20743-20743/com.harpz.htmlee W/System.err:     at com.harpz.htmleetim.Htmlee.fromHtml(Htmlee.java:13)
    10-28 17:12:55.772 20743-20743/com.harpz.htmlee W/System.err:     at com.harpz.htmlee.MainActivity.onCreate(MainActivity.java:40)
    10-28 17:12:55.772 20743-20743/com.harpz.htmlee W/System.err:     at android.app.Activity.performCreate(Activity.java:6679)
    10-28 17:12:55.772 20743-20743/com.harpz.htmlee W/System.err:     at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1118)
    10-28 17:12:55.772 20743-20743/com.harpz.htmlee W/System.err:     at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2618)
    10-28 17:12:55.772 20743-20743/com.harpz.htmlee W/System.err:     at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2726)
    10-28 17:12:55.772 20743-20743/com.harpz.htmlee W/System.err:     at android.app.ActivityThread.-wrap12(ActivityThread.java)
    10-28 17:12:55.772 20743-20743/com.harpz.htmlee W/System.err:     at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1477)
    10-28 17:12:55.772 20743-20743/com.harpz.htmlee W/System.err:     at android.os.Handler.dispatchMessage(Handler.java:102)
    10-28 17:12:55.772 20743-20743/com.harpz.htmlee W/System.err:     at android.os.Looper.loop(Looper.java:154)
    10-28 17:12:55.772 20743-20743/com.harpz.htmlee W/System.err:     at android.app.ActivityThread.main(ActivityThread.java:6119)
    10-28 17:12:55.772 20743-20743/com.harpz.htmlee W/System.err:     at java.lang.reflect.Method.invoke(Native Method)
    10-28 17:12:55.772 20743-20743/com.harpz.htmlee W/System.err:     at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:886)
    10-28 17:12:55.772 20743-20743/com.harpz.htmlee W/System.err:     at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:776)





Aucun commentaire:

Enregistrer un commentaire