简体   繁体   English

如何在父级活动中访问片段布局?

[英]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.

相关问题 我如何从片段中访问活动中的按钮ID - how can i access the button id in activity from fragment 如何从活动/片段访问在服务中创建的MediaPlayer? - How can i access a MediaPlayer created in a Service from an Activity/Fragment? 如何从扩展Fragment而不是Activity的类中正确检索声明为片段布局的ImageView? - How can I correctly retrieve an ImageView declared into a fragment layout from a class that extends Fragment and not Activity? 片段未替换父活动的容器布局 - Fragment not replacing parent activity's container layout Android-如何从Fragment访问父活动中的getter方法 - Android - How to access a getter method in parent Activity from Fragment 如何从父Activity访问片段类中的方法? - How to access a method in fragment class from parent Activity? 如何在扩展Activity的类中访问布局,该类将由另一个Activity子类化? - How can I access the layout in a class extending Activity that is to be subclassed by another Activity? 我无法使用片段从我的活动中访问片段布局内的文本视图 - I am not being able to access the text view inside my fragment layout from my Activity using the fragment 我怎样才能将片段称为活动? - How can i call fragment to activity? 创建片段时如何更改父布局的颜色? - How do I change the color of a parent layout when a fragment is created?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM