当前位置: 首页 > 知识库问答 >
问题:

如何修复在Android中找不到的片段视图[重复]

柯清野
2023-03-14
@Override
public void setUserVisibleHint(boolean isVisibleToUser) {
    super.setUserVisibleHint(isVisibleToUser);
    if (isVisibleToUser){
        getData(getActivity());
    }
}
java.lang.NullPointerException: Attempt to invoke virtual method 'void android.widget.ProgressBar.setVisibility(int)' on a null object reference
    at com.app.test.Fragments.MainNotificationFrags.NotificationAllFrag.getData(NotificationAllFrag.java:157)
    at com.app.test.Fragments.MainNotificationFrags.NotificationAllFrag.setUserVisibleHint(NotificationAllFrag.java:142)
    at android.support.v4.app.FragmentStatePagerAdapter.setPrimaryItem(FragmentStatePagerAdapter.java:157)
    at android.support.v4.view.ViewPager.populate(ViewPager.java:1266)
    at android.support.v4.view.ViewPager.populate(ViewPager.java:1116)
    at android.support.v4.view.ViewPager.onMeasure(ViewPager.java:1642)
    at android.view.View.measure(View.java:17496)
    at android.widget.RelativeLayout.measureChildHorizontal(RelativeLayout.java:728)
    at android.widget.RelativeLayout.onMeasure(RelativeLayout.java:464)
    at android.view.View.measure(View.java:17496)
    at android.support.v4.view.ViewPager.onMeasure(ViewPager.java:1658)
    at android.view.View.measure(View.java:17496)
    at android.widget.RelativeLayout.measureChildHorizontal(RelativeLayout.java:728)
    at android.widget.RelativeLayout.onMeasure(RelativeLayout.java:464)
    at android.view.View.measure(View.java:17496)
    at android.support.v4.widget.DrawerLayout.onMeasure(DrawerLayout.java:1081)
    at android.view.View.measure(View.java:17496)
    at android.view.ViewGroup.measureChildWithMargins(ViewGroup.java:5466)
    at android.widget.FrameLayout.onMeasure(FrameLayout.java:430)
    at android.support.v7.widget.ContentFrameLayout.onMeasure(ContentFrameLayout.java:139)
    at android.view.View.measure(View.java:17496)
    at android.view.ViewGroup.measureChildWithMargins(ViewGroup.java:5466)
    at android.widget.LinearLayout.measureChildBeforeLayout(LinearLayout.java:1438)
    at android.widget.LinearLayout.measureVertical(LinearLayout.java:724)
    at android.widget.LinearLayout.onMeasure(LinearLayout.java:615)
    at android.view.View.measure(View.java:17496)
    at android.view.ViewGroup.measureChildWithMargins(ViewGroup.java:5466)
    at android.widget.FrameLayout.onMeasure(FrameLayout.java:430)
    at android.view.View.measure(View.java:17496)
    at android.view.ViewGroup.measureChildWithMargins(ViewGroup.java:5466)
    at android.widget.LinearLayout.measureChildBeforeLayout(LinearLayout.java:1438)
    at android.widget.LinearLayout.measureVertical(LinearLayout.java:724)
    at android.widget.LinearLayout.onMeasure(LinearLayout.java:615)
    at android.view.View.measure(View.java:17496)
    at android.view.ViewGroup.measureChildWithMargins(ViewGroup.java:5466)
    at android.widget.FrameLayout.onMeasure(FrameLayout.java:430)
    at com.android.internal.policy.impl.PhoneWindow$DecorView.onMeasure(PhoneWindow.java:2636)
    at android.view.View.measure(View.java:17496)
    at android.view.ViewRootImpl.performMeasure(ViewRootImpl.java:2031)
    at android.view.ViewRootImpl.measureHierarchy(ViewRootImpl.java:1193)
    at android.view.ViewRootImpl.performTraversals(ViewRootImpl.java:1400)
    at android.view.ViewRootImpl.doTraversal(ViewRootImpl.java:1078)
    at android.view.ViewRootImpl$TraversalRunnable.run(ViewRootImpl.java:5875)
    at android.view.Choreographer$CallbackRecord.run(Choreographer.java:767)
    at android.view.Choreographer.doCallbacks(Choreographer.java:580)
    at android.view.Choreographer.doFrame(Choreographer.java:550)
    at android.view.Choreographer$FrameDisplayEventReceiver.run(Choreographer.java:753)
    at android.os.Handler.handleCallback(Handler.java:739)
    at android.os.Handler.dispatchMessage(Handler.java:95)
    at android.os.Looper.loop(Looper.java:135)
    at android.app.ActivityThread.main(ActivityThread.java:5349)
    at java.lang.reflect.Method.invoke(Native Method)
    at java.lang.reflect.Method.invoke(Method.java:372)
    at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:908)
    at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:703)
public class NotificationAllFrag extends Fragment {

    @BindView(R.id.mainNotification_recyclerView)
    RecyclerView mainNotification_recyclerView;
    @BindView(R.id.mainNotification_noNotificationTxt)
    TextView mainNotification_noNotificationTxt;
    @BindView(R.id.mainNotification_progressBar)
    ProgressBar mainNotification_progressBar;
    @BindView(R.id.newsPageLoadLay)
    RelativeLayout newsPageLoadLay;
    private Context context;
    private List<Datum> model = new ArrayList<>();
    public static NotificationListAdapter notificationListAdapter;
    private SharedPrefrencesHandler prefrencesHandler;
    private String token = "";
    private LinearLayoutManager layoutManager;

    public NotificationAllFrag() {
        // Required empty public constructor
    }

    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container,
                             Bundle savedInstanceState) {
        // Inflate the layout for this fragment
        View view = inflater.inflate(R.layout.fragment_notification_all, container, false);

        //Initialize
        ButterKnife.bind(this, view);
        context = getActivity();
        notificationListAdapter = new NotificationListAdapter(context, model);
        prefrencesHandler = new SharedPrefrencesHandler(context);
        layoutManager = new LinearLayoutManager(context);
        //RecyclerView
        mainNotification_recyclerView.setLayoutManager(layoutManager);
        mainNotification_recyclerView.setHasFixedSize(true);

        //Get token
        token = prefrencesHandler.getFromShared(SharedPrefrencesKeys.TOKEN.name());
        // Lazy loader
        newsPageLoadLay.setVisibility(View.GONE);
        mainNotification_recyclerView.setOnScrollListener(new EndlessRecyclerLinearPage1(layoutManager) {
            @Override
            public void onLoadMore(int current_page) {
                ExploreSendData sendData = new ExploreSendData();
                sendData.setPageIndex(current_page);
                sendData.setPageSize(10);
                sendData.setShowFollows(true);
                sendData.setShowMovies(false);
                sendData.setShowNews(false);
                sendData.setShowReplies(true);
                sendData.setShowSeries(false);
                sendData.setShowSuggestions(true);

                newsPageLoadLay.setVisibility(View.VISIBLE);

                InterfaceApi api = ApiClient.getClient().create(InterfaceApi.class);
                Call<ExploreResponse> call = api.getExplore(token, sendData);

                call.enqueue(new Callback<ExploreResponse>() {
                    @Override
                    public void onResponse(Call<ExploreResponse> call, Response<ExploreResponse> response) {
                        if (response.body().getData() != null && response.body().getStatusCode() != 401
                                && response.body().getStatusCode() != 402) {
                            if (response.body().getData().size() > 0) {
                                notificationListAdapter.addNewItem(response.body().getData());
                                //Gone no explore
                                newsPageLoadLay.setVisibility(View.GONE);
                            }
                        } else {
                            prefrencesHandler.remove(SharedPrefrencesKeys.TOKEN.name());
                            startActivity(new Intent(context, LoginActivity.class));
                        }

                        newsPageLoadLay.setVisibility(View.GONE);
                    }

                    @Override
                    public void onFailure(Call<ExploreResponse> call, Throwable t) {
                        newsPageLoadLay.setVisibility(View.GONE);
                    }
                });
            }
        });

        getData(getActivity());

        return view;
    }

    @Override
    public void setUserVisibleHint(boolean isVisibleToUser) {
        super.setUserVisibleHint(isVisibleToUser);
        if (isVisibleToUser){
            getData(getActivity());
        }
    }

    public void getData(final Context context) {
        ExploreSendData sendData = new ExploreSendData();
        sendData.setPageIndex(1);
        sendData.setPageSize(10);
        sendData.setShowFollows(true);
        sendData.setShowMovies(false);
        sendData.setShowNews(false);
        sendData.setShowReplies(true);
        sendData.setShowSeries(false);
        sendData.setShowSuggestions(true);

        mainNotification_progressBar.setVisibility(View.VISIBLE);

        InterfaceApi api = ApiClient.getClient().create(InterfaceApi.class);
        Call<ExploreResponse> call = api.getExplore(
                new SharedPrefrencesHandler(context).getFromShared(SharedPrefrencesKeys.TOKEN.name()), sendData);

        call.enqueue(new Callback<ExploreResponse>() {
            @Override
            public void onResponse(Call<ExploreResponse> call, Response<ExploreResponse> response) {
                if (response.body().getData() != null && response.body().getStatusCode() != 401
                        && response.body().getStatusCode() != 402) {
                    if (response.body().getData().size() > 0) {
                        model.clear();
                        model.addAll(response.body().getData());
                        notificationListAdapter.notifyDataSetChanged();
                        mainNotification_recyclerView.setAdapter(notificationListAdapter);
                        //Gone no explore
                        mainNotification_noNotificationTxt.setVisibility(View.GONE);

                    } else {
                        mainNotification_noNotificationTxt.setVisibility(View.VISIBLE);
                        mainNotification_recyclerView.setVisibility(View.GONE);
                    }
                } else {
                    prefrencesHandler.remove(SharedPrefrencesKeys.TOKEN.name());
                    startActivity(new Intent(context, LoginActivity.class));
                }

                mainNotification_progressBar.setVisibility(View.GONE);
            }

            @Override
            public void onFailure(Call<ExploreResponse> call, Throwable t) {
                mainNotification_progressBar.setVisibility(View.GONE);
            }
        });
    }

我该怎么修好它?请帮忙

共有1个答案

阎庆
2023-03-14

这不是正确的解决办法,但它百分之百起作用

 @Override
 public void setUserVisibleHint(boolean isVisibleToUser) {
     super.setUserVisibleHint(isVisibleToUser);
     if (isVisibleToUser && mainNotification_recyclerView!=null){
         getData(getActivity());
     }
 }



 @Override
 public void onViewCreated(View view, @Nullable Bundle savedInstanceState) {
     super.onViewCreated(view, savedInstanceState);
     if (getUserVisibleHint()) {
         getData(getActivity());
     }
 }

当用户在选项卡上进行初始化数据时,它与多个或单个选项卡一起工作

它可以帮助更快地加载数据,甚至同时加载所有片段

 类似资料:
  • 我一整天都被困在这个问题上,我似乎找不到一个适合我的情况的答案,足以让我使用。 当我单击一个列表项时,比如Blue,在Portraint中,我希望它显示一个新的片段,这是一个蓝色的屏幕,而在Sandwork中,它在半个屏幕上显示该片段,在另一个屏幕上显示listview。我正在使用ActionBarSherlock和碎片来完成这一点。 我从里到外构建了它,所以我从一个列表开始,它按照我想要的方式工

  • 我刚开始学Android。我有一个活动类,我想把它重定向到导航栏的ViewPage中的一个片段?我尝试了很多方法,但都不奏效。我希望能帮上忙!!!我的代码:有一个mainactivity.java有一个ViewPager和BottomNavigationView ViewPageApdater类 和Activity类,但我遇到一个问题:java.lang.IllegalArgumentExcept

  • 问题内容: 我有一个活动,其中包含一个片段,在该片段中有一个按钮,当单击它时,会弹出一个对话框。 在此对话框中,有一个Viewpager,其中包含一些要显示的片段。 这是代码和错误,请您花宝贵的时间告诉我我哪里错了。非常感谢您的帮助。 MainActivity.class MyFragment.class PagerDialog.class 这是dialog.xml: 这是错误 问题答案: 我找到

  • 我已经更新了Android Studio的3.4.1版本。 立即,Android Gradle Plugin版本和Gradle版本不是最后一个版本,所以我用3.4.1和5.1.1(文件>项目结构>项目)进行了更改。 现在,我试图创建一个apk,但出现错误“无法找到com.Android.tools.Build:Gradle:3.4.1”。 该项目是用cordova创建的,首先我在9.0.0版本上更

  • 所以我一直在尝试使用GraphView。我已经将它导入到我的依赖项中,但由于某种原因,我收到了依赖项解析错误。这是我的应用程序/build.gradle 但是当我尝试运行该应用程序时,构建失败,并且我收到以下错误: 转到留档以了解如何修复依赖项解析错误。" “implementation ' com . Android . support:app compat-V7:28.0.0””这一行显示错误

  • 我试图探索Android框架是如何管理碎片的,通过我的研究,我了解了很多关于碎片的新知识,但我有一次陷入了困境,无法理解这是如何发生的。 请先试着理解我的场景。它是这样的:我有一个活动,一个接一个地添加两个片段。首次加载活动时,使用以下代码将片段A附加到该活动: 片段A的这些回调方法在加载时被调用 FirstDummyFragment:onCreate:savedInstanceState---