Can't use ArrayAdapter in a custom dialog.
This is the error I am getting
You're getting that error because the ArrayAdapter constructor requires the first argument to be a Context object. Since you're calling the constructor from inside an OnClickListener, this refers to the listener, not your activity (i.e. not a Context).
You can qualify the this keyword with the name of your outer class. Assuming that code is written inside of MainActivity, you could write MainActivity.this instead.
Using this won't work since you are in the scope of the OnClickListener.
You should try getContext() or getApplicationContext() instead of this.
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).
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.
I created a class that inherits from SurfaceView. I would like to pass an argument to its constructor while calling findViewById(). Is it possible? How? How to handle it if not?
this.surfaceView = (TubeSurfaceView) findViewById(R.id.surfaceView);
Thank you.
Nope. findViewById is used on already instantiated views to find already instantiated children. Either you need to create the surface view programatically (in which case you wouldn't need to call findViewById), or you need to make whatever you want to pass via constructor an xml parameter.
How to get the context of the project which is under test.I have a class which is singleton,To create object for this i need to call one constructor of this class in setUp() method of testcase class,but i need to pass some context as a parameter to call that constructor, but i dont know how to get the context of this testcase class.I got confused whether i shuld pass the context of testcase class or the class which is under test.could any one help in this please.Could any one help me in this
you can get getApplicationContext() or getBaseContext or simply this!
MockContext wouldn't be a good choice, since there are no methods implemented. Every method call will give you an exception.
There is an IsolatedContext which has some stub methods implemented, but still throws a few exceptions.
If your class is extending AndroidTestCase you can simply call getContext() to receive an instance of Context.
Never used it myself, but the answer is probably here :
http://developer.android.com/reference/android/test/mock/MockContext.html
I am calling a class's member function from another class. So,I am creating an object of the class for which i have to call constructor. Within that constructor, I need to call onc reate method of my activity class.how can I do it?
Do not call onCreate from your constructor. The system itself will call onCreate for you.
Take a look at the Activity Lifecycle for more information of how onCreate is called.
Well,i presume u r trying to call a constructor in an activity.If that is the case then some thing is wrong in the way u have designed ur project.For more details check out these links
Creating an object of Android Activity class and Android - Activity Constructor vs onCreate
All method's of Activity class should be called within activity only . write your own methods for whatever operation you need to perform and pass data from other Activity/class by various data transfer algorithms .