我是android新手我已经应用了所有解决方案。但 问题没有解决。 我为asyncTask,adapter,Model制作了单独的类 activity oncreate调用asyncTask类。在asyncTask适配器中调用 和listview被调用。 善意的帮助。提前致谢
我的活动是
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_course_outlines);
CourseOutlinesTask task = new CourseOutlinesTask(CourseOutlinesActivity.this); task.execute("http://mantis.vu.edu.pk/bridging_the_gap/public/viewCourseOutlines");
asyncTASK CLASS是
public class CourseOutlinesTask extends AsyncTask<String, String, String> {
ProgressDialog dialog;
Activity context;
private ArrayList<CourseModel> postList = new ArrayList<CourseModel>();
private ListView listView;
TrainerCourseAdapter adapter;
public CourseOutlinesTask(Activity context) {
context = this.context;
}
@Override
protected void onPreExecute() {
}
@Override
protected String doInBackground(String... params) {
HttpURLConnection connection = null;
BufferedReader reader = null;
try {
URL url = new URL(params[0]);
connection = (HttpURLConnection) url.openConnection();
connection.connect();
InputStream stream = connection.getInputStream();
reader = new BufferedReader(new InputStreamReader(stream));
StringBuffer buffer = new StringBuffer();
String line = "";
while ((line = reader.readLine()) != null) {
buffer.append(line);
}
return buffer.toString();
} catch (MalformedURLException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
} finally {
if (connection != null)
connection.disconnect();
try {
if (reader != null)
reader.close();
} catch (IOException e) {
e.printStackTrace();
}
}
return null;
}
@Override
protected void onPostExecute(String result) {
super.onPostExecute(result);
//close process dialog
if (this.dialog != null) {
this.dialog.dismiss();
}
//parse json
try {
JSONObject jsonParse = new JSONObject(result);
JSONArray query = jsonParse.getJSONArray("courses");
for (int i = 0; i < query.length(); i++) {
try {
JSONObject jsonParser = query.getJSONObject(i);
CourseModel post = new CourseModel();
post.setId(jsonParser.getInt("id"));
post.setTitle(jsonParser.getString("title"));
post.setStatus(jsonParser.getString("status"));
post.setDescription(jsonParser.getString("description"));
System.out.println(post.getStatus()+"asdadasdad");
System.out.println(post);
postList.add(post);
TrainerCourseAdapter adapter = new TrainerCourseAdapter(context,postList);
System.out.println("ttttttttttttttttttttt");
listView = (ListView) context.findViewById(R.id.course_listView);
listView.setAdapter(new TrainerCourseAdapter(context,postList));
}catch (Exception e) {
System.out.println(e);
}
// Parsing json
post.setDescription(obj.getString("description"));
}
} else {
MyAppUtil.getToast(getApplicationContext(), message);
}*/
}
} catch (JSONException e) {
e.printStackTrace();
}
}
}
我的适配器类是
public class TrainerCourseAdapter extends BaseAdapter {
private List list;
private Context context;
private static LayoutInflater inflater = null;
String [] cName;
String [] cDetail;
String [] created;
String [] cStatus;
ArrayList<CourseModel> itemList;
Context mcontext;
public TrainerCourseAdapter(Context context,List list) {
System.out.println("aaaaaaaaaaaaaaaaaaaaaaaaaaaaa");
mcontext = context;
itemList = (ArrayList<CourseModel>) this.list;
}
@Override
public int getCount() {
return itemList.size();
}
@Override
public Object getItem(int i) {
return i;
}
@Override
public long getItemId(int i) {
return i;
}
public void setItemList(ArrayList<CourseModel> itemList) {
this.itemList = itemList;
}
public class Holder
{
TextView c_name;
TextView c_detail;
TextView c_date ;
Button c_status;
}
@Override
public View getView(final int i, View rowView, ViewGroup viewGroup) {
Holder holder = new Holder();
rowView = inflater.inflate(R.layout.row_courses_list, null);
holder.c_name = (TextView) rowView.findViewById(R.id.txt_courseName);
holder.c_detail = (TextView) rowView.findViewById(R.id.txt_courseDetail);
holder.c_date = (TextView) rowView.findViewById(R.id.txt_courseDate);
holder.c_status = (Button) rowView.findViewById(R.id.btn_courseStatus);
final CourseModel data = itemList.get(i);
holder.c_name.setText(data.getTitle());
holder.c_detail.setText(data.getDescription());
holder.c_status.setText(data.getStatus());
holder.c_date.setText(data.getId());
rowView.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view) {
Toast.makeText(context, "You Clicked "+ cName[i], Toast.LENGTH_LONG).show();
}
});
return rowView;
}
}
模型是
public class CourseModel {
private int id;
private String title;
private String description;
private String status;
// private int totalCount;
// private int limit;
// private String offset;
public CourseModel(){
}
/*Getter for Course Model*/
public int getId() {
return id;
}
public String getTitle() {
return title;
}
public String getDescription() {
return description;
}
public String getStatus() {
return status;
}
/*Endo of Model Getter*/
/*Setter for Model*/
public void setId(int id) {
this.id = id;
}
public void setTitle(String title) {
this.title = title;
}
public void setDescription(String description) {
this.description = description;
}
public void setStatus(String status) {
this.status = status;
}
/*End for Model Setters*/
}
当我运行应用列表时为空并且错误是
10-05 19:22:04.352 10503-10537/vu.bc110201891.btg I/OpenGLRenderer: Initialized EGL, version 1.4
10-05 19:22:04.478 10503-10537/vu.bc110201891.btg D/OpenGLRenderer: Enabling debug mode 0
10-05 19:22:04.541 10503-10537/vu.bc110201891.btg W/EGL_emulation: eglSurfaceAttrib not implemented
10-05 19:22:04.541 10503-10537/vu.bc110201891.btg W/OpenGLRenderer: Failed to set EGL_SWAP_BEHAVIOR on surface 0xeb9943c0, error=EGL_SUCCESS
10-05 19:22:04.718 10503-10503/vu.bc110201891.btg I/Choreographer: Skipped 30 frames! The application may be doing too much work on its main thread.
10-05 19:22:05.564 10503-10503/vu.bc110201891.btg I/System.out: Rejectasdadasdad
10-05 19:22:05.564 10503-10503/vu.bc110201891.btg I/System.out: vu.bc110201891.btg.Models.CourseModel@21937920
10-05 19:22:05.565 10503-10503/vu.bc110201891.btg I/System.out: aaaaaaaaaaaaaaaaaaaaaaaaaaaaa
10-05 19:22:05.565 10503-10503/vu.bc110201891.btg I/System.out: ttttttttttttttttttttt
10-05 19:22:05.565 10503-10503/vu.bc110201891.btg I/System.out: java.lang.NullPointerException: Attempt to invoke virtual method 'android.view.View android.app.Activity.findViewById(int)' on a null object reference
答案 0 :(得分:1)
下面:
context = this.context;
context
对象为null
。在构造函数中将其更改为:
this.context = context;