Calling method available in the activity - android

I am trying to call a method available in a Activity from a java file.
The calling is made fine and the control transfers to the function by this code in the java file.
myActivity my = new myActivty();
myActivity.method1();
Now my problem is i cant able to give a toast message or display the alertdialog, Its giving null pointer exception when im using the Toast message,
07-20 15:13:00.836: ERROR/AndroidRuntime(418): java.lang.NullPointerException
Am i doing right?
Where am i wrong?
Help will be greatly appreciated.

Toast needs Context to show up. You're trying to show toast but you have created Activity without Context. Pass instance of working Activity to create dialog or show toast.

:) This wont work. You will need to pass some kind of a reference from your activity to the other Java class.
More in this here http://groups.google.com/group/android-developers/browse_thread/thread/741caff5a9536859?pli=1

Related

What does the first argument do in the given code:

Toast toast = Toast.makeText(this, "Toast!!!", Toast.LENGTH_SHORT);
toast.setGravity(Gravity.AXIS_PULL_AFTER , 0, 0); toast.show();
what is the role of Gravity.AXIS_PULL_AFTER argument in order to positiong the Toast on the UI.
As per Android documentation:
Make a standard toast that just contains a text view.
Context: The context to use. Usually your Application or Activity
object.
Generally, this will be a reference to the activity calling Toast. However, if you are inside an anonymous class (for example, creating a click listener for a button), you will lose reference to your activity.
The parameter this is the object which tells the Toast where to show it, in your case is this your Activity which extends a Context. Note that this is used a lot around Android widgets. If you are using this inside an anonymous class e.g. an onClickListener(), use YourActivityName.this instead.
There is no clear specification for it according to Google documentation.
Its just a parameter that tells the current activity or application where makeToast function has to show Toast message. current activity or application

StartActivity - Android

I'm facing a problem in starting an activity in Android.
What am i doing?
I have a setOnClickListner in SignUpActivity.class which calls a method called "DoSignUp" located in MethodsParse.class.
What does this "DoSignUp" do?
It literally does the signUp using the values receveid from parameters.
But, as i'm using Parse library and Parse has an exclusive method to do SignUps: "signUpInBackground()", i don't know what to do.
What trouble am i facing?
When i try to do the signUp as an existing User, it works fine (shows me the Toast with: Something went wrong...)!
When i try to do the signUp as a new User, it shows me a error:
But, I tried to debug my app, and when i tried to do the signUp as a new User, the data from it goes to Database, it means, my problem is on startActivity... but i don't know why.
I see some problems
Remove extends Activity from MethodsParse, you use it as a regular Java object, hence it should not extend Activity
Looks like you initialize Parse inside MethodsParse. This should be done in an extends Application object, see: https://parse.com/questions/should-developers-call-parseinitialize-in-oncreate-of-every-activity-android or How to know when Parse.initialize() has already been called?
Looking at your error I strongly believe 1) to be the cause of your error. You call startActivity(..,..) and the method startActivity() is only available because you extend Activity. The problem is, since you call the login method directly from another activity MethodsParse has not started it's lifecycle (by the OS calling onCreate()), making startActivity() fail as it has no context.
Long answer short, change:
startActivity(..,..)
To:
currentCtx.startActivity(..,..)
The exception is nullpointerexception, one of the variable that you are passing is null. This error could be due to nextClass or currentCtx is null.

Unknown method getIntent

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.

getApplicationContext to Activity CastException

I have to send getApplicationContext to a method, and in this method, I have to cast context to activity to use
getWindowManager().getDefaultDisplay().getMetrics(dm)
But it show an exception
Caused by: java.lang.ClassCastException: android.app.Application cannot be cast to android.app.Activity.
Why getApplicationContext?because I check my application update in main activity and don't want to limit it to an Activity (I want lifecycle).
How handle it?
UPDATE
I have FN.java that all static functions are here. one method job (getUniqueID) is to get device ID and screen width and height and etc. IN another hand, I have a Update.java that's for checking application update.
In this class, I use getUniqueID.
For using Update class in my main activity, I do it like :
if(updateInstance == null)
updateInstance = new Update(getApplicationContext());
If I pass this instead of getApplicationContext, I know it's ok, but I want use getApplicationContext because maybe user click on some menu item and go to another activity or close application immediately; And I want to show notification Anyway.
I just use
this
and everything works fine.
And for update check use singleton.

Invoke a dialog from another activity

I have Activity A which defines a dialog using AlertBuilder.create etc. This Activity invokes the dialog using showDialog(dialogID). dialogID is declared and recognized in all classes/activities. Everything works perfectly in Activity A.
My question is when trying to invoke this same dialog -- showDialog(dialogID) -- from another Activity (Activity B) the application crashes. Can anyone help?
How to invoke a dialog from multiple activities?
Thanks in advance.
Andy
My question is when trying to invoke
this same dialog --
showDialog(dialogID) -- from another
Activity (Activity B) the application
crashes.
How do you invoke the dialog from Activity B? Just to let you know, you do not instiante activities (so no new ActivityA().showDialog(id).
What you can do is
Create a class that extends AlertDialog.Builder and accept a Context parameter in the constructor. You can customize the text, buttons and other things.
From your activity, in your onCreateDialog, you can just instantiate your class and call create() on it. And your class will be accessible from any activities.
I don't think you can accomplish what you want without hooking up a similar entry point in your new activity.
showDialog(int id)
The id is unique within the activity that launches it. So if two activties A and B both call showDialog(1); That will do something different in each unless someone has coded the same code path for them in their onDialogCreate() and onPrepareDialog methods. So in your onDialogCreate of the original activity, that code will have to exist in both activities. You can sometimes get way with creating a new Dialog type that does all the initialization internally based on a given context and just call show() on it. The problem with this solution usually arises when the context is no longer valid and you need to dismiss or show it. Basically though when using showDialog() it's on a per activity basis.
I don't think that's possible.
Dialog built in Activity A, belongs to Activity A.
No matter if you store its ID in a global data space. It won't be available to be used in Activity B.
You'll have to create another Dialog in Activity B

Categories

Resources