【发布时间】: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
button inside a fragment doesn't work
我仍然不知道如何使这个简单的代码工作。 所以我创建了一个带有 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