未在片段中调用setOnClickListener

迈阿贾姆

嗨,大家好,我的程序中有一个问题,就是我的程序中没有在片段中调用setOnClickListener,我有三个片段,直到现在我在第一个片段代码中,我需要在这个片段中处理数据库,但是直到现在我都遇到了这个问题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);

另外,您也不需要实现OnClickListenercoz,因为您有一个匿名内部类

本文收集自互联网,转载请注明来源。

如有侵权,请联系[email protected] 删除。

编辑于
0

我来说两句

0条评论
登录后参与评论

相关文章

来自分类Dev

从片段中未在对话框片段中调用OnCreateView

来自分类Dev

从片段中未在对话框片段中调用OnCreateView

来自分类Dev

如何在片段中实现按钮SetOnClickListener?

来自分类Dev

片段中的按钮setOnClickListener onCreateView()或onActivityCreated()

来自分类Dev

在片段中覆盖setOnClickListener停止工作

来自分类Dev

片段中的Android按钮setOnClickListener错误

来自分类Dev

片段未在ViewPager中更新

来自分类Dev

从活动中调用片段

来自分类Dev

int值未在button.SetOnClickListener()中更改

来自分类Dev

片段中setOnClickListener上的java.lang.NullPointerException

来自分类Dev

Android工具栏未在Backstack的片段上调用onOptionsItemSelected

来自分类Dev

页面未在codeigniter中调用

来自分类Dev

未在 C 中调用的函数

来自分类Dev

函数未在 javascript 中调用

来自分类Dev

在活动中调用片段方法?

来自分类Dev

片段中未调用onActivityResult

来自分类Dev

从活动中调用片段方法

来自分类Dev

片段中未调用onContextItemSelected

来自分类Dev

在片段中添加onOptionsItemSelected调用

来自分类Dev

片段中未调用onActivityResult

来自分类Dev

片段中无法调用OnClickListener

来自分类Dev

片段中的onActivityResult不被调用

来自分类Dev

如何从活动中调用片段

来自分类Dev

片段中无法调用OnClickListener

来自分类Dev

从片段中调用意图

来自分类Dev

从导航抽屉中调用片段

来自分类Dev

在活动中调用片段布局

来自分类Dev

从 MainActivity 调用片段中的函数

来自分类Dev

在主活动中调用片段