导航抽屉按钮不起作用

时间:2016-03-26 23:37:33

标签: android android-navigation-drawer

我在导航抽屉里添加了按钮。但是,当我点击按钮。该应用程序被迫停止。代码中没有错误。这是Java文件

package com.android.pet.view;
import android.os.Bundle;
import android.support.v4.app.Fragment;
import android.view.View;
import android.widget.RadioButton;
import android.widget.RadioGroup;
import android.widget.Toast;
import android.view.LayoutInflater;
import android.view.ViewGroup;

import com.doepiccoding.navigationdrawer.R;

public class Lesson111 extends Fragment {
    private RadioGroup radioGroup;


    public View onCreateView(LayoutInflater Inflater, ViewGroup container,Bundle savedInstanceState) {
        View rootView = Inflater.inflate(R.layout.onepointthree, null);

    /* Initialize Radio Group and attach click handler */
        radioGroup = (RadioGroup) rootView.findViewById(R.id.radioGroup);
        radioGroup.clearCheck();

    /* Attach CheckedChangeListener to radio group */
        radioGroup.setOnCheckedChangeListener(new RadioGroup.OnCheckedChangeListener() {
            @Override
            public void onCheckedChanged(RadioGroup group, int checkedId) {
                RadioButton rb = (RadioButton) group.findViewById(checkedId);
                if(null!=rb && checkedId > -1){
                    Toast.makeText(Lesson111.this.getActivity(), rb.getText(), Toast.LENGTH_SHORT).show();
                }

            }
        });
        return rootView;
    }

    public void onClear(View v) {
    /* Clears all selected radio buttons to default */
        radioGroup.clearCheck();
    }

    public void onSubmit(View v) {
        RadioButton rb = (RadioButton) radioGroup.findViewById(radioGroup.getCheckedRadioButtonId());
        Toast.makeText(Lesson111.this.getActivity(), rb.getText(), Toast.LENGTH_SHORT).show();
    }
}

此外,这是logcat。

03-27 07:36:12.185 24720-24720/com.doepiccoding.navigationdrawer E/AndroidRuntime: FATAL EXCEPTION: main
                                                                                   Process: com.doepiccoding.navigationdrawer, PID: 24720
                                                                                   java.lang.IllegalStateException: Could not find a method onClear(View) in the activity class com.android.pet.view.NavigationActivity for onClick handler on view class android.widget.Button with id 'clearBtn'
                                                                                       at android.view.View$1.onClick(View.java:3871)
                                                                                       at android.view.View.performClick(View.java:4499)
                                                                                       at android.view.View$PerformClick.run(View.java:18575)
                                                                                       at android.os.Handler.handleCallback(Handler.java:733)
                                                                                       at android.os.Handler.dispatchMessage(Handler.java:95)
                                                                                       at android.os.Looper.loop(Looper.java:136)
                                                                                       at android.app.ActivityThread.main(ActivityThread.java:5021)
                                                                                       at java.lang.reflect.Method.invokeNative(Native Method)
                                                                                       at java.lang.reflect.Method.invoke(Method.java:515)
                                                                                       at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:827)
                                                                                       at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:643)
                                                                                       at dalvik.system.NativeStart.main(Native Method)
                                                                                    Caused by: java.lang.NoSuchMethodException: onClear [class android.view.View]
                                                                                       at java.lang.Class.getConstructorOrMethod(Class.java:472)
                                                                                       at java.lang.Class.getMethod(Class.java:857)
                                                                                       at android.view.View$1.onClick(View.java:3864)
                                                                                       at android.view.View.performClick(View.java:4499) 
                                                                                       at android.view.View$PerformClick.run(View.java:18575) 
                                                                                       at android.os.Handler.handleCallback(Handler.java:733) 
                                                                                       at android.os.Handler.dispatchMessage(Handler.java:95) 
                                                                                       at android.os.Looper.loop(Looper.java:136) 
                                                                                       at android.app.ActivityThread.main(ActivityThread.java:5021) 
                                                                                       at java.lang.reflect.Method.invokeNative(Native Method) 
                                                                                       at java.lang.reflect.Method.invoke(Method.java:515) 
                                                                                       at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:827) 
                                                                                       at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:643) 
                                                                                       at dalvik.system.NativeStart.main(Native Method) 

以下是XML文件的代码

<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
              android:layout_width="fill_parent"
              android:layout_height="wrap_content"
              android:orientation="vertical"
              android:padding="20dp">

    <TextView
        android:layout_width="fill_parent"
        android:layout_height="wrap_content"
        android:text="What is ICT?"
        android:textColor="@android:color/black"
        android:textSize="20sp"
        android:layout_alignParentTop="true"
        android:layout_centerHorizontal="true"
        android:gravity="center"
         />

    <RadioGroup
        android:id="@+id/radioGroup"
        android:layout_width="fill_parent"
        android:layout_height="wrap_content"
        android:orientation="vertical"
            >

        <RadioButton
            android:id="@+id/radioButton1"
            android:layout_width="fill_parent"
            android:layout_height="wrap_content"
            android:text="Option 1"
            android:textColor="@android:color/black"
            android:textSize="20sp"
/>

        <RadioButton
            android:id="@+id/radioButton2"
            android:layout_width="fill_parent"
            android:layout_height="wrap_content"
            android:text="Option 2"
            android:textColor="@android:color/black"
            android:textSize="20sp"
/>

        <RadioButton
            android:id="@+id/radioButton3"
            android:layout_width="fill_parent"
            android:layout_height="wrap_content"
            android:text="Option 3"
            android:textColor="@android:color/black"
            android:textSize="20sp"
/>
    </RadioGroup>

    <LinearLayout
        android:layout_width="fill_parent"
        android:layout_height="wrap_content"
        android:orientation="horizontal">

        <Button
            android:id="@+id/clearBtn"
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:layout_weight="1"
            android:onClick="onClear"
            android:text="Clear " />

        <Button
            android:id="@+id/submitBtn"
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:layout_weight="1"
            android:onClick="onSubmit"
            android:text="Submit" />
    </LinearLayout>
</LinearLayout>

1 个答案:

答案 0 :(得分:1)

布局中的此按钮:

    <Button
        android:id="@+id/clearBtn"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:layout_weight="1"
        android:onClick="onClear"
        android:text="Clear " />

它说当点击它时,在包含视图的活动中执行一个名为onClear()的方法。

您的错误消息表明活动中没有相关的onClear()方法。你的片段中有一个onClear,但这不是一个活动。它只适用于活动。

不是使用android:onClick来指定方法的名称,而是在代码中注册OnClickListener对象几乎总是更好。