我有一个小问题:如果我想从我的数据库中检索所有课程列表作为列表视图,一切看起来都很好,但是如果我想单击其中一门课程来显示该课程的信息,它不起作用,这个问题是显示在 logcat 中:
09-21 01:39:58.915: E/AndroidRuntime(6968): FATAL EXCEPTION: main
09-21 01:39:58.915: E/AndroidRuntime(6968): java.lang.NullPointerException
09-21 01:39:58.915: E/AndroidRuntime(6968): at com.ksu.sms.ViewCourseStudent$GetCourseDetails.onPostExecute(ViewCourseStudent.java:142)
09-21 01:39:58.915: E/AndroidRuntime(6968): at com.ksu.sms.ViewCourseStudent$GetCourseDetails.onPostExecute(ViewCourseStudent.java:1)
09-21 01:39:58.915: E/AndroidRuntime(6968): at android.os.AsyncTask.finish(AsyncTask.java:602)
09-21 01:39:58.915: E/AndroidRuntime(6968): at android.os.AsyncTask.access$600(AsyncTask.java:156)
09-21 01:39:58.915: E/AndroidRuntime(6968): at android.os.AsyncTask$InternalHandler.handleMessage(AsyncTask.java:615)
09-21 01:39:58.915: E/AndroidRuntime(6968): at android.os.Handler.dispatchMessage(Handler.java:99)
09-21 01:39:58.915: E/AndroidRuntime(6968): at android.os.Looper.loop(Looper.java:137)
09-21 01:39:58.915: E/AndroidRuntime(6968): at android.app.ActivityThread.main(ActivityThread.java:4340)
09-21 01:39:58.915: E/AndroidRuntime(6968): at java.lang.reflect.Method.invokeNative(Native Method)
09-21 01:39:58.915: E/AndroidRuntime(6968): at java.lang.reflect.Method.invoke(Method.java:511)
09-21 01:39:58.915: E/AndroidRuntime(6968): at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:784)
09-21 01:39:58.915: E/AndroidRuntime(6968): at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:551)
09-21 01:39:58.915: E/AndroidRuntime(6968): at dalvik.system.NativeStart.main(Native Method)
ViewCourseStudent.java 包 com.ksu.sms;
import android.app.Activity;
import java.util.ArrayList;
import java.util.List;
import org.apache.http.NameValuePair;
import org.apache.http.message.BasicNameValuePair;
import org.json.JSONArray;
import org.json.JSONException;
import org.json.JSONObject;
import android.app.ProgressDialog;
import android.content.Intent;
import android.os.AsyncTask;
import android.os.Bundle;
import android.util.Log;
import android.widget.TextView;
public class ViewCourseStudent extends Activity {
TextView Name; TextView Description;
TextView OfficeHours;
TextView CreditHours;
TextView MaxAbsenceDays;
TextView ExamsDates ;
JSONObject course;
String CourseID ;
// Progress Dialog
private ProgressDialog pDialog;
// JSON parser class
JSONParser jsonParser = new JSONParser();
// single course url
private static final String url_course_detials = "http://10.0.2.2/SmsPhp/view_course.php";
//JSON Node names
private static final String TAG_SUCCESS = "success";
private static final String TAG_CourseID = "CourseID";
private static final String TAG_course = "course";
private static final String TAG_Name = "Name";
private static final String TAG_OfficeHours = "OfficeHours";
private static final String TAG_CreditHours = "CreditHours";
private static final String TAG_Description = "Description";
private static final String TAG_MaxAbsenceDays = "MaxAbsenceDays";
private static final String TAG_ExamsDates = "ExamsDates";
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.view_course);
Intent i = getIntent();
CourseID = i.getStringExtra(TAG_CourseID);
// Getting complete course details in background thread
new GetCourseDetails().execute();
}
/**
* Background Async Task to Get complete course details
* */
class GetCourseDetails extends AsyncTask<String, String, String> {
/**
* Before starting background thread Show Progress Dialog
* */
@Override
protected void onPreExecute() {
super.onPreExecute();
pDialog = new ProgressDialog(ViewCourseStudent.this);
pDialog.setMessage("Loading course details. Please wait...");
pDialog.setIndeterminate(false);
pDialog.setCancelable(true);
pDialog.show();
}
@Override
protected String doInBackground(String... arg0) {
// updating UI from Background Thread
// Check for success tag
int success;
try {
// Building Parameters
List<NameValuePair> params = new ArrayList<NameValuePair>();
params.add(new BasicNameValuePair("CourseID", CourseID));
// getting course details by making HTTP request
// Note that product details url will use GET request
JSONObject json = jsonParser.makeHttpRequest(
url_course_detials, "GET", params);
// check your log for json response
Log.d("Single course Details", json.toString());
// json success tag
success = json.getInt(TAG_SUCCESS);
if (success == 1) {
// successfully received product details
JSONArray courseObj = json
.getJSONArray(TAG_course); // JSON Array
// get first course object from JSON Array
course = courseObj.getJSONObject(0);
// course with this course_id found
// textview Text
/*TextView Name; TextView Description;
TextView OfficeHours;
TextView CreditHours;
TextView MaxAbsenceDays;
TextView ExamsDates ;*/
}else{
// course with course id not found
}
} catch (JSONException e) {
e.printStackTrace();
}
return null;
}
/**
* After completing background task Dismiss the progress dialog
* **/
protected void onPostExecute(String file_url) {
// dismiss the dialog once got all details
pDialog.dismiss();
try
{
Name = (TextView) findViewById(R.id.C_Name);
Description = (TextView) findViewById(R.id.C_Des);
CreditHours = (TextView) findViewById(R.id.C_Hours);
OfficeHours=(TextView) findViewById(R.id.C_Ohour);
MaxAbsenceDays=(TextView) findViewById(R.id.C_absence);
ExamsDates=(TextView) findViewById(R.id.Add_C_Exam);
// display product data in EditText
Name.setText( course.getString(TAG_Name));
Description.setText( course.getString(TAG_Description));
OfficeHours.setText( course.getString(TAG_OfficeHours));
MaxAbsenceDays.setText( course.getString(TAG_MaxAbsenceDays));
ExamsDates.setText( course.getString(TAG_ExamsDates));
CreditHours.setText( course.getString(TAG_CreditHours));
}
catch (JSONException e) {
e.printStackTrace();
}
}
}
}
查看所有课程学生 .java
package com.ksu.sms;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import org.apache.http.NameValuePair;
import org.json.JSONArray;
import org.json.JSONException;
import org.json.JSONObject;
import android.app.ListActivity;
import android.app.ProgressDialog;
import android.content.Intent;
import android.os.AsyncTask;
import android.os.Bundle;
import android.util.Log;
import android.view.View;
import android.widget.AdapterView;
import android.widget.AdapterView.OnItemClickListener;
import android.widget.ListAdapter;
import android.widget.ListView;
import android.widget.SimpleAdapter;
import android.widget.TextView;
import android.widget.Toast;
public class ViewALLCourseStudent extends ListActivity {
// Progress Dialog
private ProgressDialog pDialog;
// Creating JSON Parser object
JSONParser jParser = new JSONParser(); //class
boolean x =true;
ArrayList<HashMap<String, String>> coursesList;
//url to get all products list
private static String url_all_course = "http://10.0.2.2/SmsPhp/view_all_course.php";
// JSON Node names
private static final String TAG_SUCCESS = "success";
private static final String TAG_course = "course";
private static final String TAG_CourseID = "CourseID";
private static final String TAG_Name = "Name";
// course JSONArray
JSONArray courses = null;
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.view_all_course_student);
coursesList = new ArrayList<HashMap<String, String>>();
// Loading courses in Background Thread
new LoadAllCourses().execute();
// updating listview
// Get list view
ListView lv = getListView();
// on seleting single course
// launching Edit course Screen
lv.setOnItemClickListener(new OnItemClickListener() {
public void onItemClick(AdapterView<?> parent, View view,
int position, long id) //one of the list
{
// getting values from selected ListItem
String CourseID = ((TextView) view.findViewById(R.id.CourseID)).getText()
.toString();
// Starting new intent
Intent ViewCourseStudent = new Intent(getApplicationContext(),
ViewCourseStudent.class);
// sending Course ID to next activity
ViewCourseStudent.putExtra(TAG_CourseID, CourseID);
// starting new activity and expecting some response back
startActivityForResult(ViewCourseStudent, 100);
}
});
}
// Response from view course Activity
@Override
protected void onActivityResult(int requestCode, int resultCode, Intent data)
{
super.onActivityResult(requestCode, resultCode, data);
// if result code 100
if (resultCode == 100) {
// if result code 100 is received
// means user view course
// reload this screen again
Intent intent = getIntent();
finish();
startActivity(intent);
}
}
/**
* Background Async Task to Load all course by making HTTP Request
* */
class LoadAllCourses extends AsyncTask<String, String, String>
{
/**
* Before starting background thread Show Progress Dialog
* */
@Override
protected void onPreExecute() {
super.onPreExecute();
pDialog = new ProgressDialog(ViewALLCourseStudent.this);
pDialog.setMessage("Loading Courses. Please wait...");
pDialog.setIndeterminate(false);
pDialog.setCancelable(false);
pDialog.show();
}
/**
* getting All products from u r l
* */
@Override
protected String doInBackground(String... args) {
// Building Parameters
List<NameValuePair> params = new ArrayList<NameValuePair>();
// getting JSON string from URL
JSONObject json = jParser.makeHttpRequest(url_all_course, "GET", params);
// Check your log cat for JSON response
Log.d("All courses: ", json.toString());
try {
// Checking for SUCCESS TAG
int success = json.getInt(TAG_SUCCESS);
if (success == 1) {
// course found
// Getting Array of course
courses = json.getJSONArray(TAG_course);
// looping through All courses
for (int i = 0; i < courses.length(); i++)//course JSONArray
{
JSONObject c = courses.getJSONObject(i); // read first
// Storing each json item in variable
String CourseID = c.getString(TAG_CourseID);
String Name = c.getString(TAG_Name);
// creating new HashMap
HashMap<String, String> map = new HashMap<String, String>();
// adding each child node to HashMap key => value
map.put(TAG_CourseID, CourseID);
map.put(TAG_Name, Name);
// adding HashList to ArrayList
coursesList.add(map);
}
} else {
x=false;
}
} catch (JSONException e) {
e.printStackTrace();
}
return null;
}
/**
* After completing background task Dismiss the progress dialog
* **/
protected void onPostExecute(String file_url) {
// dismiss the dialog after getting all products
pDialog.dismiss();
if (x==false)
Toast.makeText(getBaseContext(),"no course" ,Toast.LENGTH_LONG).show();
ListAdapter adapter = new SimpleAdapter(
ViewALLCourseStudent.this, coursesList,
R.layout.list_item, new String[] { TAG_CourseID,
TAG_Name},
new int[] { R.id.CourseID, R.id.Name });
setListAdapter(adapter);
// Updating parsed JSON data into ListView
}
}
}