[英]How can i access fragment layout in parent activity?
I have got a tabpager and fragments. 我有一个tabpager和片段。 And a frament has got a "GridView". 而且一个frament有一个“ GridView”。 I am using a basedapter innerclass in the fragment parent activity. 我在片段父活动中使用了基于适配器的内部类 。
But when i want to set adapter to my gridview im getting nullpointerexception . 但是当我想为我的gridview设置适配器时,我会得到nullpointerexception 。 Because GridView variable is always null.how can solve this issue? 由于GridView变量始终为null。如何解决此问题?
Fragment Parent MainActivity OnCreate Method; 片段父MainActivity OnCreate方法;
GridView gv = (GridView)findViewById(R.id.gridview); // debugging and always null
gv.setAdapter(new AdapterB(this,WallPaperList));
Fragment xml 片段xml
<!-- TODO: Update blank fragment layout -->
<GridView xmlns:android="http://schemas.android.com/apk/res/android"
android:id="@+id/gridview"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:numColumns="auto_fit"
android:verticalSpacing="10dp"
android:horizontalSpacing="10dp"
android:columnWidth="90dp"
android:stretchMode="columnWidth"
android:gravity="center"
/>
MyGrid.xml (For populate with adapter) MyGrid.xml(用于使用适配器填充)
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:orientation="vertical">
<ImageView
android:id="@+id/imagepart"
android:layout_width="fill_parent"
android:layout_height="wrap_content"/>
<CheckBox
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Seç"
android:id="@+id/checkBox" />
</LinearLayout>
You cant acces fragment views like that, becouse they could be not yet in the activity view hierarchy. 您不能像这样访问片段视图,因为它们可能尚未出现在活动视图层次结构中。
You have to add some method to your fragment, like 您必须在片段中添加一些方法,例如
myCustomFragment.setGridAdapter(MyCustomGridViewAdapter adapter)
and in this method in your custom fragment: 并在您的自定义片段中使用以下方法:
public void setGridAdapter(){
this.myCustomGridAdapter = adapter;
GridView gv = (GridView)findViewById(R.id.gridview);
if(gv != null)
gv.setAdapter(this.myCustomGridAdapter);
}
This code: 这段代码:
GridView gv = (GridView)findViewById(R.id.gridview); // debugging and always null
gv.setAdapter(new AdapterB(this,WallPaperList));
should be inside of your fragment's onCreateView
. 应该在片段的onCreateView
。 You should create fragments that are independent from the activity they're attached to. 您应该创建独立于其附加活动的片段。
If you absolutely need to access the fragment's layout from your activity you need to delay the findViewById
because the fragment's layout is not yet inflated when you call it: 如果您绝对需要从活动中访问片段的布局,则需要延迟findViewById
因为调用时片段的布局尚未膨胀:
new Handler().postDelayed(new Runnable() {
@Override
public void run() {
GridView gv = (GridView)findViewById(R.id.gridview);
gv.setAdapter(new AdapterB(this, WallPaperList));
}
}, 1000);
Although this code would work, I highly discourage you from using it! 尽管此代码可以工作,但我强烈不鼓励您使用它!
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.