没有按钮ID或处理程序?

时间:2013-05-07 06:45:18

标签: android android-fragments

所以我收到一个我无法弄清楚的错误。我点击button内的fragment以启动dialog,我的程序崩溃了。它在我的logcat中说我没有button的id而method没有handler。我显然有这些东西,或者我不会感到困惑。我觉得我疲惫的眼睛缺少明显的东西。以下是.java.xmllogcat,我们非常感谢您的帮助。

的.java

public class Store_Fragment extends Fragment {
View view;

public View onCreateView(LayoutInflater inflater, ViewGroup container,
        Bundle savedInstanceState) {

    view = inflater.inflate(R.layout.fragment_store, container, false);

    return view;
}

public void syncHandler (View view) {
    DialogFragment newFragment = new StoreDialog_Fragment();
    newFragment.show(getFragmentManager(), "correct store?");
}

}

.XML

<?xml version="1.0" encoding="utf-8"?>

<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:id="@+id/LinearLayout1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:gravity="center"
android:orientation="vertical" >

<TableRow
    android:id="@+id/tableRow1"
    android:layout_width="match_parent"
    android:layout_height="wrap_content" >

    <TextView
        android:id="@+id/textView1"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="@string/store_number"
        android:textAppearance="?android:attr/textAppearanceMedium" />

    <EditText
        android:id="@+id/storeNum_input_ID"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:ems="8"
        android:hint="@string/store_hint" />

    <Button
        android:id="@+id/sync_button_ID"
        style="?android:attr/borderlessButtonStyle"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:onClick="syncHandler"
        android:text="@string/sync" />

</TableRow>

</LinearLayout>

logcat的

05-07 06:38:34.143: W/dalvikvm(1579): threadid=1: thread exiting with uncaught exception (group=0x40a71930)
05-07 06:38:34.183: E/AndroidRuntime(1579): FATAL EXCEPTION: main
05-07 06:38:34.183: E/AndroidRuntime(1579): java.lang.IllegalStateException: Could not find a method syncHandler(View) in the activity class 
com.facilitysolutionsinc.trackflex.Main for onClick handler on view class android.widget.Button with id 'sync_button_ID'
05-07 06:38:34.183: E/AndroidRuntime(1579):     at android.view.View$1.onClick(View.java:3586)
05-07 06:38:34.183: E/AndroidRuntime(1579):     at android.view.View.performClick(View.java:4204)
05-07 06:38:34.183: E/AndroidRuntime(1579):     at android.view.View$PerformClick.run(View.java:17355)
05-07 06:38:34.183: E/AndroidRuntime(1579):     at android.os.Handler.handleCallback(Handler.java:725)
05-07 06:38:34.183: E/AndroidRuntime(1579):     at android.os.Handler.dispatchMessage(Handler.java:92)
05-07 06:38:34.183: E/AndroidRuntime(1579):     at android.os.Looper.loop(Looper.java:137)
05-07 06:38:34.183: E/AndroidRuntime(1579):     at android.app.ActivityThread.main(ActivityThread.java:5041)
05-07 06:38:34.183: E/AndroidRuntime(1579):     at java.lang.reflect.Method.invokeNative(Native Method)
05-07 06:38:34.183: E/AndroidRuntime(1579):     at java.lang.reflect.Method.invoke(Method.java:511)
05-07 06:38:34.183: E/AndroidRuntime(1579):     at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:793)
05-07 06:38:34.183: E/AndroidRuntime(1579):     at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:560)
05-07 06:38:34.183: E/AndroidRuntime(1579):     at dalvik.system.NativeStart.main(Native Method)
05-07 06:38:34.183: E/AndroidRuntime(1579): Caused by: java.lang.NoSuchMethodException: syncHandler [class android.view.View]
05-07 06:38:34.183: E/AndroidRuntime(1579):     at java.lang.Class.getConstructorOrMethod(Class.java:460)
05-07 06:38:34.183: E/AndroidRuntime(1579):     at java.lang.Class.getMethod(Class.java:915)
05-07 06:38:34.183: E/AndroidRuntime(1579):     at android.view.View$1.onClick(View.java:3579)
05-07 06:38:34.183: E/AndroidRuntime(1579):     ... 11 more

1 个答案:

答案 0 :(得分:1)

这种设置点击式处理程序的方式仅适用于Activity个对象。

您正尝试在Fragment内执行此操作 - 这就是您收到错误的原因。

尝试在您的活动中放置syncHandler(View)方法,将控制权传递给片段,或使用传统的手动方式设置点击处理程序。


完整性更新 - 这是我的2条建议,在@Pragnani引用的问题的上下文中(How to handle button clicks using the XML onClick within Fragments