Julian
Julian

Reputation: 657

Load activity from a string variable

I am doing an app where I have questions displayed and I have stored the next question number in a database so the user can resume whenever they wanted.

My problem here is I fetch the next question number from the DB and append the number to a String variable so then goNextQuestion is equal to Question# (# being the number from the DB).

I then want to pass that variable, for example it is equal to Question2, to the new Intent function to load the activity associated with that question.

Here is the code from Home.class:

button.setOnClickListener(new View.OnClickListener() {
        public void onClick(View v) {
                try {
                        final String goQuestion = "Question" + lastQuestion";
                        startActivity(new Intent(Home.this, Class.forName(goQuestion)));
                } catch (ClassNotFoundException e) {
                        e.printStackTrace();
                }
        }
});

The full Home.class can be viewed here: https://pastebin.com/6mbrBpQm

My expected result would be it wil load the same way as it would if I just passed Question2.class into the Intent. However, there is an error and the catch statement is always executed with the following error:

W/System.err: java.lang.ClassNotFoundException: Question2.class
        at java.lang.Class.classForName(Native Method)
        at java.lang.Class.forName(Class.java:400)
        at java.lang.Class.forName(Class.java:326)
        at uk.co.jrtevents.k_quiz.Home$1.onClick(Home.java:45)
W/System.err:     at android.view.View.performClick(View.java:5610)
        at android.view.View$PerformClick.run(View.java:22265)
        at android.os.Handler.handleCallback(Handler.java:751)
        at android.os.Handler.dispatchMessage(Handler.java:95)
        at android.os.Looper.loop(Looper.java:154)
        at android.app.ActivityThread.main(ActivityThread.java:6077)
        at java.lang.reflect.Method.invoke(Native Method)
        at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:866)
        at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:756)
    Caused by: java.lang.ClassNotFoundException: Didn't find class "Question2.class" on path: DexPathList[[zip file "/data/app/uk.co.jrtevents.k_quiz-1/base.apk"],nativeLibraryDirectories=[/data/app/uk.co.jrtevents.k_quiz-1/lib/x86, /system/lib, /vendor/lib]]
        at dalvik.system.BaseDexClassLoader.findClass(BaseDexClassLoader.java:56)
        at java.lang.ClassLoader.loadClass(ClassLoader.java:380)
        at java.lang.ClassLoader.loadClass(ClassLoader.java:312)
        ... 13 more

What would be the best way to resolve this?

Link to Question2.class: https://pastebin.com/5sdsc1Ec

Upvotes: 1

Views: 66

Answers (2)

vikas kumar
vikas kumar

Reputation: 11018

you can try something like below.

if you use the Simple class name like Step8CompletedActivity then it will give the error.

var clazz =
Class.forName(Step8CompletedActivity::class.java.simpleName!!).kotlin as KClass<Activity>

below error will come

java.lang.ExceptionInInitializerError
    at com.google.samples.motionlayoutcodelab.MainActivityKt.access$getData$p(MainActivity.kt:1)
    at com.google.samples.motionlayoutcodelab.MainActivity.onCreate(MainActivity.kt:114)
    at android.app.Activity.performCreate(Activity.java:6662)
    at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1118)
    at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2599)
    at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2707)
    at android.app.ActivityThread.-wrap12(ActivityThread.java)
    at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1460)
    at android.os.Handler.dispatchMessage(Handler.java:102)
    at android.os.Looper.loop(Looper.java:154)
    at android.app.ActivityThread.main(ActivityThread.java:6077)
    at java.lang.reflect.Method.invoke(Native Method)
    at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:866)
    at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:756)
 Caused by: java.lang.ClassNotFoundException: Step8CompletedActivity
    at java.lang.Class.classForName(Native Method)
    at java.lang.Class.forName(Class.java:400)
    at java.lang.Class.forName(Class.java:326)
    at com.google.samples.motionlayoutcodelab.MainActivityKt.<clinit>(MainActivity.kt:39)
    at com.google.samples.motionlayoutcodelab.MainActivityKt.access$getData$p(MainActivity.kt:1) 

instead of using the simple name only use Canonical name or Full name with path

var clazz =
Class.forName(Step8CompletedActivity::class.java.canonicalName!!).kotlin as KClass<Activity>

or using below will produce the same results.

 var clazz =
Class.forName("com.google.samples.motionlayoutcodelab.Step8CompletedActivity").kotlin as KClass<Activity>

Upvotes: 1

Ticherhaz FreePalestine
Ticherhaz FreePalestine

Reputation: 2377

You don't have put .class

  final String goQuestion = "Question" + lastQuestion;

Try this

button.setOnClickListener(new View.OnClickListener() {
            public void onClick(View v) {
                try {
                    final String goQuestion = "Question" + lastQuestion;
                    startActivity(new Intent(Home.this, Class.forName(goQuestion)));
                } catch (ClassNotFoundException e) {
                    e.printStackTrace();
                }
            }
        });

Upvotes: 0

Related Questions