java – 按钮onClick错误…无法找到方法
作者:互联网
我似乎无法从我的Button开始一个新的Activity,我在这里搜索了答案,但似乎无法解决问题.我希望有人有一个简单的解决方案.提前致谢.
这是错误:
E/AndroidRuntime﹕ FATAL EXCEPTION: main
java.lang.IllegalStateException: Could not find a method openSearch(View) in the
activity class dk.mathias.splitcab.MainActivity
for onClick handler on view class android.widget.Button with id 'btnStartSearch'
MainActivity.java
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
}
public void openSearch(){
Intent openSearchIntent = new Intent(MainActivity.this, StartSearch.class);
startActivity(openSearchIntent);
}
AndroidManifest.xml中
<activity
android:name=".StartSearch"
android:label="@string/title_activity_start_search" >
<intent-filter>
<action android:name="dk.mathias.splitcab.STARTSEARCH" />
<category android:name="android.intent.category.DEFAULT" />
</intent-filter>
</activity>
activity_main.xml中
<Button
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="@string/StartSearch"
android:id="@+id/btnStartSearch"
android:layout_alignParentBottom="true"
android:layout_alignParentLeft="true"
android:layout_alignParentStart="true"
android:layout_alignRight="@+id/tvWelcome"
android:layout_alignEnd="@+id/tvWelcome"
android:onClick="openSearch"
/>
StartSearch.java
public class StartSearch extends Activity {
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_start_search);
}
}
解决方法:
问题出在您的方法签名中
public void openSearch(){
它应该有一个,只有一个参数,这是一个视图.
将其更改为
public void openSearch(View v){
v显然可以是你想要的任何东西,但你应该让它像v,view等有意义……
In order for this to work, the method must be public and accept a View as its only parameter.
See this answer有关添加按钮和OnClick的更详细说明
标签:buttonclick,android,java,android-activity 来源: https://codeday.me/bug/20191003/1849675.html