2016-10-18 5 views
-3

Получение этой ошибки: enter image description here Я пытаюсь показать gridview в фрагменте, но эта ошибка возникает, когда я устанавливаю адаптер.set adapter in fragment

Мой фрагмент выглядит так:

public class ExploreFragment extends Fragment { 


GridView gv; 
Context context; 
ArrayList prgmName; 
public static String [] prgmNameList={"Adventure","Animals","Automotive","Foods","Diy","Business","Technology","Creative Art","Entertainment"}; 
public static int [] prgmImages={R.drawable.adventure,R.drawable.animals,R.drawable.automobiles,R.drawable.food_and_drinks,R.drawable.diy,R.drawable.business,R.drawable.technology,R.drawable.creativity,R.drawable.entertainment}; 

@Nullable 
@Override 
public View onCreateView(LayoutInflater inflater, @Nullable ViewGroup container, @Nullable Bundle savedInstanceState) { 
    return super.onCreateView(inflater, container, savedInstanceState); 
    View view = inflater.inflate(R.layout.fragment_explore,container,false); 
    gv=(GridView) view.findViewById(R.id.fragment_explore_gridView1); 
    gv.setAdapter(new CustomAdapter((MainActivity) getContext().getApplicationContext(),prgmNameList,prgmImages)); 
} 

Пожалуйста, поправьте меня здесь.

+0

Вам не нужен оператор return, который является переменной «view». – vidulaJ

ответ

0

Comment return super and add return view at end

@Nullable 
    @Override 
    public View onCreateView(LayoutInflater inflater, @Nullable ViewGroup container, @Nullable Bundle savedInstanceState) { 
     // return super.onCreateView(inflater, container, savedInstanceState); 
     View view = inflater.inflate(R.layout.fragment_explore,container,false); 
     gv=(GridView) view.findViewById(R.id.fragment_explore_gridView1); 
     gv.setAdapter(new CustomAdapter((MainActivity) getContext().getApplicationContext(),prgmNameList,prgmImages)); 
     return view; 
    } 
0

Добавьте эту строку ниже установленного адаптера линии

return view; 

Ваш onCreateView будет выглядеть следующим образом:

public View onCreateView(LayoutInflater inflater, @Nullable ViewGroup container, @Nullable Bundle savedInstanceState) { 
     View view = inflater.inflate(R.layout.fragment_explore,container,false); 
     gv=(GridView) view.findViewById(R.id.fragment_explore_gridView1); 
     gv.setAdapter(new CustomAdapter((MainActivity) getContext().getApplicationContext(),prgmNameList,prgmImages)); 
     return view; 
    } 
-1

возвращение вид после накачивания

@Nullable 
    @Override 
    public View onCreateView(LayoutInflater inflater, @Nullable ViewGroup container, @Nullable Bundle savedInstanceState) { 
     // return super.onCreateView(inflater, container, savedInstanceState); 
     View view = inflater.inflate(R.layout.fragment_explore,container,false); 
     gv=(GridView) view.findViewById(R.id.fragment_explore_gridView1); 
     gv.setAdapter(new CustomAdapter((MainActivity) getContext().getApplicationContext(),prgmNameList,prgmImages)); 
     return view; 
    }