2012-09-27 54 views
2

在类中,我以编程方式添加Fragment。Android:如何获得片段的意见

这个片段包含一个按钮,我想要一个onClick实现。

这里是I类有:

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

和我ProductFragment1类:

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

和我的主类使用的布局是:

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

和布局我的ProductFragment1用途是:

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

的问题是:

我在这行我主类得到一个NullPointerException异常:

Button done_button = (Button) fragment.getView().findViewById(R.id.done_product_1); 
done_button.setOnClickListener .... 

当我搜索上的一个分段的回报是什么getView()。我发现它返回的视图从onCreateView();

我检查它不返回null。

谢谢你

回答

8

你在这里错了。您获得NPE,因为尚未创建Fragments视图。 Fragments也有一个生命周期,就像Activities

你应该做的是在你的FragmentsonViewCreated()方法中指定OnClickListener。并从那里...你应该创建一个回调,并通知您的主办Activity的事件。