按钮单击片段不起作用

时间:2015-08-23 05:54:15

标签: android android-fragments android-viewpager

实际上我想用滑动标签制作应用程序。下载了滑动标签的代码。它有3个片段和MainActivity,它扩展了FragmentActivity并使用了viewpager。滑动标签工作正常。现在我尝试在其中一个片段中添加一个按钮,并在单击按钮时显示一个Toast。下面是我在片段的xml文件中添加的代码:

<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:orientation="vertical"
    android:background="#fa6a6a" >

    <TextView android:layout_width="fill_parent"
        android:layout_height="wrap_content"
        android:gravity="center"
        android:text="Design Top Rated Screen"
        android:textSize="20dp"
        android:layout_centerInParent="true"/>

    <Button
        android:id="@+id/btn_frgmnt" 
        android:layout_width="fill_parent"
        android:layout_height="wrap_content"
        android:text="Go to actvity"
        android:onClick="navToActivity"
        />
</RelativeLayout>

在这个片段的java文件中,我已经完成了:

public class TopRatedFragment extends Fragment 
{

    Button btnNav;

      Context context;

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

        View rootView = inflater.inflate(R.layout.fragment_top_rated, container, false);
        btnNav = (Button)rootView.findViewById(R.id.btn_frgmnt);

        return rootView;
    }

    @Override
    public void onCreate(Bundle savedInstanceState) {
        // TODO Auto-generated method stub

        super.onCreate(savedInstanceState);
        //btnNav = (Button)rootView.findViewById(R.id.btn_frgmnt);
        context = getActivity().getApplicationContext();

    }


    public void navToActivity(View v)
    {
        Toast.makeText(getActivity().getApplicationContext(), "Clicked inside fragment!", 2000).show();
    }
}

但显示错误!我哪里错了。或者如何使按钮单击工作。它的这个片段类我应该写按钮点击的代码,对吗?

    08-23 11:58:31.385: E/AndroidRuntime(3033): FATAL EXCEPTION: main
    08-23 11:58:31.385: E/AndroidRuntime(3033): Process: info.androidhive.tabsswipe, PID: 3033
    08-23 11:58:31.385: E/AndroidRuntime(3033): java.lang.IllegalStateException: Could not find a method navToActivity(View) in the activity class info.androidhive.tabsswipe.MainActivity for onClick handler on view class android.widget.Button with id 'btn_frgmnt'
    08-23 11:58:31.385: E/AndroidRuntime(3033):     at android.view.View$1.onClick(View.java:3810)
    08-23 11:58:31.385: E/AndroidRuntime(3033):     at android.view.View.performClick(View.java:4438)
    08-23 11:58:31.385: E/AndroidRuntime(3033):     at android.view.View$PerformClick.run(View.java:18422)
    08-23 11:58:31.385: E/AndroidRuntime(3033):     at android.os.Handler.handleCallback(Handler.java:733)
    08-23 11:58:31.385: E/AndroidRuntime(3033):     at android.os.Handler.dispatchMessage(Handler.java:95)
    08-23 11:58:31.385: E/AndroidRuntime(3033):     at android.os.Looper.loop(Looper.java:136)
    08-23 11:58:31.385: E/AndroidRuntime(3033):     at android.app.ActivityThread.main(ActivityThread.java:5001)
    08-23 11:58:31.385: E/AndroidRuntime(3033):     at java.lang.reflect.Method.invokeNative(Native Method)
    08-23 11:58:31.385: E/AndroidRuntime(3033):     at java.lang.reflect.Method.invoke(Method.java:515)
    08-23 11:58:31.385: E/AndroidRuntime(3033):     at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:785)
    08-23 11:58:31.385: E/AndroidRuntime(3033):     at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:601)
    08-23 11:58:31.385: E/AndroidRuntime(3033):     at dalvik.system.NativeStart.main(Native Method)
    08-23 11:58:31.385: E/AndroidRuntime(3033): Caused by: java.lang.NoSuchMethodException: navToActivity [class android.view.View]
    08-23 11:58:31.385: E/AndroidRuntime(3033):     at java.lang.Class.getConstructorOrMethod(Class.java:472)
    08-23 11:58:31.385: E/AndroidRuntime(3033):     at java.lang.Class.getMethod(Class.java:857)
    08-23 11:58:31.385: E/AndroidRuntime(3033):     at android.view.View$1.onClick(View.java:3803)
    08-23 11:58:31.385: E/AndroidRuntime(3033):     ... 11 more

3 个答案:

答案 0 :(得分:3)

为了捕获片段的按钮事件,使用片段xml的查看或者只是像这样注册回调:

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

        View rootView = inflater.inflate(R.layout.fragment_top_rated, container, false);
        btnNav = (Button)rootView.findViewById(R.id.btn_frgmnt);

        btnNav.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                //Action to be performed on button click
            }
        });

        return rootView;
    }

答案 1 :(得分:0)

在xml文件中声明的单击事件的按钮的回调函数必须在您的活动类中。因此,您必须在xml中声明回调函数,否则您将获得异常。

要获取片段中的点击事件,您需要调用&#39; navToActivity&#39;您的片段中的方法显式。在您的Activity中添加相同的方法,并从活动中的按钮单击回调调用片段方法

brew install opencv3

答案 2 :(得分:0)

请注意,使用上面的XML,Android将仅在当前的Activity中查找onClick方法navToActivity()。如果您正在使用片段,这一点很重要,因为即使您使用片段添加上面的XML,Android也不会在用于添加XML的片段的.java文件中查找onClick方法。