我发现这个问题很普遍,我搜索它,但没有回应我正在发生的事情。
以下是Log Cat:
05-10 19:42:31.564: E/AndroidRuntime(2839): FATAL EXCEPTION: main
05-10 19:42:31.564: E/AndroidRuntime(2839): Process: com.blabla.quiztest, PID: 2839
05-10 19:42:31.564: E/AndroidRuntime(2839): java.lang.IllegalStateException: **Could not find a method setupMessageButton(View) in the activity class com.blabla.quiztest.MainActivity for onClick handler on view class android.widget.Button with id 'button1'**
05-10 19:42:31.564: E/AndroidRuntime(2839): at android.view.View$1.onClick(View.java:3810)
05-10 19:42:31.564: E/AndroidRuntime(2839): at android.view.View.performClick(View.java:4438)
05-10 19:42:31.564: E/AndroidRuntime(2839): at android.view.View$PerformClick.run(View.java:18422)
05-10 19:42:31.564: E/AndroidRuntime(2839): at android.os.Handler.handleCallback(Handler.java:733)
05-10 19:42:31.564: E/AndroidRuntime(2839): at android.os.Handler.dispatchMessage(Handler.java:95)
05-10 19:42:31.564: E/AndroidRuntime(2839): at android.os.Looper.loop(Looper.java:136)
05-10 19:42:31.564: E/AndroidRuntime(2839): at android.app.ActivityThread.main(ActivityThread.java:5017)
05-10 19:42:31.564: E/AndroidRuntime(2839): at java.lang.reflect.Method.invokeNative(Native Method)
05-10 19:42:31.564: E/AndroidRuntime(2839): at java.lang.reflect.Method.invoke(Method.java:515)
05-10 19:42:31.564: E/AndroidRuntime(2839): at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:779)
05-10 19:42:31.564: E/AndroidRuntime(2839): at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:595)
05-10 19:42:31.564: E/AndroidRuntime(2839): at dalvik.system.NativeStart.main(Native Method)
05-10 19:42:31.564: E/AndroidRuntime(2839): Caused by: java.lang.NoSuchMethodException: setupMessageButton [class android.view.View]
05-10 19:42:31.564: E/AndroidRuntime(2839): at java.lang.Class.getConstructorOrMethod(Class.java:472)
05-10 19:42:31.564: E/AndroidRuntime(2839): at java.lang.Class.getMethod(Class.java:857)
05-10 19:42:31.564: E/AndroidRuntime(2839): at android.view.View$1.onClick(View.java:3803)
05-10 19:42:31.564: E/AndroidRuntime(2839): ... 11 more
MainActivity.xml
package com.blabla.quiztest;
import android.content.Context;
import android.os.Bundle;
import android.support.v4.app.Fragment;
import android.support.v7.app.ActionBarActivity;
import android.view.LayoutInflater;
import android.view.Menu;
import android.view.MenuItem;
import android.view.View;
import android.view.ViewGroup;
import android.widget.Button;
import android.widget.Toast;
public class MainActivity extends ActionBarActivity {
@Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_main); if (savedInstanceState == null) { getSupportFragmentManager().beginTransaction() .add(R.id.container, new PlaceholderFragment()) .commit(); } } public void setupMessageButton() { Button button = (Button) findViewById(R.id.button1); button.setOnClickListener(new View.OnClickListener() { public void onClick(View view) { // Do something in response to button click //Intent intent = new Intent(this, DisplayMessageActivity.class); Context context = getApplicationContext(); CharSequence text = "Hello toast!"; int duration = Toast.LENGTH_SHORT; Toast toast = Toast.makeText(context, text, duration); toast.show(); } }); } @Override public boolean onCreateOptionsMenu(Menu menu) { // Inflate the menu; this adds items to the action bar if it is present. getMenuInflater().inflate(R.menu.main, menu); return true; } @Override public boolean onOptionsItemSelected(MenuItem item) { // Handle action bar item clicks here. The action bar will // automatically handle clicks on the Home/Up button, so long // as you specify a parent activity in AndroidManifest.xml. int id = item.getItemId(); if (id == R.id.action_settings) { return true; } return super.onOptionsItemSelected(item); } /** * A placeholder fragment containing a simple view. */ public static class PlaceholderFragment extends Fragment { public PlaceholderFragment() { } @Override public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) { View rootView = inflater.inflate(R.layout.fragment_main, container, false); return rootView; } }
}
在 fragment_main.xml
中<Button
android:id="@+id/button1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentBottom="true"
android:layout_centerHorizontal="true"
android:layout_marginBottom="189dp"
android:onClick="setupMessageButton"
android:text="Button" />
可能是什么问题?
答案 0 :(得分:1)
2个解决方案:
把它放在你的oncreate:
Button button = (Button) findViewById(R.id.button1);
button.setOnClickListener(new View.OnClickListener() {
public void onClick(View view) {
// Do something in response to button click
//Intent intent = new Intent(this, DisplayMessageActivity.class);
Context context = getApplicationContext();
CharSequence text = "Hello toast!";
int duration = Toast.LENGTH_SHORT;
Toast toast = Toast.makeText(context, text, duration);
toast.show();
}
或者将所有setupmessagebutton()替换为:
您还需要将setupmessagebutton()更改为setupmessagebutton(查看视图)
Context context = getApplicationContext();
CharSequence text = "Hello toast!";
int duration = Toast.LENGTH_SHORT;
Toast toast = Toast.makeText(context, text, duration);
toast.show();
我会选择第二种解决方案,但这取决于你。
编辑:问题是您在XML中定义了一个onclick回调,您不需要在代码中设置OnClickListener。
答案 1 :(得分:1)
当您使用onClick
侦听器及其属性android:onClick="foo"
时,该方法必须在其参数中使用已单击的View
。如下:
public void setupMessageButton(View view) {
...
}
正如您在the Documentation(第一部分)中所看到的那样:
您在android:onClick属性中声明的方法必须具有完全如上所示的签名。具体而言,该方法必须:
- 公开发言 - 返回空白
- 将视图定义为唯一参数(这将是单击的视图)
最后,您使用xml中的属性,然后您不需要在setOnClickListener
中调用Activity
方法,因为系统已使用此属性使用OnClickListener
。<登记/>
然后,您的方法需要是:
public void setupMessageButton(View view) {
// Do something in response to button click
//Intent intent = new Intent(this, DisplayMessageActivity.class);
Context context = getApplicationContext();
CharSequence text = "Hello toast!";
int duration = Toast.LENGTH_SHORT;
Toast toast = Toast.makeText(context, text, duration);
toast.show();
}
此外,如果您想要更新或使用点击事件中按下的View
,请使用以下变量:
public void foo(View view) {
// view = the view clicked.
view.setText("Clicked");
}
答案 2 :(得分:1)
该消息说它正在寻找这样的方法:
setupMessageButton(View)
你定义了这个:
public void setupMessageButton() {
您需要添加View
参数:
public void setupMessageButton(View view) {
答案 3 :(得分:0)
您需要关注LogCat中的这一行:
java.lang.IllegalStateException: **Could not find a method setupMessageButton(View) in the activity class com.blabla.quiztest.MainActivity for onClick handler on view class android.widget.Button with id 'button1'**
请注意:
Could not find a method setupMessageButton(View)...for onClick handler on view class
这是因为您在XML中使用了android:onClick="setupMessageButton"
但未在类中为该操作创建相应的方法。
您创建了public void setupMessageButton()
,但正如错误日志所述,它正在寻找View
参数。
所以改变这个:
public void setupMessageButton() {
到此:
public void setupMessageButton(View view) {
该方法只需要View
参数即可识别&#34;。