(!)onClick
方法不存在
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.
* 您好,我是新来的,我只是在 Coursera 上尝试类(class),但它似乎已经过时了,而且有些东西没有按预期正常工作。 我一直试图在论坛上搜索以找到答案,并且尝试了一些方法,但似乎我太弱智了,而且还是个新手。 这可能与我的工具:上下文有关吗?
这是我的 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" />
这是我的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");
}
}
}
}
最佳答案
我分析了您的代码,发现您在 onCreate() 方法
中声明了您的导航方法,因此您可以从 xml 中找到它。
如何在 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");
}
}
关于java - onClick 方法不存在,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/49941486/