I have been trying to use Intent method in my program , the code shows no error when I use myactivity.this ... when I use the other two (myactivity.class or this),eclipse shows an error.
Intent i = new Intent(myActivity.this,myActivity2.class);
startActivity(i);
When I use myactivity.class or this in the first param,
Eclipse shows an error of Constructor Intent not defined. Why is that, can anyone explain?
myActivity.this == Refrence to context
myActivity2.class == Reference to class, this is its class name
this == It is Current Type, say if you are in Thread then it is Thread Type; if you are in Activity then it is Activity Type; if you are in your custom class say CAR then it is CAR type
When you do the this then you get an error because you must not be in main thread in this you can use getApplicationContext()
When you use myActivity.this It Knows that it will be started from this activitie's context.
Let me give you the answer of:
When i use myactivity.class or this in the first param ,Eclipse shows
an error of Constructor Intent not defined.
Reason you got error is that you are supposed to pass valid parameters to the Intent Constructor that you are trying to invoke.
See this: LINK
Which are
A Context of the application package implementing this class.
The component class that is to be used for the intent.
And as you mentioned, you tried myactivity.class , refering to KITKAT'S answer this parameter is not valid enough to get passed to the Intent constructor.
As for this is concerned, you shouldn't get any compile error, if you are within valid activity context.
The first param is for the current activities context therefore this or Activity.this or getApplicationContext will do. and the second param refers to the class name where you want to move to. That is why
.this in the first param and .class in the second. Hope you got it now.
Maybe you are writing this code in another object ,like in OnClickListener ,thus this is representing the current object of OnClickListener not the MainActivity class.
That's why you should use MainActivity.class to reference to main Activity.
this in this context is representing the object of OnClickListener.
Related
I have broadcastReciever,which i registered in activity. I want to modify existing intent,to start activities with it,but i not understand,how i can delete class from my intent. When i try to do intent.setClass(applicationContext,null) i get compilation error: Null can not be a value of a non-null type Class. But how delete class from existing intent? It's very interesting for me,what class intent have when we create it using standard constructors of intent? Thanks everybody for any help.
setClass is a convenience function for looking up a component and calling setComponent. So you can call setComponent(null) to clear the component. An Intent does not have to have an associated component.
However, it is unusual to need to modify an intent that was already set up for some other task. I can't think of a reason why you would do this instead of creating a new Intent.
I am making an application on AIDE for android, and I'm using an intent to send data from an activity to a normal class.I'm using:
int level= (currentLevel*100)/scale;
Intent i = new Intent(context, caller.class);
i.putExtra("level",level);
context.startActivity(i);
in the class that sends the data ("percentage.class").
int p = getIntent().getIntExtra("level");
in the class that receives the data ("caller.class")
which gives me an error: "Unknown method getIntent()".
What can I do to fix this?
Thanks in advance!
It's easy to say in your question what is the problem. The class in which you are calling getIntent does not inherit the class Activity.
Unlike what other people are saying inheriting Activity is unlikely to give you what you're looking for. What I'm suspecting is that you're calling getIntent in a button or something like this. Since it might be wrapped inside a method that isn't directly pointing to your activity. You should "keep" a pointer to the activity.
Usually, what you are looking for should be in the context. Calling context.getIntent might work if your context is the thing I "believe" it should be. Show more to give us a better idea of what is going on. Because since getIntent is calling from the activity. getIntent is the same as writing this.getIntent but Java implicitely calls function on this and then on the global scope (the thing you import).
If you want to avoid this problem, alway call it from this and when you're calling from within a Handler, you can keep references in your class to the current activity. I'm not so sure but on some object, you should have a function getActivity that will return the activity in which they are located.
you could have something like this. obj.getActivity().getIntent()...
Check this out: What does getActivity() mean?
The getIntent() method belongs to the Activity class. You will get this error if your class does not extend Activity. Try extending Activity and see if it works.
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 have a class which already inherits another class. Now from within this class, I want to start an activity, which needs activity class to be inherited. Which is not possible since my class has already inherited one.
In this situation, whats the best way to start an activity?
Some suggested me to use getBaseContext() method, but its saying "getBaseContext can not be resolved". Could anyone suggest a way...?
create a constructor for this class which takes Activity Instance as arguement.. and if u r creating object of this class in an activity.. pass its instance and then use that to get Context... or u can just say
if its a reciever then its very easy.. reciever gets a context object in its onRecieve method.. which u can use like this
Intent i = new Intent(context, some.class) //activity is an instance of Activity()
i.setFlag(Intent.FLAG_ACTIVITY_NEW_TASK)
context.startActivity(i);
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.