第一次尝试使用排球库。我的适配器似乎每次都是null。 logcat说:
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)
第93行是list.setAdapter(myadapter);
这是我的片段:
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\\\
}
My ArrayAdapter(在不同的java文件中):
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;
}
}
我几乎可以肯定这个问题与上下文有关,但我无法弄明白。
答案 0 :(得分:0)
我有类似的问题,解决了它。 在你的片段中 然后覆盖onActivityCreated list = getlistview() 另外,在那里声明并设置适配器。