【问题标题】:Dynamically add/remove the same xml view动态添加/删除相同的 xml 视图
【发布时间】:2017-05-09 01:57:46
【问题描述】:

我正在尝试在从我的片段中添加和删除相同的 linearLayout 视图之间切换。每次我添加代码行:

    ((ViewGroup)indexLayout.getParent()).removeView(indexLayout)

我得到一个空指针异常,即使尝试重新添加视图...

OfflineFragment.java

private void displayIndex() {

     indexLayout = (LinearLayout)v.findViewById(R.id.side_index);

        List<String> indexList = new ArrayList<>(mapIndex.keySet());

        TextView textView;

        for (String index : indexList) {
            textView = (TextView) getActivity().getLayoutInflater().inflate(R.layout.alphabet_indicator, null);
            textView.setText(index);
            textView.setOnClickListener(this);
            indexLayout.addView(textView);
        }
}

private void showDialog() {
    Dialog deleteDialog = new AlertDialog.Builder(getActivity()).setTitle("Warning:")
            .setMessage("Are you sure that you want to delete all offline contacts?")
            .setPositiveButton("Yes", new DialogInterface.OnClickListener() {
                        public void onClick(DialogInterface dialog, int whichButton) {

                            //Delete cached data from internal storage and clear data structures
                            File dir = getActivity().getFilesDir();
                            File file = new File(dir, "cached.txt");
                            boolean deleted = file.delete();
                            System.out.println(deleted);
                            listView.setAdapter(null);
                            cachedSearch.clear();
                            l.clear();
                            mapIndex.clear();
                            sections = null;
                            names = null;
                           ((ViewGroup)indexLayout.getParent()).removeView(indexLayout)

                        }
                    }
            )
            .setNegativeButton("No", new DialogInterface.OnClickListener() {
                        public void onClick(DialogInterface dialog, int whichButton) {
                            dialog.cancel();
                        }
                    }
            ).create();
    deleteDialog.show();
}

堆栈跟踪:

java.lang.NullPointerException: Attempt to invoke virtual method 'void android.widget.LinearLayout.addView(android.view.View)' on a null object reference
                                                                                    at com.fil.uk.mobile.fidelitycontacts.Fragments.OfflineFragment$override.displayIndex(OfflineFragment.java:122)
                                                                                    at com.fil.uk.mobile.fidelitycontacts.Fragments.OfflineFragment$override.refreshData(OfflineFragment.java:215)
                                                                                    at com.fil.uk.mobile.fidelitycontacts.Fragments.OfflineFragment$override.access$dispatch(OfflineFragment.java)
                                                                                    at com.fil.uk.mobile.fidelitycontacts.Fragments.OfflineFragment.refreshData(OfflineFragment.java:0)
                                                                                    at com.fil.uk.mobile.fidelitycontacts.CarouselFragment.refreshData(CarouselFragment.java:215)
                                                                                    at com.fil.uk.mobile.fidelitycontacts.MainActivity.update(MainActivity.java:120)
                                                                                    at com.fil.uk.mobile.fidelitycontacts.Fragments.SearchResultFragment.refreshData(SearchResultFragment.java:212)
                                                                                    at com.fil.uk.mobile.fidelitycontacts.Fragments.SearchResultFragment.onCreateView(SearchResultFragment.java:105)
                                                                                    at android.support.v4.app.Fragment.performCreateView(Fragment.java:2074)
                                                                                    at android.support.v4.app.FragmentManagerImpl.moveToState(FragmentManager.java:1104)
                                                                                    at android.support.v4.app.FragmentManagerImpl.moveToState(FragmentManager.java:1286)
                                                                                    at android.support.v4.app.BackStackRecord.run(BackStackRecord.java:758)
                                                                                    at android.support.v4.app.FragmentManagerImpl.execPendingActions(FragmentManager.java:1671)
                                                                                    at android.support.v4.app.FragmentManagerImpl$1.run(FragmentManager.java:532)
                                                                                    at android.os.Handler.handleCallback(Handler.java:739)
                                                                                    at android.os.Handler.dispatchMessage(Handler.java:95)
                                                                                    at android.os.Looper.loop(Looper.java:158)
                                                                                    at android.app.ActivityThread.main(ActivityThread.java:7225)
                                                                                    at java.lang.reflect.Method.invoke(Native Method)
                                                                                    at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:1230)
                                                                                    at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:1120)

【问题讨论】:

  • 为异常提供堆栈跟踪!
  • @Jordan 完成队友!
  • 你确定你的xml中有一个ID为R.id.side_index的LinearLayout吗?
  • 另外,displayIndex() 中的 v 来自哪里?
  • @Jordan 是的。首次启动片段时它加载正常,唯一的问题是当我尝试将视图添加回布局时,在方法中删除它后 - showDialog

标签: android layout add removechild dynamic-view


【解决方案1】:

如果你删除视图

 ((ViewGroup)indexLayout.getParent()).removeView(indexLayout)

比你不能拿回来!如果你想使用它,你必须再次添加它

如果为 null 则尝试添加 View

private void displayIndex() {

     indexLayout = (LinearLayout)v.findViewById(R.id.side_index);

      if(indexLayout==null){
          LayoutInflater inflater = (LayoutInflater) getContext().getSystemService(getContext().LAYOUT_INFLATER_SERVICE);
          View mView = inflater.inflate(R.layout.your_view_having_indexLayout , null, false);
          indexLayout =(LinearLayout)mView.findViewById(R.id.side_index);
      }  
        List<String> indexList = new ArrayList<>(mapIndex.keySet());

        TextView textView;

        for (String index : indexList) {
            textView = (TextView) getActivity().getLayoutInflater().inflate(R.layout.alphabet_indicator, null);
            textView.setText(index);
            textView.setOnClickListener(this);
            indexLayout.addView(textView);
        }
}

【讨论】:

  • 感谢您的帮助@Sushant,我不再收到空指针异常...但不再显示文本视图。我会解决这个问题的
猜你喜欢
  • 2012-11-19
  • 2011-04-29
  • 1970-01-01
  • 2020-08-23
  • 2018-10-21
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多