0

Now i know people have already asked this question many times before, and i read all of these posts:

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

and still i don't know how to make this simple code work. So i created a viewpager with 2 fragments, in fragment A there is a button and every time i click on it the onClickListener is not preformed and there is no exceptions.

Fragment activity 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;
    }
}

Fragment 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">           
  </android.support.v4.view.ViewPager>

Fragment 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);
    }

}

I tried alternative way for line A like getView().findViewById(...) and it is still not working

Fragment 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 (from now on, not really important)

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);
    }


}

Fragment 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>

i know there is lot of questions like this and i actually read all of them and non of the answers were really helpful. thanx for commenting this post.

Community
  • 1
  • 1
john
  • 11

3 Answers3

0

A solution is to use an Activity, and change some code. The XML file could be like this:

<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>

And you change your Activity class in this way:

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

I hope that it works.

0

In your fragment you're inflating a layout twice:

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

The layout you've set your click listener on is not the same that's displayed on screen.

Return the view you inflated and set click listener on and don't inflate another layout.

laalto
  • 150,114
  • 66
  • 286
  • 303
0

I don't know if it helps anymore, but:

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

}

This won't work because System.out.println("") doesn't work in Android. Use Log.d(tag, message) for debug messages or Log.e(tag, message) for errors instead.

shim
  • 9,289
  • 12
  • 69
  • 108
Namnodorel
  • 385
  • 5
  • 17