1

BaseExpandableListAdaptor를 확장하는 ExpandListAdapter 클래스의 LayoutInflator에서 널 포인터 예외가 발생합니다.LayoutInflator의 Null 포인터 예외

import java.util.ArrayList; 

import android.content.Context; 
import android.view.LayoutInflater; 
import android.view.View; 
import android.view.ViewGroup; 
import android.widget.BaseExpandableListAdapter; 
import android.widget.TextView; 

public class ExpandListAdapter extends BaseExpandableListAdapter{ 
     private Context context; 
     private ArrayList<ExpandListGroup> groups; 

     public ExpandListAdapter(Context context, ArrayList<ExpandListGroup> groups){ 
      this.context=context; 
      this.groups=groups; 
     } 

     public void addItem(ExpandListChild item, ExpandListGroup group){ 
      if(!groups.contains(group)){ 
       groups.add(group); 
      } 
      int index=groups.indexOf(group); 
      ArrayList<ExpandListChild> ch=groups.get(index).getItems(); 
      ch.add(item); 
      groups.get(index).setItems(ch); 
     } 

     public Object getChild(int groupPosition, int childPosition){ 
      ArrayList<ExpandListChild> chList=groups.get(groupPosition).getItems(); 
      return chList.get(childPosition); 
     } 

     public long getChildId(int groupPosition, int childPosition){ 
      return childPosition; 
     } 

     public View getChildView(int groupPosition, int childPosition, boolean isLastChild, View view, ViewGroup parent){ 
      ExpandListChild child = (ExpandListChild) getChild(groupPosition, childPosition); 
      if (view == null) { 

       LayoutInflater infalInflater = (LayoutInflater) this.context.getSystemService(context.LAYOUT_INFLATER_SERVICE); 
       view = infalInflater.inflate(R.layout.expandlist_child_item, null); 
      } 

       TextView tv = (TextView) view.findViewById(R.id.tvChild); 
       tv.setText(child.getName().toString()); 
       tv.setTag(child.getTag()); 

       // TODO Auto-generated method stub 
       return view; 

     } 

     public int getChildrenCount(int groupPosition) { 
      ArrayList<ExpandListChild> chList = groups.get(groupPosition).getItems(); 
      return chList.size(); 
     } 

     public Object getGroup(int groupPosition) { 
      return groups.get(groupPosition); 
     } 

     public int getGroupCount() { 
      return groups.size(); 
     } 

     public long getGroupId(int groupPosition) { 
      // TODO Auto-generated method stub 
      return groupPosition; 
     } 

     public View getGroupView(int groupPosition, boolean isLastChild, View view, ViewGroup parent) { 
      ExpandListGroup group = (ExpandListGroup) getGroup(groupPosition); 
      if (view == null) { 
       LayoutInflater inf = (LayoutInflater) context.getSystemService(context.LAYOUT_INFLATER_SERVICE); 
       view = inf.inflate(R.layout.two_lines_list_layout, null); 
      } 
      TextView tv = (TextView) view.findViewById(R.id.line_book); 
      tv.setText(group.getName()); 
      TextView tv2 = (TextView) view.findViewById(R.id.line_author); 
      tv2.setText(group.getAuthor()); 
      return view; 
     } 

     public boolean hasStableIds() { 
      // TODO Auto-generated method stub 
      return true; 
     } 
     public boolean isChildSelectable(int arg0, int arg1) { 
      // TODO Auto-generated method stub 
      return true; 
     } 
} 

나는 줄에서 예외를 얻고있다 : LayoutInflater에서 infalInflater = (LayoutInflater에서) this.context.getSystemService (context.LAYOUT_INFLATER_SERVICE);

또한, 자사의 "정적 필드 Context.LAYOUT_INFLATER_SERVICE은 정적 인 방법으로 접근해야한다"라고 그 선에서 경고를 보여주는

는 제발 도와주세요 .. !!

답변

2

그래서 처음에는 쉬운 부분. 정적 인 방법으로 LAYOUT_INFLATER_SERVICE에 액세스해야한다는 경고는 인스턴스가 아닌 클래스를 통해 변수에 액세스 할 수 있도록 context.LAYOUT_INFLATER_SERVICE에서 Context.LAYOUT_INFLATER_SERVICE (Capitol 'C')로 변경해야합니다.

둘째, null 포인터 예외가 'context'변수에서 확실히 발생하므로 ExpandListAdapter를 인스턴스화 할 때마다 컨텍스트에 null 값을 제공하게됩니다. 그 코드를주지 않았으므로 살펴볼 수는 없지만 onCreateView 또는 onViewCreated에서 ExpandListAdapter를 인스턴스화하는 것이 좋습니다. 이것을 액티비티에 생성하는 경우에는 액티비티 ('this')에 대한 참조를 전달하고, 조각에서 생성하는 경우 getActivity()에 대한 호출을 전달합니다. 귀하의 단편이 활동에 첨부되어 있지 않은 경우 (귀하의 쟁점 일 수 있음) 그래도 작동하지 않습니다.

+0

정말로 고맙습니다. ExpandListAdapter를 인스턴스화 할 때 null 컨텍스트를 전달했습니다. 조각에서 호출 했으므로 getActivity()가 작동했습니다 ...! – user2602096

3

줄에서 예외가 발생합니다. LayoutInflater infalInflater = (LayoutInflater) this.context.getSystemService (context.LAYOUT_INFLATER_SERVICE);

contextnull입니다. ActivityExpandListAdapter 생성자로 전달했는지 확인하십시오. 또한

그 다음,

Context보다는 Activity을 위해 생성자를 변경

"정적 필드 Context.LAYOUT_INFLATER_SERVICE은 정적 인 방법으로 접근해야한다"라고 그 줄에서 경고를 표시 변화 :

LayoutInflater infalInflater = (LayoutInflater) this.context.getSystemService(context.LAYOUT_INFLATER_SERVICE); 

에 :

LayoutInflater infalInflater = whateverYouDecideToCallYourActivityDataMember.getLayoutInflater(); 
+0

도와 주셔서 감사합니다. 나는 ExpandListAdapter 생성자에 Activity를 전달하지 않고 대신 null 컨텍스트를 전달했습니다. – user2602096