Android layout 膨胀布局中引发空指针异常

Android layout 膨胀布局中引发空指针异常,android-layout,Android Layout,我有以下代码: 1)从膨胀的EditText中读取值并将其存储在ArrayList中的方法 private List<String> getDynEditText(LinearLayout parentLayout, int resourceId) { List<String> contactDataList = new ArrayList<String>(); int dataSize = (parentLayout.getChildCount()

我有以下代码:

1)从膨胀的EditText中读取值并将其存储在ArrayList中的方法

private List<String> getDynEditText(LinearLayout parentLayout, int resourceId) {
  List<String> contactDataList = new ArrayList<String>();
  int dataSize = (parentLayout.getChildCount() != 0 ) ? parentLayout.getChildCount() : 0;

  for (int i=0; i<dataSize;i++) {
     EditText editText = (EditText) parentLayout.getChildAt(i).findViewById(resourceId);

     String editString = editText.getText().toString();

     if (editString != null && !editString.trim.equals("")) {
       contactDataList.add(editString);
   }
}
问题:

  java.lang.NullPointerException

  at.dyn.edt.contacts.ContactActivityDemo.getDynEditText(....)
如果我点击这个,它会在行中显示错误

  int dataSize = (parentLayout.getChildCount() != 0) ? parentLayout.getChildCount() : 0;

有人能帮我吗?

NPE可以发生在多个地方:

  • parentLayout
    null

  • parentLayout.getChildAt(i)
    null

  • editText
    null

  • editText.getText()
    为空


您应该始终分析完整stacktrace以找到异常的根本原因。

请提供完整stacktrace
  int dataSize = (parentLayout.getChildCount() != 0) ? parentLayout.getChildCount() : 0;