Android的:如何让一个片段的意见 [英] Android: how to get the views of a Fragment
问题描述
在一个类我加入一个片段编程。
In a class i am adding a Fragment programatically.
该片段包含一个按钮,我想有一个onClick实现。
This Fragment contains a Button, which i want to have an onClick implementation.
下面是类我有:
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类是:
and my ProductFragment1 class is:
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;
}
}
和我的主类使用的布局是:
and the layout my main class uses is:
<?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使用的布局是:
and the layout my ProductFragment1 uses is:
<?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异常,在此行中我的主类:
I am getting a NullPointerException at this line in my main class:
Button done_button = (Button) fragment.getView().findViewById(R.id.done_product_1);
done_button.setOnClickListener ....
当我搜索什么 getView()
上一个片段的回报。我发现,它返回从 onCreateView()的观点;
when i searched for what getView()
on a fragment returns. I found out that it returns the view from onCreateView();
我检查没有返回null。
I checked it doesn't return null.
感谢您
推荐答案
您是在错误的轨道在这里。你得到的 NPE
,因为片段
的意见是尚未创建。 片段
也有,就像活动生命周期
。
You are on the wrong track here. You get the NPE
because the Fragments
views is not created yet. Fragments
also have a lifecycle just like Activities
.
你应该做的是分配 OnClickListener
里面的片段
onViewCreated()
方法。从那里...你应该创建一个回调,并通知您的主机活动事件
。
What you should be doing is assigning the OnClickListener
inside your Fragments
onViewCreated()
method. And from there... you should create a callback and notify your hosting Activity
of the event.
这篇关于Android的:如何让一个片段的意见的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!