android - setOnClickListener 未在 fragment 中调用

标签 android android-fragments

大家好,我的程序中有一个问题,即 setOnClickListener 未在 fragment 中调用 在我的程序中,我有三个 fragment ,到目前为止,我处于第一个 fragment 代码中,我需要在此 fragment 中处理数据库,但直到现在我遇到了 setOnClickListener 未调用的问题

public class PhoneActivity extends Fragment implements View.OnClickListener {

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

     super.onCreateView(inflater, container, savedInstanceState);

     View rootView = inflater.inflate(R.layout.activity_phone, container, false);

     LinearLayout mLinearLayout = (LinearLayout) inflater.inflate(R.layout.activity_phone,
             container, false);

     Button PhListBtn =(Button)mLinearLayout.findViewById(R.id.phListBtn);
     Button AddBtn =(Button)mLinearLayout.findViewById(R.id.AddBtn);

     final MySqlHelp db = new MySqlHelp(this.getActivity());



    AddBtn.setOnClickListener(new View.OnClickListener() {

        @Override
        public void onClick(View v) {
            Toast.makeText(getActivity(), "hi", Toast.LENGTH_LONG).show();
        }
    });
    return rootView; 



}

public boolean onCreateOptionsMenu(Menu menu) {
    // Inflate the menu; this adds items to the action bar if it is present.
//  getMenuInflater().inflate(R.menu.phone, menu);
    return true;
}

这是 xml

<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:id="@+id/LinearLayout1"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical"
android:paddingBottom="@dimen/activity_vertical_margin"
android:paddingLeft="@dimen/activity_horizontal_margin"
android:paddingRight="@dimen/activity_horizontal_margin"
android:paddingTop="@dimen/activity_vertical_margin"
tools:context=".PhoneActivity" >

<Button
    android:id="@+id/phListBtn"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:text="@string/phoneList" 
    android:clickable="True"/>

<Button
    android:id="@+id/AddBtn"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:text="@string/AddPhone" 
     android:clickable="True"/>

最佳答案

删除这个

 LinearLayout mLinearLayout = (LinearLayout) inflater.inflate(R.layout.activity_phone,
         container, false);

更改为

 Button PhListBtn =(Button)rootView.findViewById(R.id.phListBtn);
 Button AddBtn =(Button)rootView.findViewById(R.id.AddBtn);

此外,您不需要实现 OnClickListener 因为您有匿名内部类

关于android - setOnClickListener 未在 fragment 中调用,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/20885538/

相关文章:

android - ScaleGestureDetector 不适用于 ICS 及以下的 Fragment 或 ViewGroup 布局中的 View

Android:如何在 fragment 中创建带有 float RecyclerView 的叠加层

android - 重新加载 fragment ,未清除 Edittext 的文本

java - 不接受错误的SDK许可证

android - 415 XML Post Rest API 不支持的媒体类型

android - 从使用导航组件的 fragment 返回时会触发哪些方法?

Android:保持 fragment 运行

android - 没有名称的 GSON 序列化普通数组

android - 如何以编程方式加入 android 中的 2 个联系人?

android - Gmail "Can' t 附加空文件”