我的片段在刷新时会一直崩溃

时间:2016-03-01 22:57:58

标签: android android-fragments nullpointerexception android-fragmentactivity

我想在按钮点击时刷新片段 - 即,销毁片段并强制调用onCreateView()方法。但是,由于NullPointerException

行上的GRRRR fragment.getFragmentManager().findFragmentByTag("Frag1");,它一直崩溃

这是我的代码:

import android.app.ProgressDialog;
import android.support.v4.app.Fragment;
import android.os.Bundle;
import android.support.annotation.Nullable;
import android.support.v7.widget.LinearLayoutManager;
import android.support.v7.widget.RecyclerView;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.AdapterView;
import android.widget.ArrayAdapter;
import android.widget.Toast;

 public class UserListRecycler extends Fragment {
    RecyclerView recyclerView;
    static UserAdapter adapter;
    RecyclerView.LayoutManager layoutManager;
    ArrayList<UserInfo> list;


    @Override
    public void onCreate(Bundle savedInstanceState){
        super.onCreate(savedInstanceState);
    }

    @Nullable
    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
        View rootView = inflater.inflate(R.layout.userlistGUI, container, false);
        recyclerView = (RecyclerView) rootView.findViewById(R.id.reUsers);
        recyclerView.setHasFixedSize(true);
        list = new ArrayList<UserInfo>();
        // Instantiate new adapter here
        adapter = new MusicRecyclerAdapter(list);
        LinearLayoutManager linearLayoutManager = new LinearLayoutManager(getActivity());
        linearLayoutManager.setOrientation(LinearLayoutManager.VERTICAL);
        recyclerView.setLayoutManager(linearLayoutManager);
        // Sets the adapter here
        recyclerView.setAdapter(adapter);
        adapter.notifyDataSetChanged();
        return rootView;
    }

    @Override
    public void onStart() {
        super.onStart();
        populateRecyclerList();
    }


    public void populateList(){
        PopulateUsers userList = new PopulateUsers(list, adapter, recyclerView);
        userList.execute();
    }


    public void recallFragment(){
        Fragment fragment = null;
        fragment.getFragmentManager().findFragmentByTag("TabOne");
        getFragmentManager().beginTransaction()
                .detach(fragment)
                .attach(fragment)
                .commit();
    }
}

导致问题的是recallFragment()方法。我想回忆一下onCreateView(),因为它会物理刷新片段并重新显示recyclerview。它在NullPointerException上获得fragment.getFragmentManager().findFragmentByTag("TabOne");。这是我的stack trace

   java.lang.NullPointerException
            at lukazs.usersapp.UserListRecycler.recallFragment()(TabOne.java:160)
            at lukazs.usersapp.UserListRecycler$RecommendUser.onPostExecute(TabOne.java:300)
            at lukazs.usersapp.UserListRecycler$RecommendUser.onPostExecute(TabOne.java:306)
            at android.os.AsyncTask.finish(AsyncTask.java:631)
            at android.os.AsyncTask.access$600(AsyncTask.java:177)
            at android.os.AsyncTask$InternalHandler.handleMessage(AsyncTask.java:644)
            at android.os.Handler.dispatchMessage(Handler.java:99)
            at android.os.Looper.loop(Looper.java:137)
            at android.app.ActivityThread.main(ActivityThread.java:5103)
            at java.lang.reflect.Method.invokeNative(Native Method)
            at java.lang.reflect.Method.invoke(Method.java:525)
            at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:737)

这是我的onCreateView()方法:

@Nullable
@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
    View rootView = inflater.inflate(R.layout.userlistGUI, container, false);
    recyclerView = (RecyclerView) rootView.findViewById(R.id.reUsers);
    recyclerView.setHasFixedSize(true);
    list = new ArrayList<UserInfo>();
    // Instantiate new adapter here
    adapter = new MusicRecyclerAdapter(list);
    LinearLayoutManager linearLayoutManager = new LinearLayoutManager(getActivity());
    linearLayoutManager.setOrientation(LinearLayoutManager.VERTICAL);
    recyclerView.setLayoutManager(linearLayoutManager);
    // Sets the adapter here
    recyclerView.setAdapter(adapter);
    adapter.notifyDataSetChanged();
    return rootView;
}

更新的XML:

<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="match_parent" android:layout_height="match_parent"
    android:id="@+id/rLayouts">
    <fragment
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:id="@+id/listFrag"
        class="lukazs.usersapp.UserListRecycler"/>
    <view
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        class="android.support.v4.view.ViewPager"
        android:id="@+id/viewPager"
        android:layout_alignParentStart="true" />
    <android.support.v7.widget.RecyclerView
        android:id="@+id/usersList"
        android:layout_width="match_parent"
        android:layout_height="420dp"
        android:layout_gravity="center_horizontal|top"
        android:layout_alignParentStart="true"
        android:layout_alignParentTop="true"></android.support.v7.widget.RecyclerView>
</RelativeLayout>

2 个答案:

答案 0 :(得分:1)

Fragment fragment = new WhatEverFragmentClassYouHave();

您将fragment设置为null,这就是您有空例外的原因。

如果你要分离然后附上一些东西。如果您知道如何使用它,请致电replace()

<fragment 
              android:id="@+id/article_fragment"

              android:layout_weight="2"
              android:layout_width="match_parent"
              android:layout_height="match_parent" 
              class="com.example.android.fragments.ArticleFragment"
/>

用于刷新

public void recallFragment(){
        Fragment fragment = new WhatEverFragmentClass();

        getFragmentManager().beginTransaction().replace(R.id.article_fragment,fragment,"MyFragmentTag").commit();

    }


@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
    View rootView = inflater.inflate(R.layout.userlistGUI, container, false);

    return rootView;
}

 @Override
    public void onViewCreated(View view, Bundle savedInstanceState) {
        super.onViewCreated(view, savedInstanceState);



 recyclerView = (RecyclerView) rootView.findViewById(R.id.reUsers);
        recyclerView.setHasFixedSize(true);
        list = new ArrayList<UserInfo>();
        // Instantiate new adapter here
        adapter = new MusicRecyclerAdapter(list);
        LinearLayoutManager linearLayoutManager = new LinearLayoutManager(getActivity());
        linearLayoutManager.setOrientation(LinearLayoutManager.VERTICAL);
        recyclerView.setLayoutManager(linearLayoutManager);
        // Sets the adapter here
        recyclerView.setAdapter(adapter);
        adapter.notifyDataSetChanged();

}

答案 1 :(得分:0)

您应该在片段中使用API​​来告诉它刷新其内容,而不是分离和附加片段,或者您应该创建片段的新实例并用新的实例替换现有实例。告诉片段刷新本身显然比破坏和创建新片段更有效。