2016-03-25 26 views
2
  • findViewById() döner null fragmanı sınıfında boş döner. ImageButton için ToastfindViewById fragmanı sınıfında

    public MyAdapter(Context context, List<ListItems> values) { 
    
        super(context, R.layout.list_item, values); 
    } 
    
    @Override 
    public View getView(int position, View convertView, ViewGroup parent) { 
    
        View v = convertView; 
        LayoutInflater inflater = LayoutInflater.from(getContext()); 
    
        if(v == null){ 
         v = inflater.inflate(R.layout.list_item, parent, false); 
        } 
    
        ListItems currLoc = LocationList.get(position); 
    
        TextView theTextView = (TextView) v.findViewById(R.id.list_item_textview); 
        theTextView.setText(currLoc.getLocation()); 
    
        TextView theTextView2 = (TextView) v.findViewById(R.id.list_item_textviewP); 
        theTextView2.setText(currLoc.getProfile()); 
    
        ImageView theImageView = (ImageView) v.findViewById(R.id.imageview1); 
        Toast.makeText(getActivity(), "image view="+theImageView, Toast.LENGTH_SHORT).show(); 
        theImageView.setImageResource(R.drawable.bullet); 
    
    
        Switch mySwitch=(Switch) v.findViewById(R.id.onoff); 
        Toast.makeText(getActivity(), ""+mySwitch, Toast.LENGTH_SHORT).show(); 
    
    
        return v; 
    } 
    
+0

Lütfen burada xml göster –

+0

Ve lütfen hata. Ve bu sizin 'parça sınıfınız' değil, 'adaptör' sınıfınız –

+0

LayoutInflater inflater = LayoutInflater.from (getContext()); 'LayoutInflater inflater = ((LayoutInflater) context.getSystemService (Context.LAYOUT_INFLATER_SERVICE)) ; – ELITE

cevap

0

Aldım .... Ben onGetView içinde findViewById tanımlanmış olan .... Switch yani mySwitch getiri null ... yardım lütfen için nesne ancak bir şey gösteriyor OnCreateView tarihinde bildirmek zorunda kaldım.

+1

Arkadaş yorumcular: Bu _is_ bir cevap. Soruna neyin neden olduğunu açıklayan OP. –

+0

** onCreateView ** sadece düzeni döndürdüm. Aslında onun içinde findViewById' olarak adlandırdığınızı bilmiyordum. –