Warning: file_get_contents(/data/phpspider/zhask/data//catemap/3/android/186.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/ruby-on-rails-3/4.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Android:如何获取片段的视图_Android_Nullpointerexception_Android Fragments - Fatal编程技术网

Android:如何获取片段的视图

Android:如何获取片段的视图,android,nullpointerexception,android-fragments,Android,Nullpointerexception,Android Fragments,在一个类中,我以编程方式添加一个片段 这个片段包含一个按钮,我希望有一个onClick实现 以下是我的课程: public class ShareProduct extends SherlockFragmentActivity { protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.

在一个类中,我以编程方式添加一个片段

这个片段包含一个按钮,我希望有一个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>
当我在片段上搜索
getView()
时,返回的是什么。我发现它从
onCreateView()返回视图

我检查过它不返回空值


谢谢你

你走错了方向。您将获得
NPE
,因为
Fragments
视图尚未创建<代码>片段也有一个生命周期,就像活动一样

您应该做的是在
片段
onViewCreated()
方法中指定
OnClickListener
。从那里。。。您应该创建一个回调,并将事件通知您的主机
活动

<?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>
Button done_button = (Button) fragment.getView().findViewById(R.id.done_product_1);
done_button.setOnClickListener ....