0

Essayer d'utiliser la bibliothèque de volley pour la première fois. mon adaptateur semble être nul à chaque fois. logcat dit:Utilisation de la bibliothèque volvo dans des fragments et un adaptateur de groupe

04-19 14:23:05.943: E/AndroidRuntime(2695): Caused by: java.lang.NullPointerException 
04-19 14:23:05.943: E/AndroidRuntime(2695):  at com.wordpress.yourhappening.happening.Homepage.onStart(Homepage.java:93) 

ligne 93 est list.setAdapter(myadapter);

Ceci est mon fragment:

public class Homepage extends Fragment{ 

    public Homepage(){} 

    ListView list; 
    private static String url = "http://192.168.1.6/webservice/events.php"; 
    static final ArrayList<String> TAG_IMG = new ArrayList<String>(); 
    static final String TAG_SPONSER= "sponser"; 
    static final ArrayList<String> TAG_TITLE= new ArrayList<String>(); 
    static final String TAG_LOCATION="event_location"; 
    static final String TAG_TIME="event_time"; 
    static final String TAG_ENDTIME="event_endtime"; 
    static final String TAG_WHOINVITED="event_whoinvited"; 
    static final ArrayList<String> TAG_MESSAGE=new ArrayList<String>(); 
    static final String TAG_DRESSCODE="event_dresscode"; 
    private ImageLoader mImageLoader; 
    ViewFlipper flippy; 


    @Override 
    public View onCreateView(LayoutInflater inflater, ViewGroup container, 
      Bundle savedInstanceState) { 

     View rootView = inflater.inflate(R.layout.homepage, container, false); 

     return rootView; 
    } 
    @Override 
    public void onStart() { 
     super.onStart(); 
     /////////////Slideshow/////////// 
///////////////////Slider ends\\\\\\\\\\\\\\\\\\\\\ 
     final BentonAdapter myadapter = new BentonAdapter(getActivity(), url, TAG_TITLE, TAG_MESSAGE, TAG_IMG); 
     list.setAdapter(myadapter); 

     RequestQueue queue = Volley.newRequestQueue(getActivity()); 
     mImageLoader = new ImageLoader(queue, new ImageLoader.ImageCache() { 
      private final LruCache<String, Bitmap> mCache = new LruCache<String, Bitmap>(10); 
      public void putBitmap(String url, Bitmap bitmap) { 
       mCache.put(url, bitmap); 
      } 
      public Bitmap getBitmap(String url) { 
       return mCache.get(url); 
      } 
     }); 
     JsonObjectRequest JOR = new JsonObjectRequest(Request.Method.GET, url, null, 
        new Response.Listener<JSONObject>() 
       { 

        @Override 
        public void onResponse(JSONObject response) { 

         try 
          { 
          JSONArray posts = (JSONArray) response.getJSONArray("posts"); 

          for(int i=0; i<posts.length();i++){ 


           TAG_TITLE.add(posts.getJSONObject(i).getString("title")); 
           TAG_MESSAGE.add(posts.getJSONObject(i).getString("message")); 
           TAG_IMG.add(posts.getJSONObject(i).getString("event_img")); 
           } 
          } 
         catch (JSONException e) 
          { 
          } 

         myadapter.notifyDataSetChanged(); 

        }}, new Response.ErrorListener(){ 

         @Override 
         public void onErrorResponse(VolleyError Error) { 
          Toast.makeText(getActivity(), Error.toString(), Toast.LENGTH_LONG).show(); 

         } 
        }); 
     queue.add(JOR); 
    // //Attempting Onclick method \\\\ 
     list=(ListView)getView().findViewById(R.id.list);// has to be before list.setonclick... 

    });//end of onitemclick 
    //startService(new Intent(this, NotificationService.class)); 
    }//end of onCreate\\\ 
} 

Mon ArrayAdapter (dans un fichier java différent):

public class BentonAdapter extends ArrayAdapter<String>{ 
    Context context; 
    ArrayList<String>mtitle; 
    ArrayList<String>mdesc; 
    ArrayList<String>mImg; 
    String murl ; 

    private ImageLoader mImageLoader; 


    public BentonAdapter(Context c, String url, ArrayList<String>title, ArrayList<String>desc, 
        ArrayList<String>img) { 
     super(c, R.layout.single_line, R.id.title, title); 
     this.context = c; 
     this.mtitle = title; 
     this.mdesc=desc; 
     this.mImg=img; 
     this.murl = url; 
    } 

    @Override 
    public View getView(int position, View convertView, ViewGroup parent) { 
     LayoutInflater inflater = (LayoutInflater) context.getSystemService(Context.LAYOUT_INFLATER_SERVICE); 
     View row = inflater.inflate(R.layout.single_line, parent, false); 
     TextView finaltitle = (TextView)row.findViewById(R.id.title); 
     TextView finaldesc = (TextView)row.findViewById(R.id.subTitle_single); 
     NetworkImageView ximg = (NetworkImageView)row.findViewById(R.id.event_pic); 
     finaltitle.setText(mtitle.get(position)); 
     finaldesc.setText(mdesc.get(position)); 
     ximg.setImageUrl(mImg.get(position), mImageLoader); 
     return row; 
    } 

} 

Je suis presque certain que le problème a à voir avec le contexte, mais je ne peux pas le comprendre.

+0

en utilisant l'activité au lieu du contexte maintenant et toujours pas de changement. – crushman

Répondre

0

J'ai eu le même problème, l'ai résolu. Dans votre fragment override onActivityCreated, puis list = getlistview() De même, déclarez et définissez l'adaptateur ici.

Questions connexes