从开始按钮,点击新活动 [英] Starting new activity with button click
问题描述
我似乎无法开始新的活动
从我的按钮
,我已经在这里寻找答案,但似乎无法来解决问题。我希望有人有一个简单的解决方案。在此先感谢。
下面是错误:
E / AndroidRuntime:致命异常:主要
java.lang.IllegalStateException:找不到的一个方法的OpenSearch(查看)
活动类dk.mathias.splitcab.MainActivity
为onclick处理程序的视图类android.widget.Button ID为'btnStartSearch
MainActivity.java
保护无效的onCreate(包savedInstanceState){
super.onCreate(savedInstanceState);
的setContentView(R.layout.activity_main);
}
公共无效的OpenSearch(){
意图openSearchIntent =新的意图(MainActivity.this,StartSearch.class);
startActivity(openSearchIntent);
}
AndroidManifest.xml中
<活动
机器人:名称=开始搜索
机器人:标签=@字符串/ title_activity_start_search>
<意向滤光器>
<作用机器人:名称=dk.mathias.splitcab.STARTSEARCH/>
<类机器人:名称=android.intent.category.DEFAULT/>
&所述; /意图滤光器>
< /活性GT;
activity_main.xml
<按钮
机器人:layout_width =WRAP_CONTENT
机器人:layout_height =WRAP_CONTENT
机器人:文本=@字符串/开始搜索
机器人:ID =@ + ID / btnStartSearch
机器人:layout_alignParentBottom =真
机器人:layout_alignParentLeft =真
机器人:layout_alignParentStart =真
机器人:layout_alignRight =@ + ID / tvWelcome
机器人:layout_alignEnd =@ + ID / tvWelcome
机器人:的onClick =OpenSearch的
/>
StartSearch.java
公共类开始搜索延伸活动{
@覆盖
保护无效的onCreate(包savedInstanceState){
super.onCreate(savedInstanceState);
的setContentView(R.layout.activity_start_search);
}
}
现在的问题是在你的方法签名
公共无效的OpenSearch(){
它应该有一个,且只有一个参数,这是一个'查看
将其更改为
公共无效的OpenSearch(视图v){
v
明明可以是你希望它是什么,但你应该让一些有意义的事情,比如 v
,视图
等等...
为了这个工作,方法必须是公开的,接受一个View作为其唯一的参数。
<一个href="http://stackoverflow.com/questions/18319711/android-eclipse-which-is-the-best-way-to-add-a-button/18319759#18319759">See这个答案添加按钮进行更详细的说明
和的OnClick
I can't seem to start a new Activity
from my Button
, I have searched here for answers, but can't seem to resolve the problem. I hope someone has an easy solution. Thanks In advance.
Here is the error:
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);
}
}
The problem is in your method signature
public void openSearch(){
it should have one, and only one param, which is a `View.
Change it to
public void openSearch(View v){
v
obviously can be anything you want it to be but you should make it something meaningful like v
, view
, etc...
In order for this to work, the method must be public and accept a View as its only parameter.
See this answer for a more detailed description of adding Buttons
and OnClick
这篇关于从开始按钮,点击新活动的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!