片段替换空指针异常

时间:2014-06-26 05:53:47

标签: android-fragments

我试图在列表视图的onItemClick内部替换片段,我想将列表片段中的选定项目名称发送到另一个片段,但是它显示空指针异常

@覆盖     public void onItemClick(AdapterView arg0,View view,int position,             长id)     {

try
    {
        Log.e("----- inside onItemClick -----", citySelected);

        MapVisibleFragment newFragment = new MapVisibleFragment();
        Bundle args = new Bundle();
        args.putString("CITYNAME", citySelected);
        newFragment.setArguments(args);
        ((BaseContainerFragment) getParentFragment()).replaceFragment(
                newFragment, true);

    } catch (Exception e)
    {
        e.printStackTrace();
    }

}

但同样的概念是在按钮点击内部进行以下操作

 try 
 {
     ListFragmentFromDBRecords2 newFragment = new ListFragmentFromDBRecords2();
     Bundle args = new Bundle();
     args.putStringArrayList("names", allData);
     newFragment.setArguments(args);
     ((BaseContainerFragment) getParentFragment()).replaceFragment(
      newFragment, true);

     Log.d("--->>>>", allData.toString());

    } 
 catch (Exception e) 
    {
     e.printStackTrace();
    }

1 个答案:

答案 0 :(得分:0)

根据您的评论

((BaseContainerFragment) getParentFragment())

对您而言为空。检查getParentFragment()方法返回null的原因。