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
button inside a fragment doesn't work
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.