2016-12-23 2 views
-1

Я собираюсь установить ImageView с setImageResource, но он не работает. ничего не ошибка тоже. и logcat кажется прекрасным.setImageResource не работает

Вот мой код:

public View onCreateView(LayoutInflater inflater, ViewGroup container, 
         Bundle savedInstanceState) { 
    View view = inflater.inflate(R.layout.fragment_item_list_request, container, false); 
    setHasOptionsMenu(true); 

    View anotherView = inflater.inflate(R.layout.style_fragment_list_request,container,false); 

    imgView = (CircleImageView)anotherView.findViewById(R.id.listPhoto); 

    imgView.setImageResource(R.drawable.pastel_red); 
} 

Учитывая, что я назвал и объявить ImageView из другого макета. Является проблемой, если я раздуваю 2 макета только для установленного изображения для другого макета?

Все, что здесь не так?

+0

Вы добавляете макет другого представления в основной макет после раздувания и настройки ресурса изображения? –

+0

нет, я раздуваю еще один вид сначала, затем устанавливаю изображение. View view = inflater.inflate (R.layout.fragment_item_list_request, контейнер, false); setHasOptionsMenu (true); View anotherView = inflater.inflate (R.layout.style_fragment_list_request, container, false); imgView = (CircleImageView) anotherView.findViewById (R.id.listPhoto); imgView.setImageResource (R.drawable.pastel_red); } – Borom1r

+0

Вы должны добавить anotherView, иначе он не будет показан. –

ответ

0
public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) { 
    View view = inflater.inflate(R.layout.fragment_item_list_request, container, false); 
    setHasOptionsMenu(true); 
    return view; 
} 

public void onActivityCreated(){ 
    ViewGroup layout = (ViewGroup) getView().findById(R.id.layout_another_view); // need layout for anotherView in fragment_item_list_request.xml 

    View anotherView = inflater.inflate(R.layout.style_fragment_list_request,container,false); 
    imgView = (CircleImageView)anotherView.findViewById(R.id.listPhoto); 
    imgView.setImageResource(R.drawable.pastel_red); 

    layout.addView(anotherView); 
} 
0

Что делать, если вы сделаете это вместо:

View anotherView = inflater.inflate(R.layout.style_fragment_list_request,null,false); 
+0

еще не работает – Borom1r

0

заявление вернуть anotherView; отсутствует в onCreateView().

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