現在我知道人已經問過這個問題之前很多次,我讀了所有這些職位的:按鈕偵聽不會對分段響應
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
和我還是不知道如何使這個簡單的代碼工作。 所以我創建了一個viewpager 2個片段,在片段一個有一個按鈕,我點擊它每次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>
片段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>
我知道有很多的疑問像這樣,我實際上閱讀所有這些,而非答案真的很有幫助。 thanx評論此帖子。