onClick attribute in XML linking to method in Activity class - android

There are quite a few questions about this subject, but could not find any with the specific problem I have...
In my layout.xml, I use the android:onClick tag for a Button to call the right onClickListener. I get the error :
java.lang.IllegalStateException: Could not find a method handle_cancel(View) in the activity class com.matthieu.HelloWorldApplication for onClick handler on view class android.widget.Button with id 'button_cancel'
I have that method implemented in the Activity, but it is looking for it in the class that extends Application... I don't understand why. The View and all that is setup only in the Activity.
If anyone needs, here is the declaration of that method (in my activity, NOT in HelloWorldApplication):
public void handle_cancel(View v) {
// do something useful here
}
Edit (from adamp request)... and probably answering my own question :
Here is part of the code where that layout is used...
public class AddVocabularyActivity extends Activity
{
#Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.top); // that layout contains an empty LinearLayout id/main_content
}
private some_other_function() {
LinearLayout main_content = (LinearLayout) findViewById(R.id.main_content);
main_content.removeAllViews();
View.inflate(getApplicationContext(), R.layout.hello, main_content); // layout.hello is the one containing the button
}
// some other stuff
}
While copy/paste this code, I am guessing the problem is that I used getApplicationContext to inflate the View with that Button...

As mentioned in my edit, changing the getApplicationContext() with the Activity context fixes it...

The convention works like this:
In the layout xml file, you give this attribute:
android:onClick:"methodname"
Then, inside a class, you define a method like this:
public void methodname(View v){
//your method code
}
Any other way of doing this is not documented. If you need parameters, just call another method inside that method.

Related

Cannot find symbol variable google_btn

I want to add google authentication, I am new to android studio and I have the error "Cannot find symbol variable google_btn". Here is my code.
findViewById(R.id.google_btn).setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
Sign_in_with_gmail();
}
});
Here is a screenshot.
https://i.stack.imgur.com/QsN4H.png
It seems like an issue with not finding the link in the xml. Which can be down to a few factors. The first is you have not declared the id in the xml. Second you are referencing the xml content when you view it.
You need to firstly identify where you are accessing your google_btn from.
If it is inside your activity_main.xml check it is created with:
android:id="#+id/google_btn"
//After setting content view
findViewById(R.id.google_btn).setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
Sign_in_with_gmail();
}
});
Then in your java code for the activity you need to reference that layout to link it.
So in your java code set the view:
setContentView(R.layout.activity_mainmenu);
This activity should also be inside of your manifest.
After you have gotten the content view you can get the reference to the button
First of all, make sure that you have this button in your layout file (probably res->layout->activity_main.xml). If you have it set its id to google_bnt. Finally, the piece of your code, that sets the OnClickListener should be in onCreate() method in your activity class (probably MainActivity.java in your case)

how to get the xml id used in first class to use in second class in android

I'm getting the android xml id in first Activity
(e.g)
setContentView(R.layout.sample);
button1=(Button)findViewById(R.id.b_one);
button2=(Button)findViewById(R.id.b_two);
Now the problem is how can i use the same button in second Activity without using findViewById().
(i.e) I don't want to access xml in second Activity rather than that i have to access those button ID from the first Activity itself.
I'm trying this to create a common header.
Please help me.
You can't, because your buttons from two different activities will not share the same instance.
What you can do instead, if you want to have some common code for your header is to delegate this code in a static method of a third class. For example :
public class MyHeader {
private Button button1,button2;
public MyHeader(Activity source) {
this.button1 = (Button)source.findViewById(R.id.b_one);
this.button2 = (Button)source.findViewById(R.id.b_two);
// ...
}
public Button getHeaderButtonOne() { return button1; }
// And so on...
}
and in your activities, keep an instance of this class (initialized with :
private MyHeader header;
// ... in onCreate() method
header = new MyHeader(this);

Accessing the setters and getters methods

I have an A activity that uses a setContentView(R.layout.activityA) method to set a layout. An activityA layout consists of a customView. My customView has a bunch of setters and getters. How can I access them from A activity? When I create an instance of customView in an acitivity A then it works but the customView is created twice: once from the setContentView and the second time when I create a new instance of it. Is there another way of accessing those method? Please advise. Thanks.
Have you tried something like this in your Activity's code:
#Override
public void onCreate(Bundle state){
super.onCreate(state);
setContentView(R.layout.activityA);
CustomView customView = (CustomView)findViewById(R.id.customviewId);
Something x = customView.someGetterMethodX();
...etc...
}
You can use Java Reflection to read attributes and call methods.
You don't have to create it twice, simply find your custom view id and assign it to a CustomView reference. Something like this:
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.theLayout);
CustomView customView = (CustomView) findViewById(R.id.customViewId);
customView.setSomething(someValue);
}

adding onClickListener from separate class to xml layout

I have separate file with listener.
I want to attach that listener to button, using xml Android:onClick, but after compilling i get error
01-11 14:35:35.560: E/AndroidRuntime(4682): java.lang.IllegalStateException: Could not find a method Btnlistener(View) in the activity class com.android.app.Activity for onClick handler on view class android.widget.Button with id 'btn1'
Is there any fancy way to import my listener class, in such way that i could use Android:onClick in xml.
Here is what I suggest:
Have a BaseActivity with the code for your listener, in a normal method
public void buttonClicked(View view) {
// put here what your listener did
}
And make all your activities that need this listener extends BaseActivity. You can define in your layouts the following xml element for buttons:
android:onClick="buttonClicked"
when you set onClick in xml then the activity class which inflate the xml must declare onClick event as a method, for example:
in XML:
onClick="btnAdd"
and in your activity:
public void btnAdd(View v){
//your code when the button click event is captured
}

Android findViewById throws NullPointerException

I have a ViewFlipper defined in a separate class. It looks like this:
public class Flipper extends Activity {
public ViewFlipper view_flipper;
/* Constructor */
public Flipper(int flipper_id) {
view_flipper = (ViewFlipper) findViewById(flipper_id);
}
...
Then in another Activity's onCreate() method I am instantiating the Flipper like this:
private Flipper flipper;
flipper = new Flipper(R.id.login_screen_flipper);
However, I get a NullPointerException in the constructor of Flipper. When I debug, I see that flipper_id has a valid id. What am I doing wrong?
You can't create Activities like this.
To create an activity you must do it using Intents. And you have to define the layout in an XML file and attach it using setContentView(R.layout.layout_name);
Have a look at this to start learning: https://developer.android.com/training/index.html
UPDATE:
OK, it's unclear of what you are accomplishing with the info of your question.
If you are extending the behaviour of ViewFlipper I suggest you extend it:
package your.package;
public class MyFlipper extends ViewFlipper {
// ...
}
Then use it in your xml like this:
<your.package.ViewFlipper
[...] parameters [...] />
And finally when you implement it you can do it like this:
public class YourActivity extends Activity {
public ViewFlipper viewFlipper;
#Override
protected void onCreate (Bundle savedInstanceState) {
viewFlipper = (ViewFlipper) findViewById(R.id.login_screen_flipper);
}
// ... more activity stuff
}
PS: It's good custom to use the lowerCamelCase notation in Java member variables (or fields in Java)
When you instantiate Flipper in another activity, findViewById is called in the constructor of Flipper. It is a method from Flipper class as it extends Activity and surely its call is not valid in the constructor because you do not set content view as it is normally done in Activity.onCreate. Flipper should not extend Activity and you should pass ViewFlipper in a constructor not id.

Categories

Resources