【问题标题】:button listener doesn't response on fragment按钮侦听器不响应片段
【发布时间】:2016-02-18 00:41:57
【问题描述】:

现在我知道人们之前已经多次问过这个问题,我阅读了所有这些帖子:

NullPointerException with Fragment Interface Listener

Click listener inside a fragmentactivity with Page Adapter

button inside a fragment doesn't work

https://stackoverflow.com/questions/22286220/my-button-action-is-not-working-in-tabs-view-using-fragments

button inside a fragment doesn't work

findViewById in Fragment

我仍然不知道如何使这个简单的代码工作。 所以我创建了一个带有 2 个片段的 viewpager,在片段 A 中有一个按钮,每次我点击它时,onClickListener 都不会执行,并且没有例外。

片段活动java

public class Asec extends FragmentActivity {
    ViewPager vp = null;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_asec);
        vp = (ViewPager) findViewById(R.id.pager);
        vp.setAdapter(new ma(getSupportFragmentManager()));

    }
}

class ma extends FragmentPagerAdapter {

    public ma(FragmentManager fm) {
        super(fm);
    }

    @Override
    public Fragment getItem(int arg0) {
        Fragment a = null;
        if (arg0 == 0) {
            a = new A();
        }
        if (arg0 == 1) {
            a = new b();
        }
        return a;
    }

    @Override
    public int getCount() {
        return 2;
    }
}

片段活动 xml

<android.support.v4.view.ViewPager 
    xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_height="match_parent"
    android:id="@+id/pager"
    android:layout_width="match_parent">           
  </android.support.v4.view.ViewPager>

片段 A java

public class A extends Fragment{
    Button b;
    @Override
    public View onCreateView(LayoutInflater inflater,
            @Nullable ViewGroup container, @Nullable Bundle savedInstanceState) {
        View view = (RelativeLayout) inflater.inflate(R.layout.av, container, false);//this is line A
        b = (Button) view.findViewById(R.id.button1);//this is line B
        b.setOnClickListener(new View.OnClickListener() {

            @Override
            public void onClick(View v) {
                System.out.println("work");//this is not printed

            }
        });
        return inflater.inflate(R.layout.av, container, false);
    }

}

我尝试了 A 行的替代方法,例如 getView().findViewById(...),但它仍然无法正常工作

片段 A xml

<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="match_parent"
    android:layout_height="match_parent" 
    android:background="#33D692">

    <TextView
        android:id="@+id/textView1"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_alignParentLeft="true"
        android:layout_alignParentTop="true"
        android:layout_marginLeft="23dp"
        android:layout_marginTop="28dp"
        android:text="Fragment A"
        android:textAppearance="?android:attr/textAppearanceLarge" />

    <Button
        android:id="@+id/button1"
        style="?android:attr/buttonStyleSmall"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_below="@+id/textView1"
        android:layout_centerHorizontal="true"
        android:layout_marginTop="53dp"
        android:text="Button" />

</RelativeLayout>

Fragment B java(从现在开始,不是很重要)

public class b extends Fragment{
    @Override
    public View onCreateView(LayoutInflater inflater,
            @Nullable ViewGroup container, @Nullable Bundle savedInstanceState) {
        // TODO Auto-generated method stub
        return inflater.inflate(R.layout.bv, container, false);
    }


}

片段 B xml

<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:background="#F53636" >

    <TextView
        android:id="@+id/textView1"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_alignParentLeft="true"
        android:layout_alignParentTop="true"
        android:layout_marginLeft="23dp"
        android:layout_marginTop="28dp"
        android:text="Fragment B"
        android:textAppearance="?android:attr/textAppearanceLarge" />

</RelativeLayout>

我知道有很多这样的问题,我实际上阅读了所有这些问题,但没有一个答案真的很有帮助。 谢谢你评论这篇文章。

【问题讨论】:

    标签: android android-fragments android-viewpager


    【解决方案1】:

    解决方案是使用 Activity,并更改一些代码。 XML 文件可能是这样的:

    <android.support.v4.view.ViewPager 
        xmlns:android="http://schemas.android.com/apk/res/android"
        android:layout_height="match_parent"
        android:id="@+id/pager"
        android:layout_width="match_parent">   
    
    <fragment
            android:id="@+id/fragment_a"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:name="logical.address.of.your.Fragment"/>
    
    </android.support.v4.view.ViewPager>
    

    你用这种方式改变你的活动类:

    mActionFragment = (ClassA)getFragmentManager().findFragmentById(R.id.fragment_a);
    

    我希望它有效。

    【讨论】:

      【解决方案2】:

      在您的片段中,您将布局膨胀两次:

      View view = (RelativeLayout) inflater.inflate(R.layout.av, container, false);//this is line A
      //...
      return inflater.inflate(R.layout.av, container, false);
      

      您设置点击侦听器的布局与屏幕上显示的布局不同。

      返回您膨胀的view 并设置点击监听器并且不要膨胀另一个布局。

      【讨论】:

        【解决方案3】:

        我不知道它是否有帮助,但是:

        @Override
        public void onClick(View v) {
            System.out.println("work");//this is not printed
        
        }
        

        这不起作用,因为System.out.println("") 在 Android 中不起作用。使用Log.d(tag, message) 获取调试消息或使用Log.e(tag, message) 获取错误信息。

        【讨论】:

          猜你喜欢
          • 2013-09-13
          • 1970-01-01
          • 1970-01-01
          • 1970-01-01
          • 1970-01-01
          • 1970-01-01
          • 1970-01-01
          • 1970-01-01
          • 1970-01-01
          相关资源
          最近更新 更多