Waypoint
Waypoint

Reputation: 17793

Android - start intent issues

Whenewer I want to start new activity, I use this code:

Intent myIntent1 = new Intent(this, Info1.class);
startActivity(myIntent1);

But, what about the case, I am getting class name from String Array? I have tried method class.forName like this:

Intent intent = new Intent(this, Class.forName(array[1]));
startActivity(intent);

But no success. It is giving me error of

java.lang.ClassNotFoundException: Info1 in loader dalvik.system.PathClassLoader

even though in first case it started an activity successfully. I have to add that I am 100% sure array[1] contains string "Info1" and that I have Info1.class in my package.

Do you know, how to solve this?

Upvotes: 3

Views: 1546

Answers (3)

Saurabh
Saurabh

Reputation: 7964

Try the following:

Class clazz = Thread.currentThread().getContextClassLoader().findClass(array[1]);
new Intent(this, clazz);

Upvotes: 1

Zwiebel
Zwiebel

Reputation: 1615

You need to add the full package name to your class.forName parameter, like ple instead of array[1].

Upvotes: 1

Adil Soomro
Adil Soomro

Reputation: 37729

The method

Class.forName("xxxxx");

Needs fully qualified name which means you should give the name along with the package details(in which package the class exists)

as an example

Class.forName("com.mypackage.MyClass");

Documentation Reference here

Upvotes: 3

Related Questions