Toast不能使用Tabbed Activity的片段

时间:2015-09-06 21:36:35

标签: android android-fragments tabbed-interface

我尝试了很多来自stackoverflow的解决方案,但仍无法找到解决方法。我试图从Tabbed Activity的片段干杯,但看起来OnClickListener不起作用。请帮帮我。这是我的代码:

Button b;
@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
                         Bundle savedInstanceState) {
    // Inflate the layout for this fragment
    View ph=inflater.inflate(R.layout.fragment_history, container, false);
    b=(Button)ph.findViewById(R.id.hb);
    b.setOnClickListener(new View.OnClickListener() {
        @Override

        public void onClick(View v) {
            Log.d("test", "test");
            Toast.makeText(getActivity().getApplicationContext(), "HISTORY TAB", Toast.LENGTH_LONG).show();
        }
    });
    return ph;
}

fragment_history布局代码是:

<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
         xmlns:tools="http://schemas.android.com/tools"
         android:layout_width="match_parent"
         android:layout_height="match_parent"
         tools:context="molecule.tabfinal.History">

<!-- TODO: Update blank fragment layout -->
<TextView
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:text="TAPPAL HISTORY TAB"/>
<Button
    android:id="@+id/hb"
    android:layout_width="75dp"
    android:layout_height="50dp"
    android:text="HISTORY"
    android:layout_alignParentTop="true"
    android:layout_centerHorizontal="true"
    android:layout_marginTop="139dp"/>
</RelativeLayout>

0 个答案:

没有答案