Reputation: 11
(!)onClick
method does not exist
Corresponding method handler 'public void navigate(android.view.View)' not found The onClick attribute value should be the name of a method in this View's context to invoke when the view is clicked. This name must correspond to a public method that takes exactly one parameter of type View. Must be a string value, using '\;' to escape characters such as '\n' or '\uxxxx' for a unicode character.
* Hello I am new here, I am simply trying courses on Coursera but it seems like it's pretty outdated, and some things are not working properly as it is supposed to. I have been trying to search here on the forums to find an answer, and I have tried a couple of things, but it seems like I am too retarded and a newbie. Could it be something to do with my tools:context?
Heres my xml:
<RelativeLayout
xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:padding="16dp"
tools:context="MainActivity"
android:background="#ffffff">
<NumberPicker
android:id="@+id/numberPicker"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentTop="true"
android:layout_alignParentLeft="true"
android:layout_alignParentStart="true"
/>
<Button
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="OK"
android:id="@+id/button"
android:layout_below="@+id/numberPicker"
android:layout_alignParentLeft="true"
android:layout_alignParentStart="true"
android:onClick="navigate"/>
<WebView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:id="@+id/webView"
android:layout_below="@+id/button"
android:layout_alignParentLeft="true"
android:layout_alignParentStart="true" />
Heres my java:
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.webkit.WebView;
import android.webkit.WebViewClient;
import android.widget.NumberPicker;
import android.view.View;
public class MainActivity extends AppCompatActivity {
NumberPicker possibilities;
WebView webView;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
possibilities = (NumberPicker) findViewById(R.id.numberPicker);
webView = (WebView) findViewById(R.id.webView);
String[] possibilitiesStrings = {
"Android",
"Checklist text-input fields",
"Coursera",
"Supelec",
};
possibilities.setDisplayedValues(possibilitiesStrings);
possibilities.setMinValue(0);
possibilities.setMaxValue(possibilitiesStrings.length - 1);
/**
* called when the OK button from activity_main.xml is clicked
* @param v the View which triggered the method call: the OK button
*/
public void navigate(View v) {
int choice = possibilities.getValue();
webView.setWebViewClient(new WebViewClient());
if (choice == 0)
webView.loadUrl("file:///android_asset/android.html");
else if (choice == 1)
webView.loadUrl("file:///android_asset/checklist.html");
else if (choice == 2)
webView.loadUrl("http:///www.coursera.org");
else if (choice == 3)
webView.loadUrl("file:///android_asset/supelec.html");
}
}
}
}
Upvotes: 1
Views: 3310
Reputation: 688
I analyzed your code and I see you declare your navigate method in your onCreate() method
so is why you can find it from xml.
Example how declare method to use in xml
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.webkit.WebView;
import android.webkit.WebViewClient;
import android.widget.NumberPicker;
import android.view.View;
public class MainActivity extends AppCompatActivity {
NumberPicker possibilities;
WebView webView;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
possibilities = (NumberPicker) findViewById(R.id.numberPicker);
webView = (WebView) findViewById(R.id.webView);
String[] possibilitiesStrings = {
"Android",
"Checklist text-input fields",
"Coursera",
"Supelec",
};
possibilities.setDisplayedValues(possibilitiesStrings);
possibilities.setMinValue(0);
possibilities.setMaxValue(possibilitiesStrings.length - 1);
}
/**
* called when the OK button from activity_main.xml is clicked
* @param v the View which triggered the method call: the OK button
*/
public void navigate(View v) {
int choice = possibilities.getValue();
webView.setWebViewClient(new WebViewClient());
if (choice == 0)
webView.loadUrl("file:///android_asset/android.html");
else if (choice == 1)
webView.loadUrl("file:///android_asset/checklist.html");
else if (choice == 2)
webView.loadUrl("http:///www.coursera.org");
else if (choice == 3)
webView.loadUrl("file:///android_asset/supelec.html");
}
}
Upvotes: 2