在Fragment中使用遇到的一些问题
大佬,我在Fragment中采用如下方式使用,提示我空指针,请问一下是什么问题
@Override
public void onActivityCreated(@Nullable Bundle savedInstanceState) {
super.onActivityCreated(savedInstanceState);
pageLayout = new PageLayout.Builder(getContext())
.initPage(MainFragment.this)
.setOnRetryListener(new PageLayout.OnRetryClickListener() {
@Override
public void onRetry() {
//重新获取数据
}
})
.create();
pageLayout.showLoading();
后来我改成如下代码,程序不崩溃,但是页面时一片空白,这个空白区域指的是Fragment的区域,挂载当前Fragment的Activity的其他区域正常显示:
pageLayout = new PageLayout.Builder(getContext())
.initPage(this.getView().findViewById(R.id.linearlayout_test))
.setOnRetryListener(new PageLayout.OnRetryClickListener() {
@Override
public void onRetry() {
//重新获取数据
}
})
.create();
pageLayout.showLoading();
请帮忙看看是什么原因
粘一下你布局代码,或者读下这段代码:
public Builder initPage(Object targetView) { ViewGroup content = null; if (targetView instanceof Activity) { mContext = (Context) targetView; content = ((Activity) targetView).findViewById(android.R.id.content); } else if (targetView instanceof Fragment) { mContext = ((Fragment) targetView).getActivity(); content = (ViewGroup) ((Fragment) targetView).getView().getParent(); } else if (targetView instanceof View) { mContext = ((View) targetView).getContext(); content = (ViewGroup) ((View) targetView).getParent(); }
由于代码布局太长,部分省略了,请帮忙看看?关于您上面那部分代码,我读过了,所以才会传递Fragment对象或者是Fragment布局中外面View来完成initPage操作:
<?xml version="1.0" encoding="utf-8"?>
<com.scwang.smartrefresh.layout.SmartRefreshLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
xmlns:tools="http://schemas.android.com/tools"
android:id="@+id/smartRefreshLayout_Main_Fragment"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:fitsSystemWindows="true"
tools:context=".shouye.MainFragment">
<com.scwang.smartrefresh.layout.header.ClassicsHeader
android:layout_width="match_parent"
android:layout_height="wrap_content"/>
<LinearLayout
android:id="@+id/linearlayout_test"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical"
android:background="@color/colorWrite"
>
<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_marginTop="12dp"
android:layout_marginBottom="14dp"
android:orientation="horizontal">
<ImageView
android:layout_width="30dp"
android:layout_height="30dp"
android:layout_marginLeft="12dp"
android:background="@mipmap/icon_main_fragment_ben_di" />
<RelativeLayout
android:layout_width="match_parent"
android:layout_height="30dp"
android:layout_marginLeft="8dp"
android:layout_marginRight="12dp"
android:background="@drawable/bg_shape_gray_main_fragment">
<省略部分布局代码>
</RelativeLayout>
</LinearLayout>
<android.support.v4.widget.NestedScrollView
android:focusableInTouchMode="true"
android:layout_width="match_parent"
android:layout_height="match_parent">
<LinearLayout
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical">
<此部分是比较长的普通布局文件,长度超过一屏幕>
</LinearLayout>
</android.support.v4.widget.NestedScrollView>
</LinearLayout>
</com.scwang.smartrefresh.layout.SmartRefreshLayout>
建议大佬,有时间可以在例子中写一个Fragment的代码,对于我们这些小白帮助很大~
已经更新代码,新增V4Fragmengt兼容和在fragmengt中使用demo
通过自己测试,可以正常展示了,谢谢大佬,但是我这边在自己的项目中使用测试的时候,会报空指针问题,
Attempt to invoke virtual method 'android.view.View.unFocus(android.view.View)'on a null object ference
我将initPage方法,打断点,发现没有空的对象,所以在问一下会是什么方面的原因呢?
经过我的分析测试,我这边的出的结论是:
如果当前Activity挂载了多个Fragment,那么就会出现上述的问题,如果Activity挂载了一个Fragment,那么就不会出现上述提到的异常
大佬 ,能不能帮忙分析一下为什么?目前应用中一个Activity挂载多个Fragment还是比较常见的情况呢~
报错日志不全,装载FG用的什么方式?报错日志定位到哪一行?这些你可以自己分析出来的,我这边试了下装载几个都可以,要是不行加我微信 http://hankkin.cn/about/