我正在使用异步任务将json检索到baseadapter并最终将其显示到listview中。我无法做到这一点,我得到错误,我的应用程序崩溃。 这是我的代码: MainActivity.java
private String jsonString, url, username;
JSONArray JArray;
ImageView profileimg;
ListView lv;
BaseAdapterTest adapter;
private JSONArray questions;
JSONObject temp_obj;
ArrayList<String> test = new ArrayList<String>();
public static String Tag_profile_pic = "profile_pic";
public static String Tag_username = "username";
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.nnj);
lv = (ListView) findViewById(R.id.listview);
new GetJson().execute();
}
;
private class GetJson extends AsyncTask<Void, Void, Void> {
@Override
protected void onPreExecute() {
super.onPreExecute();
}
@Override
protected Void doInBackground(Void... params) {
ServiceHandler http = new ServiceHandler();
jsonString = http.makeServiceCall(url, ServiceHandler.GET,
null);
if (jsonString != null) {
try {
// Getting JSON Array node
JArray = new JSONArray(jsonString);
// looping through All Contacts
for (int i = 0; i < JArray.length(); i++) {
temp_obj = JArray.getJSONObject(i);
JSONObject currentObject = new JSONObject();
currentObject.put("profile_pic", temp_obj.getString(Tag_profile_pic)
.toString());
currentObject.put("username", temp_obj.getString(Tag_username)
.toString());
JArray.put(currentObject);
}
} catch (JSONException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
} else {
Log.d("TAG", "Empty");
}
return null;
}
@Override
protected void onPostExecute(Void result) {
// TODO Auto-generated method stub
super.onPostExecute(result);
Toast.makeText(getApplicationContext(), JArray + "", Toast.LENGTH_LONG).show();
adapter = new BaseAdapterTest(MainActivity.this, JArray) {
@Override
public int getCount() {
return 0;
}
@Override
public Object getItem(int position) {
return null;
}
@Override
public long getItemId(int position) {
return 0;
}
@Override
public View getView(int position, View convertView, ViewGroup parent) {
return null;
}
};
// Set the adapter to the ListView
lv.setAdapter(adapter);
}
}
}
和BaseAdapter.java的代码
public class BaseAdapterTest extends android.widget.BaseAdapter {
// Declare Variables
Context context;
LayoutInflater inflater;
Array JArray;
public BaseAdapterTest(MainActivity mainActivity, JSONArray JArray) {
}
@Override
public int getCount() {
return getCount();
}
@Override
public Object getItem(int position) {
return null;
}
@Override
public long getItemId(int position) {
return 0;
}
public View getView(final int position, View convertView, ViewGroup parent) {
// Declare Variables
TextView username;
ImageView profilepic;
inflater = (LayoutInflater) context
.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
View itemView = inflater.inflate(R.layout.listview, parent, false);
// Locate the TextViews in listview_item.xml
username = (TextView) itemView.findViewById(R.id.questionfeed_customelistview_txt_username);
// Locate the ImageView in listview_item.xml
profilepic = (ImageView) itemView.findViewById(R.id.questionfeed_customelistview_imgv_follow);
itemView.setOnClickListener(new OnClickListener() {
@Override
public void onClick(View arg0) {
// Get the position
Intent intent = new Intent(context, MainActivity.class);
// Pass all data rank
intent.putExtra("username", (MainActivity.Tag_username));
// Pass all data flag
intent.putExtra("profilepic",(MainActivity.Tag_profile_pic));
// Start SingleItemView Class
context.startActivity(intent);
}
});
return itemView;
}
}
这是我的Logcat
04-02 15:49:31.871 2935-2935/com.cheerz.json E/AndroidRuntime﹕ FATAL EXCEPTION: main
Process: com.cheerz.json, PID: 2935
java.lang.NullPointerException: Attempt to invoke virtual method 'void android.widget.ListView.setAdapter(android.widget.ListAdapter)' on a null object reference
at com.cheerz.json.MainActivity$GetJson.onPostExecute(MainActivity.java:115)
at com.cheerz.json.MainActivity$GetJson.onPostExecute(MainActivity.java:47)
at android.os.AsyncTask.finish(AsyncTask.java:632)
at android.os.AsyncTask.access$600(AsyncTask.java:177)
at android.os.AsyncTask$InternalHandler.handleMessage(AsyncTask.java:645)
at android.os.Handler.dispatchMessage(Handler.java:102)
at android.os.Looper.loop(Looper.java:135)
at android.app.ActivityThread.main(ActivityThread.java:5312)
at java.lang.reflect.Method.invoke(Native Method)
at java.lang.reflect.Method.invoke(Method.java:372)
at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:901)
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:696)
答案 0 :(得分:-1)
替换
JArray = new JSONArray(jsonString);
与
JSONObject jsonobject = new JSONObject(jsonString);
JArray = ((JSONObject) jsonobject).getJSONArray("key_name_of_your_array");