Android: comment faire pour avoir le point de vue d'un Fragment
Dans une classe, je suis l'ajout d'un Fragment par programmation.
Ce Fragment contient un Bouton, ce qui je veux avoir un onClick mise en œuvre.
Ici, c'est la classe que j'ai:
public class ShareProduct extends SherlockFragmentActivity {
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.share_product);
FragmentManager fragmentManager = getSupportFragmentManager();
final FragmentTransaction fragmentTransaction = fragmentManager
.beginTransaction();
ProductFragment1 fragment = new ProductFragment1();
fragmentTransaction.add(R.id.share_product_parent, fragment);
fragmentTransaction.commit();
Button done_button = (Button) fragment.getView().findViewById(
R.id.done_product_1);
done_button.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View arg0) {
ProductFragment2 fragment = new ProductFragment2();
fragmentTransaction
.replace(R.id.share_product_parent, fragment);
fragmentTransaction.commit();
}
});
}
}
et mon ProductFragment1 classe est:
public class ProductFragment1 extends SherlockFragment {
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
}
public void onActivityCreated(Bundle savedInstanceState) {
super.onActivityCreated(savedInstanceState);
}
@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
View view = inflater
.inflate(R.layout.share_product_1, container, false);
return view;
}
}
et la mise en page de ma classe principale utilise est:
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:id="@+id/share_product_parent"
android:layout_width="wrap_content"
android:layout_height="fill_parent"
android:gravity="right|center_vertical"
android:orientation="horizontal" >
</RelativeLayout>
et la mise en page de mon ProductFragment1 utilise est:
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="wrap_content"
android:layout_height="fill_parent"
android:orientation="vertical" >
<TextView
android:id="@+id/tv1"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:textSize="25dp" />
<EditText
android:id="@+id/et1"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:layout_below="@id/tv1"
android:textSize="25dp" />
<Button
android:id="@+id/done_product_1"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:layout_below="@id/et1"
android:text="Done" />
</RelativeLayout>
Le problème est:
Je suis un NullPointerException à cette ligne dans ma classe principale:
Button done_button = (Button) fragment.getView().findViewById(R.id.done_product_1);
done_button.setOnClickListener ....
quand j'ai cherché pour ce getView()
sur un fragment de la retourne. J'ai trouvé qu'il retourne à la vue de onCreateView();
J'ai vérifié, il ne retourne pas null.
Merci
OriginalL'auteur Archie.bpgc | 2012-09-27
Vous devez vous connecter pour publier un commentaire.
Vous êtes sur la mauvaise voie. Vous obtenez le
NPE
parce que leFragments
points de vue n'est pas encore créé.Fragments
aussi ont un cycle de vie commeActivities
.Ce que vous devriez faire est de l'affectation de la
OnClickListener
à l'intérieur de votreFragments
onViewCreated()
méthode. Et à partir de là... vous devez créer un rappel et informez-en votre hébergementActivity
de l'événement.OriginalL'auteur Ovidiu Latcu