-1

我有一個工具欄片段被稱爲與主片段中定義的左圖標聽衆的性質(定義見下文)使用,隨處可見應用程序崩潰上的圖標收聽

工具欄片段(常見的工具欄片段雙擊發生如報頭)

public void refresh(commonFragment f) { 
     if (view != null && f != null && !commonFragment .class.equals(f.getClass()) && !BlankcommonFragment .class.equals(f.getClass())) { 

      setRightIcon(f.getRightIcon(), f.getRightIconListener()); 
      setLeftIcon(f.getLeftIcon(), f.getLeftIconListener()); 


     } 
    } 

共同片段(共同屬性保持這裏用於重新使用)

 public Integer getLeftIcon() { 
      return R.drawable.left_fragment_icon; 
     } 

     public View.OnClickListener getLeftIconListener() { 
      return new NavigationOnClickListener(this, new LeftFragment()); 
     } 
public View.OnClickListener getRightIconListener() { 

     return MenuFragment.instance().getDashboardIconListener(); 
    } 

頁面片段(頁面,其中發生在左圖標聽者問題)

@Override 
    public Integer getLeftIcon() { 
     return R.drawable.back_icon; 
    } 

    @Override 
    public View.OnClickListener getLeftIconListener() { 

     return new NavigationOnClickListener(this, new HomeFragment()); 
    } 

NavigationOnClickListener(使用用於導航)

public class NavigationOnClickListener implements View.OnClickListener { 
    private Common Fragment from; 
    private Common Fragment to; 

    public NavigationOnClickListener(Common Fragment from, Common Fragment to) { 
     this.from = from; 
     this.to = to; 
    } 

    @Override 
    public void onClick(View v) { 
     from.navigate(to); 
    } 
} 

後退按鈕圖標和左圖標切換(相同的圖像的視圖,但與不同的圖像ID)點擊根據不同的頁面。
例如:
有3個片段:A,B和C,B是主片段。
通過單擊B上的左邊圖標它把你帶到A,並單擊C上的後退圖標帶你到B

的問題上回雙擊圖標崩潰的應用程序

NullPointerException: Attempt to invoke virtual method 'void android.widget.ListView.setAdapter(android.widget.ListAdapter)' on a null object reference 

正在添加錯誤上views.setAdapter

public View build(final LayoutInflater inflater, final ViewGroup container, Bundle savedInstanceState) { //line added 

     ... 

    View home = inflater.inflate(R.layout.fragment_home, container, false); 

    final Activity activity = this.getActivity(); 
    final HomeFragment fragment = this; 

     @Override 
     public void onSuccess(Call<List<xyzView>> call, Response<List<xyzView>> response) { 
      ListView views = (ListView) container.findViewById(R.id.course_list); 
      views.setAdapter(new xyzAdapter(activity, R.layout.fragment_home_item, response.body(), fragment)); 

如何防止上後退按鈕多次點擊會崩潰嗎?

+1

你'ListView',即'views'爲空。什麼是'容器'? – SripadRaj

+0

@SripadRaj添加一條主線上「正在添加的錯誤上views.setAdapter」那裏是集裝箱 –

+0

當你'新HomeFragment()',沒有它包含的意見被初始化 –

回答

0

您防止一種觀點認爲NullPointerException異常在正確的XML佈局找到它。

例如,如果列表視圖是fragment_home.xml ...

View home = inflater.inflate(R.layout.fragment_home, container, false); 
final ListView views = (ListView) home.findViewById(R.id.course_list); 

... 

    @Override 
    public void onSuccess(Call<List<xyzView>> call, Response<List<xyzView>> response) { 

     views.setAdapter(new xyzAdapter(getActivity(), R.layout.fragment_home_item, response.body(), HomeFragment.this)); 
+0

This Line did the magic謝謝 - >最終ListView views =(ListView)home.findViewById(R.id.course_list); –