2017-10-14 9 views
0

Donc, fondamentalement, je suis en train d'avoir un ArrayAdapter dans un fragment à utiliser sur un TabLayoutArrayAdapter à l'intérieur Tablayout utilisant des fragments

public class fragment_restaurants extends Fragment{ 

    @Override 
    public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) { 
     View view = inflater.inflate(R.layout.fragment_restaurants, container, false); 

     ArrayList<InformationModel> hotelList = new ArrayList<>(); 

     hotelList.add(new InformationModel("Copacabana Palace", "Copacabana", "215")); 
     hotelList.add(new InformationModel("Copacabana Resort", "Botafogo", "589")); 
     hotelList.add(new InformationModel("Sarrada Motel", "Bangu", "105")); 
     hotelList.add(new InformationModel("5 Estrelas da Oi", "Centro", "669")); 
     hotelList.add(new InformationModel("Pereira Cardoso", "Saramamduba", "123")); 
     hotelList.add(new InformationModel("Sepetiba Palace", "Sepetiba", "774")); 
     hotelList.add(new InformationModel("Copacabana Palace", "Copacabana", "215")); 
     hotelList.add(new InformationModel("Copacabana Resort", "Botafogo", "589")); 
     hotelList.add(new InformationModel("Sarrada Motel", "Bangu", "105")); 
     hotelList.add(new InformationModel("5 Estrelas da Oi", "Centro", "669")); 
     hotelList.add(new InformationModel("Pereira Cardoso", "Saramamduba", "123")); 
     hotelList.add(new InformationModel("Sepetiba Palace", "Sepetiba", "774")); 

     InformationAdapter adapter = new InformationAdapter(mContext, hotelList); 
     ListView listView = (ListView) view.findViewById(R.id.listViewTabRestaurants); 

     listView.setAdapter(adapter); 
     return view; 
    } 
} 

J'ai cette erreur:

FATAL EXCEPTION: main Process: com.example.rick.tourapp, PID: 4616 java.lang.NullPointerException: Attempt to invoke virtual method 'java.lang.Object android.content.Context.getSystemService(java.lang.String)' on a null object reference

Comment puis-je résoudre ce? Dois-je faire une autre classe avec cet adaptateur de tableau déjà fait et initialiser à l'intérieur de cette sous-classe de fragment?

Répondre

0

regardant votre code semble comme votre mContext est nulle

lsinitialisez:

mContext = getActivity(); 
0

Tente Desta forma:

if (getView != null){ 
    InformationAdapter adapter = new InformationAdapter(mContext, hotelList); 
    ListView listView = (ListView) 
    view.findViewById(R.id.listViewTabRestaurants); 
    listView.setAdapter(adapter); 
} 

Se o problema continuar você pode tentar substituir mContext por getContext() apenas para testes.

+0

Eu passei getActivity() como contexte sem o si déclaration e funciono de boa. Masquer la commande pour voir le contenu. Saberia como résolveur? Eu quueria que pas de balayage ne visionner ou tablayout acompanhasse. Nao faco idée de como fazer isso. Sabe como fazer? –

+0

Desculpe, não faco ideia, eu recomendaria que voc de desir uma olhada nessa bibliothèque: https://github.com/ogaclejapan/SmartTabLayout, eu utiliso-a esta funcionando perfeitamente aqui. –

+0

Tentei usar mas nao facia idée de como aplicar no meu projeto. Vi ate une demo deles mas nao entendi. Vc conhece algum artigo ou vidéo que ensine a implementacao? –