【问题标题】:How can i access fragment layout in parent activity?如何访问父活动中的片段布局?
【发布时间】:2015-06-11 14:01:59
【问题描述】:

我有一个标签页和片段。框架有一个“GridView”。我在片段父活动中使用 basedapter 内部类

但是当我想将适配器设置为我的 gridview 时,我得到了 nullpointerexception。因为 GridView 变量总是为 null,请问如何解决这个问题?

Fragment 父 MainActivity OnCreate 方法;

GridView gv = (GridView)findViewById(R.id.gridview); // debugging and always null
gv.setAdapter(new AdapterB(this,WallPaperList)); 

片段 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(用于填充适配器)

<?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>

【问题讨论】:

    标签: java android android-fragments gridview baseadapter


    【解决方案1】:

    你不能像那样访问片段视图,因为它们可能还没有在活动视图层次结构中。

    你必须为你的片段添加一些方法,比如

    myCustomFragment.setGridAdapter(MyCustomGridViewAdapter adapter)
    

    并在您的自定义片段中使用此方法:

    public void setGridAdapter(){
        this.myCustomGridAdapter = adapter;
        GridView gv = (GridView)findViewById(R.id.gridview); 
    
        if(gv != null)
            gv.setAdapter(this.myCustomGridAdapter);
    }
    

    【讨论】:

    • 即使片段可能不在活动的视图层次结构中,findViewById 也会在活动的窗口中找到视图。因此,只要片段已经膨胀,您就可以从片段中找到视图。
    • 那么我必须编写所有代码片段 java 类,对吗?或者有什么解决方案可以在 mainactivity 类中找到片段视图?
    • 您想知道您是否需要实现自定义片段类? - 是的
    • 那我必须在这里再次提问。如何将主要活动上下文发送到片段类到我的适配器内部类?
    • 如果您在片段附加到活动时调用此方法,您可以通过 getActivity() 方法获取活动,但如果您在片段附加到活动之前通过适配器,您必须检查为此,在 onCreateView 中完成所有工作。
    【解决方案2】:

    这段代码:

    GridView gv = (GridView)findViewById(R.id.gridview); // debugging and always null
    gv.setAdapter(new AdapterB(this,WallPaperList)); 
    

    应该在片段的onCreateView 内。您应该创建独立于它们所附加到的活动的片段。

    如果您绝对需要从您的活动中访问片段的布局,您需要延迟findViewById,因为当您调用它时片段的布局尚未膨胀:

    new Handler().postDelayed(new Runnable() {
        @Override
        public void run() {
            GridView gv = (GridView)findViewById(R.id.gridview);
            gv.setAdapter(new AdapterB(this, WallPaperList));
        }
    }, 1000);
    

    虽然此代码可以工作,但我强烈建议您不要使用它!

    【讨论】:

    • 那我必须在这里再次提问。如何将主要活动上下文发送到片段类到我的适配器内部类?
    • @EmreErol 你的意思是getActivity()
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2020-02-20
    • 1970-01-01
    相关资源
    最近更新 更多