4

クラスでは、プログラムでフラグメントを追加しています。

このフラグメントには、onClick実装が必要なボタンが含まれています。

これが私が持っているクラスです:

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を返さないことを確認しました。

ありがとうございました

4

1 に答える 1

9

あなたはここで間違った方向に進んでいます。ビューがまだ作成されていないNPEため、を取得します。のようなライフサイクルもあります。FragmentsFragmentsActivities

あなたがしなければならないことはあなたのメソッドのOnClickListener内部を割り当てることです。Fragments onViewCreated()そしてそこから...コールバックを作成しActivity、イベントのホスティングに通知する必要があります。

于 2012-09-27T13:03:23.370 に答える