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 where 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.
Related
I know this is a basic question but i have seen that using the method getApplicationContext() to get the context work at places where the "this" keyword does't work, especially inside an onClickListener.
Why is this?
In the case of an OnClickListener, this is the anonymous class of the OnClickListener, therefore not a Context.
Whereas calling that method works because it's from the Activity class.
Alternatively, MyActivity.this works as well.
getActivity(): Used inside a Fragment to get the context of activity it is currently associated to.
this: Returns the context of current block in which it is called. If it is called inside an onClickListener then it would return the context of that listener, not the activity.
MyActivity.this: Returns the context of the activity. This can be used at the place of getActivity() as an alternate. (MyActivity should be read as the activity name you are using).
This is strange, maybe someone can help with this. I have two classes and when I try to access variable from main class, value is always 0.0
MAIN CLASS
public class MainActivity extends Activity {
public float angleCurrent;
- do something whith angleCurrent
//System.out.println(angleCurrent); - I get right values
}
SECOND CLASS
public class SecondClass extends ImageView {
public float curAngle = new MainActivity().angleCurrent;
//System.out.println(curAngle); - I get 0.0 all the time
}
Code is just illustration.
There are a couple big issues at play here. First, why the behavior is occurring:
You are not accessing the variable from the first MainActivity instance inside of SecondClass, you have created a new second instance of MainActivity, so you are getting access to a different variable than the original.
Now, the second issue surrounds the correct way to do this in Android. You should NEVER directly instantiate Activity instances. It is also not good practice to pass references to an Activity around any more than the framework already does. If your Activity needs to pass some information to your custom ImageView, you should create a method on your ImageView that you can use for the Activity to pass the value forward (i.e. SecondClass.setCurrentAngle(angleCurrent))
If you are looking to a View to access variables in the activity, it's not going to work. You need to add a setMethod to your custom view and set the value from the activity. Chthlu is right about why you aren't getting a value: new creates a different instance than the one you have on screen. You should never call new on an Activity class. You shouldn't even HAVE a constructor for an Activity, you should setup everything in the onCreate method.
I'm trying to make a app that when the screen is touch it will call up a new intent.
I have code that catches the touch event in the view class. When I try to create a new intent, Intent(this, cYesNoDisplay.class);, i get a error saying the constuctor is undefined, I'm assuming the constructor is not defined in the view base class, but the Activity class?
I'm confused about how to do this, is there a way for my View class that is a member of the intent class, to call it some how???? I figure there must be a wy to do this, still learning Java.
Ted
Your assesment about the View class that you are inside of being the problem is correct. In order to get it working do this:
Intent i = new Intent(NameOfYourActivity.this, cYesNoDisplay.class);
replace [NameOfYourActivity] with the name of the activity that you are inside of.
EDIT: I might have misunderstood what you were doing. If you have actually built your own View class and are overriding onTouch() you actually need to do it a little bit differently.
If you don't already have it add:
Context ctx;
to your classes declarations.
in your constructor alter it to store the context that gets passed in as a parameter in the ctx reference that you declared.
public [ClassName] (Context c){
this.ctx = c;
}
Then inside the onTouch() do it like this:
Intent i = new Intent(ctx, cYesNoDisplay.class);
ctx.startActivity();
EDIT again: The reason you have to use ctx.startActivity(i); is that startActivity() is a method of Context. Since Activity is a Context you don't have to put anything in front of it when you call it from inside an activity. But when you are "inside" of a different class you have to use a reference to a Context to call the method.
Use this . this work fine in my project.
Intent i = new Intent(NameOfYourActivity.this, cYesNoDisplay.class);
startActivity(i);
I want to call onCreate(Bundle cicici); from other class then i am getting "NullPointerException", so please guide me how can i call the onCreate() from another class.
There is only one way in which onCreate can be called, by starting an Activity, since onCreate is as part of Activity life cycle.
startActivity(new Intent(presentActivity.this, NextActivity.class));
if you want to call onCreate in order to actually present a new screen, then you need to create a the new Activity using the android framework style.
Ingredients:
1- An event to call your new activity( ie. onClickListener of a Button or list triggered)
2- On the event you need to create an Intent with the reference of the current activity and a class reference of your new Activity, example:
Intent intent =new Intent(CurrenActivity.this, MyNewActivity.class);
3- You need to call this activity depending on what you'll need you use startActivity or startActivityForResult, the last is use when you expect a response from your activity.
You can also refer to Android documentation Common Task, let us know if its helpful
It depends what you want to do in the second activity. If you want to create a simple task you can always use dialogs and you can show them inside your activity.
Or, on a second thought, you can hide some of your views and enable others but I guess that's not an orthodox solution :)
I have an asynch task with my app which goes to a website, grabs the results from the API and appends a number of clickable textviews to an existing LinearLayout.
However I want to be able to launch a new activity when the textview is clicked. This isn't possible with the asynch class defined in a seperate file, would it be easier to define it as an inline class within the activity?
You can always pass Context to your async class.
A better approach would be to have callbacks (listeners) in the calling class for the async to call back to.
One approach is to inflate your TextViews from an XML file that declares an onClick attribute, naming a method defined in your Activity.
Do not use a context as an Activity! You will probably receive a cast error anyway. Instead, you can pass the activity as a function parameter, like this:
public void function(Activity act)
{
Intent intent = new Intent(act, newActivity.class);
act.startActivity(intent);
}
Or overload the constructor to accept the activity as a parameter. But I strongly suggest you to check you code. If you are calling an activity, you, probably, should be within another one, don't you agree? But, I Know that sometimes we have to make a few concessions, in order to make things work properly. So, use it wisely.