When you use Class.forName(className)
method, className
has to be constructed from your package name and your Class
name without the .class
extension.
For ro.movieapp.activities.MovieActivity.class
your call should be something like this: Class.forName("ro.movieapp.activities.MovieActivity")
.
That being said, it is a really bad idea to do it because it’s an easy source of bugs (your question is a good example). You should keep a reference to your activity’s Class
inside your Movie
object.
My suggestion is to do something like this:
class Movie {
private Class<?> activityClass;
public <C extends AppCompatActivity> void setActivityClass(Class<C> activityClass) {
this.activityClass = activityClass;
}
@SuppressWarnings("unchecked")
public <C extends AppCompatActivity> Class<C> getActivityClass() {
return (Class<C>) activityClass;
}
}
...
final Movie movie = new Movie();
movie.setActivityClass(MovieActivity.class);
...
final Intent intent = new Intent(getActivity(), movie.getActivityClass());
startActivity(intent);
0
solved Open a new activity from a class string. Class null