2016-03-25 2 views
2
  • findViewById() возвращает null в классе фрагментов. Toast для ImageButton показывает что-то, кроме объекта для Switch т.е. mySwitch возвращается null ... пожалуйста, помогите ....findViewById возвращает null в классе фрагментов

    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

Пожалуйста, покажите xml здесь –

+0

И ошибка, пожалуйста. И это не ваш «класс фрагментов», это ваш класс «адаптер», который –

+0

заменит «LayoutInflater inflater = LayoutInflater.from (getContext());' by 'LayoutInflater inflater = ((LayoutInflater) context.getSystemService (Context.LAYOUT_INFLATER_SERVICE)) ; ' – ELITE

ответ

0

Я получил его .... я определил findViewById в onGetView Мне пришлось объявить его на OnCreateView Извините .....

+1

Рецензенты: этот _is_ ответ. Это ОП объясняет, что вызвало проблему. –

+0

Я думал, что ** onCreateView ** просто возвращает макет. Я не знал, что вы на самом деле называете 'findViewById' внутри него. –

Смежные вопросы