I have menu xml which included in my activities, im using onClick attribute instead of binding on every startActivity.
My question is, how can I define on the xml onClick attr to call methods which is located on my mainActivity for example?
I thought about something like android:onClick="mainActivity.doSomething" but its doesnt work.
Using onclick in xml is a bad idea. The fact is it only tries to find the method that the current class is in which is using it. One way to do this IF you really want to do it this way is:
startActivity:
public void callOtherMethod(){
mainActivity.doStuff();
}
mainActivity:
public static void doStuff(){
//dosomething.
}
startActivity.xml:
android:onClick="callOtherMethod"
Your doStuff method must also be static, unless you can get a instance of the target method Activity.
I thought about something like
android:onClick="mainActivity.doSomething" but its doesnt work.
It's normal, Android looks for the onClick method declared in the xml layout only in the current activity where that layout file is used(and it wouldn't make sense anyway to look in other activities as those activities could be well destroyed when you call that method).
My question is, how can I define on the xml onClick attr to call
methods which is located on my mainActivity for example?
You should explain what you're trying to do. Accessing methods of an Activity from another Activity should be avoided, it's not the proper way of doing things in Android.
Related
in my app, there are 5 classes and 1 activity. This activity has, as you all know, an OnDestroy() method. In this method I need to remove a test provider which is set up in another class called "mockingclass".
In "mockingclass" I have a method similar to this:
public void mocker()
{
xxx
location.RemoveTestProvider(location.GpsProvider);
xxx
}
While xxx stands for many other functions in this method, when the app is being destroyed I need to call ONLY for that ONE function in within this whole method.
Is there any way to do that at all? If not, what would be your suggestions?
THANKS!
Obviously you can't choose a single line from method to be executed. You need to extract it separate method which will be called separately. Maybe you should look at some injection? Let this call be injected as an delegate for example. In that way you could manipulate what should be called depending on situation.
I am wondering if it is possible to access a button from a non-activity class using an activity class? The button is in a different xml file than the one of the activity class.
Also, is it possible to create options menu in the non-activity class?
Whenever you need to access any UI element from a non-activity class, you need to pass the context to your class. A rough method is:
in your activity's onCreate() method:
myInstance.setContext(this);
and in your Class, you have:
Context mContext;
public void setContext(Context c){
mContext = c;
}
I am wondering if it is possible to access a button from a
non-activity class using an activity class?
Yes, it's possible if the non-activity class can access the methods of an activity class. This can be done by passing the reference of an activity to that non-activity class in the onCreate() method. But remember to release that reference and any other reference to the activity Views when the Activity is destroyed, that is when it's onDestroy() method being called.
The button is in a different xml file than the one of the activity
class.
A button that is not added to an Activity's View Tree can be accessed. You can inflate the layout that contains the button and add the inflated View root to your Activity's ViewGroup.
Also, is it possible to create options menu in the non-activity class?
Yes, it's possible if the non-activity class can access the methods of an activity class.
Anyone can correct me if I'm wrong. But my assumption answer to your question and how I understand what you trying to achieve. Android has made it easy to access controls in other xml files. You have the < include> which you can include other non-activity(fragments) in your activity. Actually it's pretty easy to do this that is the first way to do it. I know they is another way still playing around with your xml files. There is resource to bring non-activity controls to activity layouts with having to do much programmatically. Look at resources available to you on your layout builder.
I have a button that I want to change its value often, so my Activity has a private variable :
private Button p1_button = (Button)findViewById(R.id.firstbut);
This simple line makes my app crash. If I put inside the onCreate it works and I can interact with the button (change text etc).
EDIT : I think I found the reason. I should initialize AFTER setcontentview ?
EDIT: Thank you for the constructive answers. I have now a different problem I removed the initialization and I did it on onCreate and it works (But I keeped the p1_button declaration as a private field). But when I tried to modify the button in a different method of my activity (just changing the text), it crashes again. So the return value of findViewById is "local" to the method where it is called and I should setcontentview in every method that access UI elements ?
Do not call findViewById() until after you call setContentView(). Otherwise, the widget will not exist.
More generally, do not call inherited methods on Activity until after super.onCreate(), unless specifically advised to do so.
It depends where you are calling this line.
http://i.stack.imgur.com/6EQaU.png
The onCreate() method contains a call to setContentView() and before this is called, Android has no idea what to do with your button as it hasn't been inflated yet!
Therefore as a really easy rule of thumb, always make sure setContentView (or if you're dealing with fragments onCreateView()) have been called and completed. Only then will findViewById() work.
If you would like further guidance, please post some code in which the crash occurs.
edit: I tried to add the image properly but don't have enough rep.
To understand this you need to know the Activity lifecycle. You are trying to look a view which has not yet been created by Android.
As per the android lifecycle explained here "http://developer.android.com/training/basics/activity-lifecycle/starting.html". In onCreate() method the activity is created and you can access different views of the activity. If you will try to look for view before onCreate() the app will crash as it does not know whether that view exists or not.
I'm trying to define an onClick handler method in an XML layout for buttons in a Dialog but the app crashes when the buttons are clicked due to NoSuchMethodException (it can't find the method). The method signature is public void searchMovies(View) and I can guarantee that it is spelled consistently in the XML file and in code. This similar question provides a solution by putting the putting the method into the Activity that the Dialog belongs to, but that doesn't work for me.
However, I'm using a FragmentActivity and FragmentStatePagerAdapter, so maybe this is the reason it doesn't work for me? I've tried putting the method into the FragmentActivity, the FragmentStatePagerAdapter, the Fragment and also the anonymous OnClickListener class that handles launching the Dialog. None of them have worked, all give NoSuchMethodException error. Again, I can guarantee that the method is spelled correctly/consistently.
I know that I can do this programmatically, but it would be much more convenient if I could do it the other way. Is it possible? Thanks.
As I said in the comments above, this is the solution (I suppose this is a duplicate, then, but I reckon it still might be useful to someone looking for the solution. I don't mind if someone feels that it should be deleted though).
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.