-2

我想刷新按钮单击的片段 - 即,销毁片段并强制在onCreateView()方法上调用。然而,它一直因为上线fragment.getFragmentManager().findFragmentByTag("Frag1");我的片段在刷新时不断崩溃

一个GRRRR NullPointerException的崩溃这是我的代码:

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(),因为这会实际刷新片段并重新显示回收站视图。它在fragment.getFragmentManager().findFragmentByTag("TabOne");上获得NullPointerException。这是我的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> 
+0

删除分离和附加。只需使用替换。 – Aizen

+0

但它崩溃在'fragment.getFragmentManager()。findFragmentByTag(“TabOne”);.'!!!!这是说,片段为空时,它不是 –

+0

它是空的,检查我的答案 – Aizen

回答

0

相反的装拆片段,您应该有一个您的片段中的API会告诉它刷新其内容,或者您​​应该创建片段的新实例并替换前者正在与新的一个例子。告诉片段自己刷新显然比销毁和创建新片段更高效。

+0

我这样做?抱歉,我是Android开发新手 –

+0

如果您只想刷新recyclerview的内容,那么只需在recallFragment方法中调用recyclerView.notifyDataSetChanged()即可。您还需要通知您的适配器发生了什么变化。 – Francesc

+0

你不能在'recyclerview'上做'notifyDataSetChanged()'! –

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(); 

} 
+0

你如何使用'replace()'?我的方法调用是否正确? –

+0

它仍然崩溃的人。我该怎么办> –

+0

您需要提供身份证明文件。例如,在你的XML文件中你有'< - 只是一个例子,但是有一个片段标签。 'replace(R.id.myFragment,fragment)'所以它需要知道在哪里放置片段并替换已经存在的片段。 – Aizen