Button不会启动新片段

时间:2014-02-08 19:00:49

标签: android android-fragments android-button

单击按钮时,我正在尝试加载片段。我按下的按钮代码为:

public void openTopBeers(){


        FragmentManager man=getFragmentManager();
        FragmentTransaction tran=man.beginTransaction();
        Fragment_one=new BPTopBeers();
        tran.add(R.id.main, Fragment_one);//tran.
        tran.addToBackStack(null);
        tran.commit();

    }

尝试启动的片段如下所示:

public class BPTopBeers extends Fragment {

    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container,
                             Bundle savedInstanceState) {
        View v = inflater.inflate(R.layout.toptaste_layout, container, false);

        SharedPreferences prefs = PreferenceManager.getDefaultSharedPreferences(getActivity());
        String userName = prefs.getString("userName", null);
        String userID = prefs.getString("userID", null);

        String title = "Top Beers on Beer Portfolio";
        TextView topTitle = (TextView) v.findViewById(R.id.topTasteTitle);
        topTitle.setText(title);


        //construct url
        String url = "myURL";

        Log.d("myUrl", url);

        //async task goes here
        new GetYourTopTasteBeers(getActivity()).execute(url);

        // Inflate the layout for this fragment

        return v;

    }



}

我收到的错误是:

02-08 13:53:52.910    9826-9826/com.beerportfolio.beerportfoliopro E/AndroidRuntime﹕ FATAL EXCEPTION: main
    java.lang.IllegalStateException: Could not find a method openTopBeers(View) in the activity class com.beerportfolio.beerportfoliopro.MainDraw for onClick handler on view class android.widget.Button with id 'discoverBeers'
            at android.view.View$1.onClick(View.java:3660)
            at android.view.View.performClick(View.java:4280)
            at android.view.View$PerformClick.run(View.java:17984)
            at android.os.Handler.handleCallback(Handler.java:730)
            at android.os.Handler.dispatchMessage(Handler.java:92)
            at android.os.Looper.loop(Looper.java:158)
            at android.app.ActivityThread.main(ActivityThread.java:5789)
            at java.lang.reflect.Method.invokeNative(Native Method)
            at java.lang.reflect.Method.invoke(Method.java:525)
            at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:1027)
            at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:843)
            at dalvik.system.NativeStart.main(Native Method)
     Caused by: java.lang.NoSuchMethodException: openTopBeers [class android.view.View]
            at java.lang.Class.getConstructorOrMethod(Class.java:423)
            at java.lang.Class.getMethod(Class.java:787)
            at android.view.View$1.onClick(View.java:3653)
            at android.view.View.performClick(View.java:4280)
            at android.view.View$PerformClick.run(View.java:17984)
            at android.os.Handler.handleCallback(Handler.java:730)
            at android.os.Handler.dispatchMessage(Handler.java:92)
            at android.os.Looper.loop(Looper.java:158)
            at android.app.ActivityThread.main(ActivityThread.java:5789)
            at java.lang.reflect.Method.invokeNative(Native Method)
            at java.lang.reflect.Method.invoke(Method.java:525)
            at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:1027)
            at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:843)
            at dalvik.system.NativeStart.main(Native Method)
02-08 13:53:52.930      615-693/system_process E/EmbeddedLogger﹕ App crashed! Process: com.beerportfolio.beerportfoliopro
02-08 13:53:52.930      615-693/system_process E/EmbeddedLogger﹕ App crashed! Package: com.beerportfolio.beerportfoliopro v1 (1.0)
02-08 13:53:52.930      615-693/system_process E/EmbeddedLogger﹕ Application Label: BeerPortfolioV2

这是我的xml的代码,其中声明了onclick:

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

<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:orientation="vertical" >

    <FrameLayout
        android:layout_width="match_parent"
        android:layout_height="wrap_content">

        <LinearLayout
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:layout_marginLeft="6dp"
            android:layout_marginRight="6dp"
            android:layout_marginTop="4dp"
            android:layout_marginBottom="4dp"
            android:orientation="vertical"
            android:background="@drawable/bg_card">

            <!-- Card Contents go here -->

            <TextView
                android:id="@+id/discoverTitle"
                android:gravity="center"
                android:layout_width="fill_parent"
                android:layout_height="wrap_content"
                android:ems="10"
                android:text="Discover"
                android:textSize="20sp"
                android:textStyle = "bold"
                android:padding="5dip"
                >
            </TextView>



        </LinearLayout >

    </FrameLayout>


    <FrameLayout
        android:layout_width="match_parent"
        android:layout_height="wrap_content">

        <LinearLayout
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:layout_marginLeft="6dp"
            android:layout_marginRight="6dp"
            android:layout_marginTop="4dp"
            android:layout_marginBottom="4dp"
            android:orientation="vertical"
            android:background="@drawable/bg_card">

            <!-- Card Contents go here -->
            <View
                android:layout_width="1dp"
                android:layout_height="30dp">
            </View>

            <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
                android:layout_width="match_parent"
                android:layout_height="wrap_content"
                android:orientation="horizontal" >

                <Button
                    android:id="@+id/discoverTaste"
                    android:layout_width="0dip"
                    android:layout_height="wrap_content"
                    style="?android:attr/borderlessButtonStyle"
                    android:textColor="@color/orange"
                    android:textStyle="bold"
                    android:padding="5dip"
                    android:text="Tastes"
                    android:onClick="openTopTastes"
                    android:layout_weight="1"
                    />

                <View
                    android:id="@+id/ViewColorPickerHelper"
                    android:layout_width="1dip"
                    android:layout_height="match_parent"
                    android:layout_alignParentTop="true"
                    android:layout_alignParentBottom="true"
                    android:layout_marginBottom="4dip"
                    android:layout_marginTop="4dip"
                    android:background="?android:attr/dividerVertical"
                    android:layout_centerHorizontal="true"/>

                <Button
                    android:id="@+id/discoverBreweries"
                    android:layout_width="0dip"
                    android:layout_height="wrap_content"
                    style="?android:attr/borderlessButtonStyle"
                    android:textColor="@color/orange"
                    android:textStyle="bold"
                    android:padding="5dip"
                    android:text="Breweries"
                    android:onClick="openTopBreweries"
                    android:layout_weight="1"
                    />

            </LinearLayout>

            <View
                android:layout_width="1dp"
                android:layout_height="30dp">
            </View>

            <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
                android:layout_width="match_parent"
                android:layout_height="wrap_content"
                android:orientation="horizontal" >

                <Button
                    android:id="@+id/discoverBeers"
                    android:layout_width="0dip"
                    android:layout_height="wrap_content"
                    style="?android:attr/borderlessButtonStyle"
                    android:textColor="@color/orange"
                    android:textStyle="bold"
                    android:padding="5dip"
                    android:text="Beers"
                    android:onClick="openTopBeers"
                    android:layout_weight="1"
                    />

                <View
                    android:id="@+id/ViewColorPickerHelper"
                    android:layout_width="1dip"
                    android:layout_height="match_parent"
                    android:layout_alignParentTop="true"
                    android:layout_alignParentBottom="true"
                    android:layout_marginBottom="4dip"
                    android:layout_marginTop="4dip"
                    android:background="?android:attr/dividerVertical"
                    android:layout_centerHorizontal="true"/>

                <Button
                    android:id="@+id/discoverStyles"
                    android:layout_width="0dip"
                    android:layout_height="wrap_content"
                    style="?android:attr/borderlessButtonStyle"
                    android:textColor="@color/orange"
                    android:textStyle="bold"
                    android:padding="5dip"
                    android:text="Styles"
                    android:onClick="openTopStyles"
                    android:layout_weight="1"
                    />

                <View
                    android:layout_width="1dp"
                    android:layout_height="30dp">
                </View>

            </LinearLayout>




        </LinearLayout >

    </FrameLayout>








</LinearLayout>

2 个答案:

答案 0 :(得分:2)

在xml中声明属性onClick时,该函数必须获取View对象 尝试将功能更改为:openTopBeers(查看视图)

答案 1 :(得分:2)

两个问题:

  1. onClick方法必须采用Udi Oshi提到的View参数。

  2. 如果在XML中定义的onClick方法必须在活动中定义。你的片段中有它。将方法移至活动,或在代码中设置View.OnClickListener