I have 2 different class... de first one (MainSoup) is the main class and this class extends activity. The second class (View2) extends View. in View2 class is wher开发者_如何学运维e i make my OnTouchEvent and my Canvas... I also have a frameLayout with 2 layout... in the first one i put multiple textViews. On top of this first layout i put the second one wich has nothing and here is where i draw with my Canvas and touch events. At this point everythings works just fine.
The problems begin when i want to make an intent... I put the intent in de Main class (MainSoup): Intent i = new Intent(this, org.me.androidsoup.MainSoup.class); startActivity(i); but i dont know how to trigger it (since the OnTouchEvent is in the View2 class).
And if i try to put it in the View2 class, i have troubles with the startActivity line, It doesnt recognize it and tells me to create a method call startActivity.
startActivity() is a method that requires a context (it's actually a method defined by the Context class). Views have a method called getContext() that will return the context attached to that view. You could use that for invoking the Intent.
Hope it helps.
Thinks briefly about it gave me 3 options:
You take care of the onTouchEvent on the Activity, (you do findViewById and set the onTouchEvent to it, then you have context)
You add a static Context variable to the Application Class, when each activity starts (onCreate) it sets the Contexts as this like:
Application.context = this;
Use the getContext() method on the View to get the Context, and do new Intent(getContext, ActivityYouWant.class);
I think the first and third are the most valid options. But you choose.
精彩评论