java - 机器人 DialogFragment 机器人 :onClick ="buttonCancel" causes IllegalStateException could not find a method

标签 java android android-layout

我的对话 fragment 有问题。我想使用 android:onClick 属性,因为在我看来代码更清晰。

在我的布局中,我有以下声明:

<Button
        android:id="@+id/dialog_new_database_button_cancel"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:layout_weight="1"
        android:text="@string/button_cancel"
        android:maxLines="1"
        style="?android:attr/buttonBarButtonStyle"
        android:onClick="buttonCancel"
        />

现在我的 DialogFragment

import android.os.Bundle;
import android.support.v4.app.DialogFragment;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;

public class DialogNewDatabase extends DialogFragment {

    public DialogNewDatabase() {
        // Empty constructor required for DialogFragment
        super();
    }

    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
    super.onCreateView (inflater, container, savedInstanceState);
        View view = inflater.inflate(R.layout.dialog_new_database, container);    
        getDialog().setTitle("Hello");
        return view;
    }

    @Override
    public void onCreate(Bundle bundle) {
        setCancelable(true);
        setRetainInstance(true);
        super.onCreate(bundle);

    }

    @Override
    public void onDestroyView() {
        if (getDialog() != null && getRetainInstance())
            getDialog().setDismissMessage(null);
        super.onDestroyView();
    }

    public void buttonCancel (View view) {
        dismiss();
    }

    public void buttonOK (View view) {

    }

}

现在,当我尝试单击取消按钮时,我得到:

java.lang.IllegalStateException: Could not find a method buttonCancel(View) in the activity class android.view.ContextThemeWrapper for onClick handler on view class android.widget.Button with id 'dialog_new_database_button_cancel'
at android.view.View$1.onClick(View.java:3031)
at android.view.View.performClick(View.java:3511)
at android.view.View$PerformClick.run(View.java:14105)
at android.os.Handler.handleCallback(Handler.java:605)
at android.os.Handler.dispatchMessage(Handler.java:92)
at android.os.Looper.loop(Looper.java:137)
at android.app.ActivityThread.main(ActivityThread.java:4482)
at java.lang.reflect.Method.invokeNative(Native Method)
at java.lang.reflect.Method.invoke(Method.java:511)
at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:787)
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:554)
at dalvik.system.NativeStart.main(Native Method)
Caused by: java.lang.NoSuchMethodException: buttonCancel [class android.view.View]
at java.lang.Class.getConstructorOrMethod(Class.java:460)
at java.lang.Class.getMethod(Class.java:915)
at android.view.View$1.onClick(View.java:3024)
... 11 more

有什么想法吗?这可能与我使用 import android.support.v4.app.DialogFragment(支持 v4)的事实有某种关系吗?如何解决(我仍然更喜欢在 xml 布局中使用 android:onClick)。

最佳答案

我会尝试一种适合我的不同方法:

  1. 在您的 fragment 中实现 OnClickListener:

    public class DialogNewDatabase extends DialogFragment implements OnClickListener`
    
  2. 在 xml 中有一个带有唯一 id 的按钮,它不需要需要 android:clickable

    <Button  android:id="@+id/dialog_new_database_button_cancel" />
    
  3. 在您的 fragment 中覆盖方法 onClick() 并在您的点击中插入 react :

    public void onClick(View v) {       
      switch (v.getId()) {
        case R.id.dialog_new_database_button_cancel:
            // your stuff here
            this.dismiss();
    
            break;          
        default:                
            break;
       }
    }   
    
  4. 导入必要的:

    import android.view.View.OnClickListener; 
    
  5. 在按钮上启动 onClickListener:

    private Button bCancel = null;
    bCancel = (Button) findViewById(R.id.dialog_new_database_button_cancel);
    bCancel.setOnClickListener(this);
    // it is possible that you might need the refrence to the view.
    // replace 2nd line with (Button) getView().findViewById(...);
    

    这样您就可以在同一个 onClick 方法中处理更多的可点击按钮。您只需要使用可点击小部件的正确 ID 添加更多案例。

关于java - 机器人 DialogFragment 机器人 :onClick ="buttonCancel" causes IllegalStateException could not find a method,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/19381185/

相关文章:

java - 将计时器添加到 Java Swing

java - ORA-01780 : string literal required using prepared statement

android - 没有用 ArrayList 填充 ListView

android - 在 RelativeLayout 中分配额外的空间

android - 在 TextView 上膨胀 xml 布局 ArrayIndexOutOfBoundsException 时出错

java - Junit 中每个方法的 Mock System.class

java - 使用字符串数组创建具有动态子类的对象

java - 从字节/帧 Java/Android 创建可播放的视频流

安卓 ImageView : Generating Drawable object from local file fails

android - 如何在所有设备中将评级栏星标颜色设置为单一颜色?