我有一个适配器,它从API中获取项目列表,然后将其放入列表视图中。这是我第一次使用适配器和列表视图,到目前为止,我正在努力奋斗!
为什么我收到此错误?我的背景错了还是什么?
package com.example.tvrplayer;
import java.io.File;
import java.util.ArrayList;
import java.util.HashMap;
import org.json.JSONArray;
import org.json.JSONException;
import org.json.JSONObject;
import android.app.Activity;
import android.content.Context;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.TextView;
public class HiddenChannelsListAdapter extends BaseAdapter {
private Context mContext;
private ArrayList mItems;
private LayoutInflater mInflater;
public HiddenChannelsListAdapter(Context ctx, ArrayList list) {
mItems = list;
mContext = ctx;
mInflater = (LayoutInflater) mContext.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
}
@Override
public int getCount() {
return mItems.size();
}
@Override
public HashMap getItem(int position) {
return (HashMap) mItems.get(position);
}
@Override
public long getItemId(int position) {
return position;
}
@Override
public View getView(int position, View convertView, ViewGroup parent) {
System.out.println("getView " + position + " " + convertView);
if (convertView == null) {
convertView = mInflater.inflate(R.layout.channel_item, parent, false);
} else {
((TextView) convertView.findViewById(R.id.channel_text)).setText("Testing");
}
return convertView;
}
public static class ViewHolder {
public TextView textView;
}
}
Asyntask从服务器获取项目:
package com.example.tvrplayer;
import java.io.File;
import java.util.ArrayList;
import java.util.HashMap;
import org.json.JSONArray;
import org.json.JSONException;
import org.json.JSONObject;
import android.content.Context;
import android.os.AsyncTask;
import android.util.Log;
import android.view.View;
import android.widget.ListView;
class ChannelPair {
public ListView lv;
public ArrayList channelList;
public Context ctx;
}
public class ChannelHandler extends AsyncTask<Object, Integer, ChannelPair> {
@Override
protected ChannelPair doInBackground(Object... params) {
ChannelPair p = new ChannelPair();
String apiurl = (String) params[0];
String linkid = (String) params[1];
String username = (String) params[2];
String channelID = null;
View vw = (View) params[3];
ListView lv = (ListView) vw.findViewById(R.id.list);
p.lv = lv;
JSONArray channels;
ArrayList< HashMap < String, String > > channelList = new ArrayList < HashMap < String, String > > ();
try {
channels = Json.getJson(apiurl + "/rest/channel/"+ username +"/"+ linkid, "GET");
Log.i("CHANNELS", channels.toString());
for (int i=0; i < channels.length(); i++) {
JSONObject json_data = channels.getJSONObject(i);
String name = json_data.getString("Name");
String channelid = json_data.getString("ChannelID");
HashMap<String, String> channelObject = new HashMap<String, String>();
// if ( json_data.getString("ParentPath") == "" ) {
channelObject.put("id", channelid);
channelObject.put("name", name);
channelList.add(channelObject);
// }
}
Log.i("ROOT CHANNELS", channelList.toString());
p.channelList = channelList;
} catch (JSONException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
return p;
}
@Override
protected void onPostExecute(final ChannelPair p) {
HiddenChannelsListAdapter adapter = new HiddenChannelsListAdapter(p.ctx, p.channelList);
p.lv.setAdapter(adapter);
}
}
错误在这一行:
HiddenChannelsListAdapter.java:28
mInflater = (LayoutInflater) mContext.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
完整的错误代码:
java.lang.NullPointerException
at com.example.tvrplayer.HiddenChannelsListAdapter.<init>(HiddenChannelsListAdapter.java:28)
at com.example.tvrplayer.ChannelHandler.onPostExecute(ChannelHandler.java:71)
at com.example.tvrplayer.ChannelHandler.onPostExecute(ChannelHandler.java:1)
at android.os.AsyncTask.finish(AsyncTask.java:602)
at android.os.AsyncTask.access$600(AsyncTask.java:156)
at android.os.AsyncTask$InternalHandler.handleMessage(AsyncTask.java:615)
at android.os.Handler.dispatchMessage(Handler.java:99)
at android.os.Looper.loop(Looper.java:137)
at android.app.ActivityThread.main(ActivityThread.java:4514)
at java.lang.reflect.Method.invokeNative(Native Method)
at java.lang.reflect.Method.invoke(Method.java:511)
at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:790)
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:557)
at dalvik.system.NativeStart.main(Native Method)