melvintcs
melvintcs

Reputation: 551

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(new AdapterView.OnItemClickListener() {

 @Override
 public void onItemClick(AdapterView<?> parent, View v, int position, long id) {
 Intent intent = new Intent(v.getContext(), ClassName.class);
 startActivity(intent);
}}

The following attempt also failed to work:

Intent intent = new Intent(ThisClassName.this, ClassName.class);
startActivity(intent);

Any advice would be greatly appreciated :)

Upvotes: 0

Views: 3073

Answers (4)

Yahya Arshad
Yahya Arshad

Reputation: 1626

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>

Upvotes: 0

Zardaloop
Zardaloop

Reputation: 1634

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.

Upvotes: 0

user370305
user370305

Reputation: 109247

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.

Upvotes: 4

herom
herom

Reputation: 2542

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....

Upvotes: 2

Related Questions