Hello I'm trying to use object from one of my libaries, but I can't pass my corrent context.
The constructor is:
public AmbilWarnaDialog(final Context context, int color, OnAmbilWarnaListener listener)
And in my class I use this for the constructor:
AmbilWarnaDialog dialog = AmbilWarnaDialog(this, initialColor, new OnAmbilWarnaListener()
{
public void onOk(AmbilWarnaDialog dialog, int[] color) {
// color is the color selected by the user.
}
public void onCancel(AmbilWarnaDialog dialog) {
// cancel was selected by the user
}
});
I get this error:
The method AmbilWarnaDialog(Settings, int, new AmbilWarnaDialog.OnAmbilWarnaListener(){}) is undefined for the type Settings
I also tried getApplicationContex() and Settings.this and it's not working.
My imports are:
import yuku.ambilwarna.AmbilWarnaDialog;
import yuku.ambilwarna.AmbilWarnaKotak;
import yuku.ambilwarna.AmbilWarnaDialog.OnAmbilWarnaListener;
You cannot call a constructor directly, to instantiate a class use the new keyword like this:
AmbilWarnaDialog dialog = new AmbilWarnaDialog(this, initialColor, new OnAmbilWarnaListener() { ...
Try this:
Context context;
private methodName(Context context) {
this.context = context;
}
call method in Activity:
methodName(this);
You are calling this method somewhere inside the Settings class (I am assuming Settings doesn't extend Context). You might be doing something like this:
public class MyClass extends Context {
public myMethod(){
new Settings(){
// the keyword this references to the Settings object instance, not MyClass object instance
// to reference Context use MyClass.this
// like Henry says below, make sure you use the new keyword to initialize objects
AmbilWarnaDialog dialog = new AmbilWarnaDialog(MyClass.this, initialColor, new OnAmbilWarnaListener() {...}
}
}
}
Related
I would like to call a method in an activity and pass an argument to it from a non activity regular class in android.
As i understand, i cant simple use the following code, plus it does not work:
int mySound = 0;
SoundsActivity soundsActivity = new SoundsActivity();
soundsActivity.playSound(mySound);
That code is located in a regular class called "MyAdapter".
There are a few ways you can do this. I can't be specific since you didn't really show any code.
You can't do what you're trying to do though. Activities can't be instantiated like that (as well as anything extending Context), and it won't do what you want.
Use a broadcast.
This will require that you have a Context object passed into your Adapter, which you can do simply by modifying the constructor and adding a global variable:
private Context context;
public MyAdapter(Context context) {
this.context = context;
}
Then you can use that Context to send a local broadcast with your own action:
Intent intent = new Intent("my_custom_action");
intent.putExtra("sound_type", 0);
LocalBroadcastManager.getInstance(context).sendBroadcast(intent);
And receive that action in your Activity to call your method: See Context-registered Receivers
When you construct the Adapter, pass a Context object into it. If you're constructing from an Activity (hopefully SoundsActivity), use this:
MyAdapter adapter = new MyAdapter(this);
Use a callback.
Delcare an interface somewhere:
public interface AdapterCallback {
void onRequestPlaySound(int type);
}
Implement that interface in your Activity:
public class SoundsActivity extends Activity implements AdapterCallback {
//...
#Override
public void onRequestPlaySound(int type) {
playSound(type);
}
//...
}
Add the interface as a parameter in your Adapter's constructor:
private AdapterCallback callback;
public MyAdapter(AdapterCallback callback) {
this.callback = callback;
}
And then use callback.onRequestPlaySound(0); from wherever you need.
When you construct the Adapter, pass your SoundsActivity instance into it. This will only work if you're constructing the Adapter from SoundsActivity:
MyAdapter adapter = new MyAdapter(this);
Pass SoundsActivity directly.
This isn't the cleanest way, nor is it the recommended way, but it will work. In your Adapter:
private SoundsActivity activity;
public MyAdapter(SoundsActivity activity) {
this.activity = activity;
}
And from SoundsActivity:
MyAdapter adapter = new MyAdapter(this);
Then just call activity.playSound(0); where you need to.
I have the following class and i try to get context so as to send an intent to another activity.
public class CloudDocumentTextRecognitionProcessor
extends VisionProcessorBase<FirebaseVisionCloudText> {
public Context mContext;
private FirebaseVisionCloudDocumentTextDetector detector;
public CloudDocumentTextRecognitionProcessor() {
super();
detector = FirebaseVision.getInstance().getVisionCloudDocumentTextDetector();
}
public CloudDocumentTextRecognitionProcessor(Context context) {
this.mContext = context;
}
#Override
protected Task<FirebaseVisionCloudText> detectInImage(FirebaseVisionImage image) {
return detector.detectInImage(image);
}
#Override
protected void onSuccess(
#NonNull FirebaseVisionCloudText text,
#NonNull FrameMetadata frameMetadata,
#NonNull GraphicOverlay graphicOverlay) {
graphicOverlay.clear();
Intent i = new Intent(mContext, ResultActivity.class).setFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
i.putExtra("key", text.getText());
mContext.startActivity(i);
}
}
But i get an error in the line where i set the intent:
"Attempt to invoke virtual method 'java.lang.String android.content.Context.getPackageName()' on a null object reference"
I ve also tried MyApplication.getContext() instead of mContext, but with no results.
Any ideas?
This class does not inherit any Android component that has a Context, so you have to inject it yourself. You have to instantiate the class with the constructor that takes a Context as a parameter, and pass it in from an Activity or other Android component that has a context/access to the app context.
Something like (pseudo-code):
Class MyActivity
{
...
imageProcessor = new CloudDocumentTextRecognitionProcessor(this);
// or imageProcessor = new CloudDocumentTextRecognitionProcessor(this.getApplicationContext());
}
How to choose the context?
If the CloudDocumentTextRecognitionProcessorinstance is supposed to exist throughout the whole lifetime of your app, use getApplicationContext();
If the CloudDocumentTextRecognitionProcessorinstance is guaranteed to only exist during the lifetime of the Activity, use this.
You already have a setter for the mContext field and you can use the secondary constructor for the class that passes the context.
Initialize the class object from your activity like this:
CloudDocumentTextRecognitionProcessor imageProcessor = new CloudDocumentTextRecognitionProcessor(this);
or
CloudDocumentTextRecognitionProcessor imageProcessor = new CloudDocumentTextRecognitionProcessor(getApplicationContext());
I need to start AsyncTask in UI thread, but the Constructor has (MainActivity parentActivity)
parametr. I don't really understand why it should be implemented and how I must pass it.
Here Eclipse says "Cant resolve MainActivity to a variable." Same for Activity.MainActivity.
new DownloaderTask(MainActivity).execute();`
And the constructor.
public DownloaderTask(MainActivity parentActivity) {
super();
mParentActivity = parentActivity;
mApplicationContext = parentActivity.getApplicationContext();
}
Change this line...
new DownloaderTask(MainActivity).execute();
to this...
new DownloaderTask(MainActivity.this).execute();
And you are passing Context of MainActivity not the activity...so in DownloaderTask() constructor, the parameter will be Context type not MainActivity...The constructor should look like as below...
public DownloaderTask(Context context) {
super();
mApplicationContext = context;
}
you can call like following if you are calling directly from the MainActivity
new DownloaderTask(this).execute();
or if you are callling from an inner class you can call like
new DownloaderTask(MainActivity.this).execute();
I have to make an application where I have to show a list of names in popup.
I have used array-list to fetch the values from database, but I cannot put it in array-adapter.
here is my code:
public class Calculator_new_Pop extends Dialog implements View.OnClickListener{
... // rest of the code
ArrayList<String> wallAreas=new ArrayList<String>();
wallAreas=GenericDAO.getWallAreas(room_id);//to fetch the values from databases
ArrayAdapter<String> new_adapter = new ArrayAdapter<String>(Calculator_new_Pop.this,android.R.layout.simple_list_item_1,wallAreas);
_ltvw.setAdapter(new_adapter);
... // rest of the code
}
the error is
"The constructor ArrayAdapter(Calculator_new_Pop, int, ArrayList) is undefined"
Can anyone help me out?
Use activity context
ArrayAdapter<String> new_adapter = new ArrayAdapter<String>(ActivityName.this,android.R.layout.simple_list_item_1,wallAreas);
You can pass the activity context to the constructor of Calculator_new_Pop and use the same instead of Calculator_new_Pop.this
Edit
Context mContext;
public Calculator_new_Pop(Context context)
{
mContext = context;
}
Then
ArrayAdapter<String> new_adapter = new ArrayAdapter<String>(mContext,android.R.layout.simple_list_item_1,wallAreas);
Have a look at the constructor of ArrayAdapter.
http://developer.android.com/reference/android/widget/ArrayAdapter.html
Refer the link
link
ArrayAdapter needs a context as a parameter, you are supplying Dialog instance which is not the Context type, this is the reason why an error is shown. Instead of Dialog instance get the activity context.
You can pass the activity context in the constructor of the dialog, and hence supply that context in the ArrayAdapter.
public class Calculator_new_Pop extends Dialog implements View.OnClickListener{
Context mContext =null;
public Calculator_new_Pop(Context c ){
this.mContext = c;}
.............
.............
.............
ArrayList<String> wallAreas=new ArrayList<String>();
wallAreas=GenericDAO.getWallAreas(room_id);//to fetch the values from databases
ArrayAdapter<String> new_adapter = new ArrayAdapter<String>(mContext,android.R.layout.simple_list_item_1,wallAreas);
_ltvw.setAdapter(new_adapter);
.......
}
When instantiating dialog from activity, pass this.
i am getting 'null' in "mContext"
so i tried this..
public Calculator_new_Pop(Activity parent) {
// TODO Auto-generated constructor stub
super(parent);
this._act = parent;
//
}
_ltvw.setAdapter(new ArrayAdapter<String>(_act,android.R.layout.simple_list_item_multiple_choice,data));
it worked..
I want to create a single class which I can call when I need to show an AlertDialog with the parameters and son on I want.
The problem is I dont know if that class have to be an Activity... the alertDialog needs an context, but I can send the current one, because what I want is to show the alert on the actual activity (not to create a new one, I want to show the alert on the actual screen). But I cant get it. I get errors sending the context of the actual activity...
Only I get working it when I create that class like an Activity, but with that, the alertDialog appears alone without the actual screen behind.
What Can I do? I don't know if I understand the contexts...
Thanks
Your class does not need to extend anything to produce a dialog. You can try this way to produce a static method that creates a dialog for you.
Make sure when you call your method you use THIS and not getApplicationContext()
MyDialogClass.getDialog(this); //good!
MyDialogClass.getDialog(getApplicationContext()); //results in error
That is likely the cause of your error
Example class:
public class MyDialogClass
{
public static AlertDialog getDialog(Context context)
{
Builder builder = new Builder(context);
builder.setTitle("Title").setMessage("Msg").setCancelable(false)
.setPositiveButton("Yes", new DialogInterface.OnClickListener() {
public void onClick(DialogInterface dialog, int id)
{
}
}).setNegativeButton("No", new DialogInterface.OnClickListener() {
public void onClick(DialogInterface dialog, int id)
{
}
});
return builder.create();
}
}
AynscTask does not need the Context; and no, it doesn't need to be an activity.
http://developer.android.com/reference/android/os/AsyncTask.html
Anyways, you should be able to get the context anytime with no problems. Just do this:
public class MyApplication extends Application{
private static Context context;
public void onCreate(){
super.onCreate();
context = getApplicationContext();
}
public static Context getAppContext() {
return context;
}
}
Then you can get the context from wherever you want with MyApplication.getAppContext(); and pass it on and it should work.