Calling an empty function from Android layout file crashes App - android

I have a checkbox in the Android layout file. When the checkbox is clicked i call an empty function in the activity class. This causes the app to stop working. Why is this?

I am assuming that you are using the "onclick" attribute in your XML.
If you are receiving a MethodNotFound exception, it could be one of two things:
You have a typo in either your Activity's method name or your XML's method name, or...
The visibility of your method in your Activity is not public.
When specifying onclick values in XML, the method should look like this in your activity:
public void myOnClickMethod(View v) { ... }

Related

why my playClick and stopClick button is never used?

this is my issues, so my button handler on activity stated my method was never used but i'm already create the button on XML
activity:
XML:
In your XML file change the tools:context in the root LinearLayout to the path of your Activity because this data is being used to bind the onClick action
As I can tell from the screenshots it should be changed to
tools:context=".LanderActivity"

Setting onClick function in Android Studio (Kotlin) shows an error message

I am building a little app in Android Studio using Kotlin. In one of my activities I have a button which when pressed I want it to call a function. For this I use the onClick attribute in the activity_main.xml android:onClick="". What I want to be able to do is display some information based on the user inputs and update a textView in the same activity.
What I am doing now is setting the onClick attribute to android:onClick"getResult".
The issue I run into is that it requires to have a parameter 'public void getResult(android.view.View)' and what I don't know how to do is what parameter to pass as android.view.view in my getResult() function when I call it in my code.
Here is the complete signature of getResult()
fun getResult(view: View){
//do stuff
}
In your case you have to use:
/** Called when the user touches the button */
fun getResult(view: View){
//view.method()
//(view as Button).method()
}
The method you declare in the android:onClick attribute must have a signature exactly as shown above. Specifically, the method must:
The value for this attribute must be the name of the method you want to call in response to a click event.
The method is hosted in the Activity
Be public
Return void
Define a View as its only parameter. This will be the View that was clicked.
You can also declare the click event handler programmatically settign the View.OnClickListener:
val button = findViewById<Button>(R.id.button)
button.setOnClickListener {
//..
}

Use a dynamic Activity name while calling it's method from a fragment

Ok, so I've a Fragment inside which I use getActivity().getClass().getSimpleName() to get the name of the activity that contains it.
Now, I've a method called sampleMethod() inside that activity, and to call it from the fragment I use ((MyActivity) getActivity()).sampleMethod(); This works fine as well.
My question is that how can I use the activity name in the statement ((MyActivity) getActivity()).sampleMethod(); dynamically. Obviously, I do get the name from getActivity().getClass().getSimpleName().
So what I want is something like
`((getActivity().getClass().getSimpleName()) getActivity()).sampleMethod();
Syntactically, the above is incorrect. What's the correct way?
All the Activities that include this fragment should implement an interface, let's say
interface Sample {
public void sampleMethod();
}
then in your fragment
((Sample)getActivity()).sampleMethod();

Method mapping not using an interface method

How does Android's View Objects call a method inside my activity? For example, when you generate a view (like a button) dynamically, you have to add an click listener and then your activity will implement click listener and then the button object will have a reference to your activity through the interface.
My question though is when you create buttons in the XML file, you can specify an attribute "onClick" and then give a custom method name such as public void button1pushed(View v) how does the button object reference my activity if my activity doesn't implement anything?

Going from a PreferenceScreen to a DialogPreference

My application has a setting menu which is actually a PreferenceActivity.
When it's created, if a boolean value is not set I want to go to the DialogPreference which sets that.
I tried doing it with an intent but the application force closed with this error msg:
E/AndroidRuntime( 239):
android.content.ActivityNotFoundException:
Unable to find explicit activity class
{com.xxxx/com.xxxx.xxxxPreference};
have you declared this activity in
your AndroidManifest.xml?
How should I do this? It's ok to add that DialogPreference to the manifest?
A DialogPreference isn't an Activity in its own right. It's just a Preference which displays a Dialog when clicked.
The problem is that there's no obvious way programmatically click a Preference. However, since you're using DialogPreference you've already got you own subclass of it. So we can solve our problem by adding the following method to your subclass of DialogPreference:
//Expose the protected onClick method
void show() {
onClick();
}
Then in the onCreate() of your PreferencesActivity you'll have something like this to load the preferences from your XML file:
// Load the preferences from an XML resource
addPreferencesFromResource(R.xml.preferences);
After that you can put some code like this:
booleanProp = true; //set this to the value of the property you're checking
if (! booleanProp) {
//Find the Preference via its android:key
//MyDialogPreference is your subclasss of DialogPreference
MyDialogPreference dp = (MyDialogPreference)getPreferenceScreen().findPreference("dialog_preference");
dp.show();
}
This is a bit of hack, as exposing protected methods isn't ideal, but it does work.
Another option would be to replace the Dialog with a PrefenceActivity which contained all the options you wish to maintain and then you could launch it via an Intent, but I'm assuming there's a good reason that you want your own custom Dialog with a specific layout. If you do want a second PreferenceActivity you can add it to your preferences XML file as follows:
<PreferenceScreen
android:title="#string/title_of_preference"
android:summary="#string/summary_of_preference">
<intent android:action="your.action.goes.HERE"/>
</PreferenceScreen>
To start an activity with an Intent, the activity must be in the Android manifest. Just add a line like:
<activity android:name=".path.to.MyActivity"/>

Categories

Resources