Skip to content Skip to sidebar Skip to footer

Start Intent Without Oncreate {}

I have created a class which extends Gallery. There is no onCreate() method in super Class, and I'm unable to run my intent. This is my sample code: this.setOnItemClickListener(n

Solution 1:

Actually, startActivity(); is the method of Activity class, To run this method you have to Context of Activity or Reference of Activity , You can not run this method in other class as outside of Activity scope without having Activity reference for it.

Try,

<Activity_Name>.this.startActivity(intent);

or

mContext.startActivity(intent);

Here mContext is reference of your Activity class.

Solution 2:

As you wrote, you want to start an Activity, if your class does not inherit from Activity (which I guess, because otherwise you WOULD HAVE TO implement onCreate()) you won't be able to start it with an Intent at all....

Solution 3:

If your class is the extends of the main activity and you want to call the other activity from the main body of the class, just simply create a method in the other class, then call the method by making an instance of the class and call the method.

Since they have the same nature, that will work, But remember to rap the method call in a try and catch method, otherwise you may get some errors.

Solution 4:

Gallery is a View type if you will see its Hirerchy you will see that its super class is View and view does't have any onCreate method

to use your CustomGallery

you can use it from xml or from code

<com.mypackage.CustomGallery >
android:id...


 </com.mypackage.CustomGallery>

Post a Comment for "Start Intent Without Oncreate {}"