我尝试从Facebook发帖和评论并将其显示在ExpandableList中,但我有错误, 请任何人都可以帮我解决这个错误.............
MianActivity.java
public class MainActivity extends Activity {
private static final String App_ID = "XXXXXXXXXXXXX";
private static final String App_Secret = "YYYYYYYYYYYYYYYYY";
ExpandableListAdapter listAdapter;
ExpandableListView expListView;
List<String> listDataHeader;
HashMap<String, List<String>> listDataChild;
ProgressDialog pd;
String comment;
List<String> comm = new ArrayList<String>();
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
// get the listview
expListView = (ExpandableListView) findViewById(R.id.lvExp);
pd = new ProgressDialog(this);
new asy().execute("https://graph.facebook.com/oauth/access_token?client_id="+App_ID+"&client_secret="+App_Secret+"&grant_type=client_credentials");
}
public class asy extends AsyncTask<String, String, String>
{
GetData getdata = new GetData();
@Override
protected String doInBackground(String... params) {
// TODO Auto-generated method stub
return getdata.GetPost(params[0]);
}
@Override
protected void onPreExecute() {
// TODO Auto-generated method stub
super.onPreExecute();
pd.setTitle("fetching");
pd.setMessage("watting...");
pd.show();
}
@Override
protected void onPostExecute(String result) {
// TODO Auto-generated method stub
try {
JSONObject obj = new JSONObject(result);
JSONArray finalObj = obj.getJSONArray("data");
for (int i = 0; i < finalObj.length(); i++)
{
final String message = finalObj.optJSONObject(i).optString(
"message");
final String comments = finalObj.optJSONObject(i).optString(
"comments");
if(!comments.equals(""))
{
JSONObject jo = new JSONObject(comments);
JSONArray ja = jo.getJSONArray("data");
for (int j = 0; j < ja.length(); j++)
{
comment = ja.optJSONObject(j).optString("message");
Log.i("comments", comment);
}
}
listDataHeader.add(message);
comm.add(comment);
listDataChild.put(listDataHeader.get(i), comm);
}
Log.i("size", finalObj.length()+"");
} catch (JSONException e1) {
// TODO Auto-generated catch block
e1.printStackTrace();
}
listAdapter = new ExpandableListAdapter(getApplicationContext(), listDataHeader, listDataChild);
// setting list adapter
expListView.setAdapter(listAdapter);
pd.dismiss();
}
}
}
ExpandableListAdapter.java
public class ExpandableListAdapter extends BaseExpandableListAdapter {
private Context _context;
private List<String> _listDataHeader; // header titles
// child data in format of header title, child title
private HashMap<String, List<String>> _listDataChild;
public ExpandableListAdapter(Context context, List<String> listDataHeader,
HashMap<String, List<String>> listChildData) {
this._context = context;
this._listDataHeader = listDataHeader;
this._listDataChild = listChildData;
}
@Override
public Object getChild(int groupPosition, int childPosititon) {
return this._listDataChild.get(this._listDataHeader.get(groupPosition))
.get(childPosititon);
}
@Override
public long getChildId(int groupPosition, int childPosition) {
return childPosition;
}
@Override
public View getChildView(int groupPosition, final int childPosition,
boolean isLastChild, View convertView, ViewGroup parent) {
final String childText = (String) getChild(groupPosition, childPosition);
if (convertView == null) {
LayoutInflater infalInflater = (LayoutInflater) this._context
.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
convertView = infalInflater.inflate(R.layout.list_item, null);
}
TextView txtListChild = (TextView) convertView
.findViewById(R.id.lblListItem);
txtListChild.setText(childText);
return convertView;
}
@Override
public int getChildrenCount(int groupPosition) {
return this._listDataChild.get(this._listDataHeader.get(groupPosition))
.size();
}
@Override
public Object getGroup(int groupPosition) {
return this._listDataHeader.get(groupPosition);
}
@Override
public int getGroupCount() {
return this._listDataHeader.size();
}
@Override
public long getGroupId(int groupPosition) {
return groupPosition;
}
@Override
public View getGroupView(int groupPosition, boolean isExpanded,
View convertView, ViewGroup parent) {
String headerTitle = (String) getGroup(groupPosition);
if (convertView == null) {
LayoutInflater infalInflater = (LayoutInflater) this._context
.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
convertView = infalInflater.inflate(R.layout.list_group, null);
}
TextView lblListHeader = (TextView) convertView
.findViewById(R.id.lblListHeader);
lblListHeader.setTypeface(null, Typeface.BOLD);
lblListHeader.setText(headerTitle);
return convertView;
}
@Override
public boolean hasStableIds() {
return false;
}
@Override
public boolean isChildSelectable(int groupPosition, int childPosition) {
return true;
}
}
GetData.java
private static final String OWNER_OF_FEED = "XXXXXXXXXXX";
String access_token;
InputStream is = null;
String data = "";
String uri;
String comment;
ExpandableListAdapter listAdapter;
ExpandableListView expListView;
List<String> listDataHeader;
// to get facebook page post.....
public String GetPost(String url)
{
try {
HttpClient hc = new DefaultHttpClient();
HttpGet hg = new HttpGet(url);
HttpResponse hr = hc.execute(hg);
HttpEntity he = hr.getEntity();
access_token = EntityUtils.toString(he);
// access_token contains thing like "access_token=XXXXXXXXXX|YYYYYY" ,
//need to replace pipe (this is ugly!)
uri = "https://graph.facebook.com/" + OWNER_OF_FEED + "/feed?"
+ access_token.replace("|", "%7C");
Log.i("uri", uri);
HttpGet get = new HttpGet(uri);
HttpResponse response = hc.execute(get);
HttpEntity e = response.getEntity();
is = e.getContent();
}catch (ParseException e) {
// TODO Auto-generated catch block
e.printStackTrace();
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
try {
InputStreamReader in = new InputStreamReader(is, "iso-8859-1");
BufferedReader reader = new BufferedReader(in, 8);
StringBuilder sb = new StringBuilder();
String line = null;
while ((line = reader.readLine()) != null)
{
sb.append(line + "\n");
}
is.close();
data = sb.toString();
Log.i("data", data);
} catch (UnsupportedEncodingException e) {
// TODO Auto-generated catch block
e.printStackTrace();
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
return data;
}
logcat的
12-31 01:48:33.439: E/AndroidRuntime(1357): FATAL EXCEPTION: main
12-31 01:48:33.439: E/AndroidRuntime(1357): java.lang.NullPointerException
12-31 01:48:33.439: E/AndroidRuntime(1357): at com.mamoon.jobforitinjordan.MainActivity$asy.onPostExecute(MainActivity.java:114)
12-31 01:48:33.439: E/AndroidRuntime(1357): at com.mamoon.jobforitinjordan.MainActivity$asy.onPostExecute(MainActivity.java:1)
12-31 01:48:33.439: E/AndroidRuntime(1357): at android.os.AsyncTask.finish(AsyncTask.java:631)
12-31 01:48:33.439: E/AndroidRuntime(1357): at android.os.AsyncTask.access$600(AsyncTask.java:177)
12-31 01:48:33.439: E/AndroidRuntime(1357): at android.os.AsyncTask$InternalHandler.handleMessage(AsyncTask.java:644)
12-31 01:48:33.439: E/AndroidRuntime(1357): at android.os.Handler.dispatchMessage(Handler.java:99)
12-31 01:48:33.439: E/AndroidRuntime(1357): at android.os.Looper.loop(Looper.java:137)
12-31 01:48:33.439: E/AndroidRuntime(1357): at android.app.ActivityThread.main(ActivityThread.java:5041)
12-31 01:48:33.439: E/AndroidRuntime(1357): at java.lang.reflect.Method.invokeNative(Native Method)
12-31 01:48:33.439: E/AndroidRuntime(1357): at java.lang.reflect.Method.invoke(Method.java:511)
12-31 01:48:33.439: E/AndroidRuntime(1357): at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:793)
12-31 01:48:33.439: E/AndroidRuntime(1357): at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:560)
12-31 01:48:33.439: E/AndroidRuntime(1357): at dalvik.system.NativeStart.main(Native Method)
答案 0 :(得分:3)
listDataHeader
是null
因为您从未初始化它。从
List<String> listDataHeader;
到
List<String> listDataHeader= new ArrayList<String>();