0

我有一个小问题:如果我想从我的数据库中检索所有课程列表作为列表视图,一切看起来都很好,但是如果我想单击其中一门课程来显示该课程的信息,它不起作用,这个问题是显示在 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 

        } 


    }
}
4

2 回答 2

0

您收到此错误是因为您尝试从 doInBackground 方法访问 View 对象,例如 Name.setText(course.getString(TAG_Name)); 您必须将所有 UI 更新移至 onPostExecute 方法。

你能测试一下这个实现吗:

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 ;

        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> {

        private JSONObject  course;

        /**
         * 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);




                    }else{
                        // course with course id not found
                        course = null;
                    }
                } 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();

        if(course != null){
             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));
        }
    }
}
} 
于 2012-09-25T17:31:13.583 回答
0

在您的班级ViewCourseStudent中,您有一个AsyncTask正在修改ViewsdoInBackground()方法的内容。

这导致CalledFromWrongThreadException. 该异常提供的消息几乎可以告诉您一切。

只有创建视图层次结构的原始线程才能接触其视图。

这意味着所有与 UI 相关的调用都不能在主 (UI) 线程之外完成。

如果您阅读AsyncTask参考资料,您会发现它doInBackground()在单独的线程上运行,从而导致此异常。

为了解决这个问题,您应该将所有与 UI 相关的代码doInBackground()onPostExecute().

与 UI 相关的代码是指以下部分:

Name = (TextView) findViewById(R.id.C_Name);
Description = (TextView) findViewById(R.id.C_Des);
...
ExamsDates.setText( course.getString(TAG_ExamsDates));
CreditHours.setText( course.getString(TAG_CreditHours));
于 2012-09-25T17:31:22.910 回答