0

我尝试了许多来自 stackoverflow 的解决方案,但仍然无法找到解决方法。我正在尝试从选项卡式活动的片段中敬酒,但看起来 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>
4

0 回答 0