(!)onClick
方法不存在
未找到相应的方法处理程序'public void navigate(android.view.View)'onClick属性值应该是此View的上下文中方法的名称,以便在单击视图时调用。此名称必须对应于只接受View类型的一个参数的公共方法。必须是字符串值,使用'\;'转义unicode字符的'\ n'或'\ uxxxx'等字符。
* 您好,我是新来的,我只是在Coursera上尝试课程,但似乎它已经过时了,而且有些东西不能正常运行。 我一直试图在论坛上搜索找到答案,我尝试了几件事,但似乎我太迟钝了,而且是新手。 这可能与我的工具有关:上下文?
继承我的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");
}
}
}
}
答案 0 :(得分:2)
我分析了您的代码,并且我看到您在onCreate() method
中声明了导航方法,因此您可以从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");
}
}