2014-04-25 1 views
0

1.이 어댑터의 ListViewjava.lang.NullPointerException이 ListViewAdapter.getView (ListViewAdapter.java:60)이

여기
public class ListViewAdapter extends BaseAdapter { 

    // Declare Variables 
Context context; 
LayoutInflater inflater; 
ArrayList<HashMap<String, String>> data; 
ImageLoader imageLoader; 
HashMap<String, String> resultp = new HashMap<String, String>(); 

public ListViewAdapter(NewsFragment newsFragment, 
     ArrayList<HashMap<String, String>> arraylist) { 
    this.context = context; 
    data = arraylist; 
    imageLoader = new ImageLoader(context); 
} 



@Override 
public int getCount() { 
    return data.size(); 
} 

@Override 
public Object getItem(int position) { 
    return null; 
} 

@Override 
public long getItemId(int position) { 
    return 0; 
} 

public View getView(final int position, View convertView, ViewGroup parent) { 
//protected void onActivityResult(int requestCode, int resultCode, Intent data) { 
    // Declare Variables 
    TextView productname; 
    TextView productid; 
    TextView producttype; 
    ImageView image; 

가 문제이다 인플레이터 프래그먼트에 통합이다 = (LayoutInflater에서) convertView.getContext() .getSystemService (Context.LAYOUT_INFLATER_SERVICE);
// inflater = (LayoutInflater) context.getSystemService (Context.LAYOUT_INFLATER_SERVICE); convertView가 처음에 널 (null)이 될 것이기 때문에

enter code here 

    View itemView = inflater.inflate(R.layout.listview_item, parent, false); 
    // Get the position 
    resultp = data.get(position); 

    // Locate the TextViews in listview_item.xml 
    productname = (TextView) itemView.findViewById(R.id.product); 
    productid = (TextView) itemView.findViewById(R.id.dateOfInput); 
    producttype = (TextView) itemView.findViewById(R.id.productprice); 

    // Locate the ImageView in listview_item.xml 
    image = (ImageView) itemView.findViewById(R.id.flag); 

    // Capture position and set results to the TextViews 
    productname.setText(resultp.get(NewsFragment.PRODUCTNAME)); 
    productid.setText(resultp.get(NewsFragment.PRODUCTPRICE)); 
    producttype.setText(resultp.get(NewsFragment.DATEOFINPUT)); 
    // Capture position and set results to the ImageView 
    // Passes flag images URL into ImageLoader.class 
    imageLoader.DisplayImage(resultp.get(NewsFragment.IMAGE), image); 
    // Capture ListView item click 
    itemView.setOnClickListener(new OnClickListener() { 

     @Override 
     public void onClick(View arg0) { 
      // Get the position 
      resultp = data.get(position); 
      Intent intent = new Intent(context, SingleItemView.class); 
      // Pass all data rank 
      intent.putExtra("productname",resultp.get(NewsFragment.PRODUCTNAME)); 
      // Pass all data country 
    intent.putExtra("productid", resultp.get(NewsFragment.PRODUCTPRICE)); 
      // Pass all data population 
       intent.putExtra("producttype", resultp.get(NewsFragment.DATEOFINPUT));     
      // Pass all data flag 
      intent.putExtra("image", resultp.get(NewsFragment.IMAGE)); 
      // Start SingleItemView Class 
      context.startActivity(intent); 

     } 
    }); 
    return itemView; 
} 

}

+0

여기서'ListViewAdapter' 클래스 생성자에 컨텍스트를 전달하고 있습니까? –

+0

imageLoader = 새로운 ImageLoader (컨텍스트); – user3414330

답변

1

하기, 당신은 잘못이

inflater = (LayoutInflater)convertView.getContext() .getSystemService(Context.LAYOUT_INFLATER_SERVICE); 

을 수행 한 LayoutInflatrot

를 초기화하는 실수를했다.

대신 당신은

inflater = (LayoutInflater)context.getSystemService(Context.LAYOUT_INFLATER_SERVICE); 

같은 것을 할 필요가 있고 당신은 당신이 제대로 어댑터의 생성자를 초기화 할 필요가 컨텍스트를 사용하려는 경우, 현재 자체를 가리키고 있습니다.

+0

inflater = (LayoutInflater) context.getSystemService (Context.LAYOUT_INFLATER_SERVICE); 이 코드는 원래 코드입니다.이 코드는 오류 코드 – user3414330

+0

입니다. 그리고 ListViewAdapter가 단 하나의 페이지 Android 활동 인 조각이므로,이 코드는 작동합니다 !! – user3414330

+0

어댑터에 avtivity 객체를 가질 수있는 경우 activity.getLayoutInflator 메서드가 –

관련 문제