致命异常:AsyncTask#1 - doInBackground()

时间:2015-06-07 13:17:40

标签: java android web-services android-fragments android-asynctask

我有一个异常,我无法通过调试器日志解决。 我是eclipse的新手,所以我希望你能帮忙,这将是完美的。

所以我得到了2个webservices,它们在一个片段中调用,以填充listview和该片段中的微调器。

第一个webservice 被称为oncreateview(),并填充微调器成功

第二个webservice 由按钮点击事件调用,并将获得另一个响应(此响应有两个帖子变量)。

Json响应是正确的,但它在行( ScheduleList.add(Sche); )中得到异常,但奇怪的是在其他类(不是这个)上。

我有两个模型类,“Schedule”和“Teacher”,可以与每个Web服务进行交互。

你认为这与同一片段上的两个异步函数有关吗?

   private class GetTeachersInfo extends AsyncTask<String, Void, Void> {

        @Override
        protected void onPreExecute() {
            super.onPreExecute();
            pDialog = new ProgressDialog(getActivity());
            pDialog.setMessage("A processar horários..");
            pDialog.setCancelable(false);
            pDialog.show();

        }

        @Override
        protected Void doInBackground(String... arg) {

            String professor = arg[0];
            String dia_Semana = arg[1];

            List<NameValuePair> params = new ArrayList<NameValuePair>();
            params.add(new BasicNameValuePair("professor", professor));
            params.add(new BasicNameValuePair("Dia_Semana", dia_Semana));

            ServiceHandler jsonParser = new ServiceHandler();
            String json = jsonParser.makeServiceCall(URL_TEACHERSINFO, ServiceHandler.POST, params);

            Log.e("Response: ", "> " + json);

            if (json != null) {
                try {
                    JSONObject jsonObj = new JSONObject(json);
                    if (jsonObj != null) {
                        JSONArray schedules = jsonObj
                                .getJSONArray("horarios");                        
                        for (int i = 0; i < schedules.length(); i++) {
                            JSONObject ScheObj = (JSONObject) schedules.get(i);
                            Schedule Sche = new Schedule(ScheObj.getInt("Cod_Horario"),ScheObj.getString("T_Entrada"),
                                    ScheObj.getString("T_Saida"),ScheObj.getString("Dia_Semana"),ScheObj.getString("Professor"),ScheObj.getString("Disciplina"),ScheObj.getString("Sala"),ScheObj.getString("Turma"));
                            ScheduleList.add(Sche);
                        }
                    }

                } catch (JSONException e) {
                    e.printStackTrace();
                }

            } else {
                Log.e("JSON Data", "Didn't receive any data from server!");
            }

            return null;
        }

编辑:

错误日志:

06-07 08:53:59.009: E/Response:(1816): > {"horarios":[{"Cod_Horario":5,"T_Entrada":"08:20","T_Saida":"09:05","Dia_Semana":"4\u00aa Feira","Professor":"Ligia Sofia da Costa Nunes","Disciplina":"Ed.Fisic","Sala":"GIN","Turma":"10-F"},{"Cod_Horario":6,"T_Entrada":"09:05","T_Saida":"09:50","Dia_Semana":"4\u00aa Feira","Professor":"Ligia Sofia da Costa Nunes","Disciplina":"Ed.Fisic","Sala":"GIN","Turma":"10-F"},{"Cod_Horario":9,"T_Entrada":"11:45","T_Saida":"12:30","Dia_Semana":"4\u00aa Feira","Professor":"Ligia Sofia da Costa Nunes","Disciplina":"Ed.Fisic","Sala":"GIN","Turma":"11-G"},{"Cod_Horario":10,"T_Entrada":"12:30","T_Saida":"13:15","Dia_Semana":"4\u00aa Feira","Professor":"Ligia Sofia da Costa Nunes","Disciplina":"Ed.Fisic","Sala":"GIN","Turma":"11-G"}],"success":1}
06-07 08:57:05.002: W/dalvikvm(1816): threadid=11: thread exiting with uncaught exception (group=0xb2caab20)
06-07 08:57:10.922: E/AndroidRuntime(1816): FATAL EXCEPTION: AsyncTask #1
06-07 08:57:10.922: E/AndroidRuntime(1816): Process: info.androidhive.tabsswipe, PID: 1816
06-07 08:57:10.922: E/AndroidRuntime(1816): java.lang.RuntimeException: An error occured while executing doInBackground()
06-07 08:57:10.922: E/AndroidRuntime(1816):     at android.os.AsyncTask$3.done(AsyncTask.java:300)
06-07 08:57:10.922: E/AndroidRuntime(1816):     at java.util.concurrent.FutureTask.finishCompletion(FutureTask.java:355)
06-07 08:57:10.922: E/AndroidRuntime(1816):     at java.util.concurrent.FutureTask.setException(FutureTask.java:222)
06-07 08:57:10.922: E/AndroidRuntime(1816):     at java.util.concurrent.FutureTask.run(FutureTask.java:242)
06-07 08:57:10.922: E/AndroidRuntime(1816):     at android.os.AsyncTask$SerialExecutor$1.run(AsyncTask.java:231)
06-07 08:57:10.922: E/AndroidRuntime(1816):     at java.util.concurrent.ThreadPoolExecutor.runWorker(ThreadPoolExecutor.java:1112)
06-07 08:57:10.922: E/AndroidRuntime(1816):     at java.util.concurrent.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:587)
06-07 08:57:10.922: E/AndroidRuntime(1816):     at java.lang.Thread.run(Thread.java:841)
06-07 08:57:10.922: E/AndroidRuntime(1816): Caused by: java.lang.NullPointerException
06-07 08:57:10.922: E/AndroidRuntime(1816):     at info.androidhive.tabsswipe.ProfessoresFragment$GetTeachersInfo.doInBackground(ProfessoresFragment.java:210)
06-07 08:57:10.922: E/AndroidRuntime(1816):     at info.androidhive.tabsswipe.ProfessoresFragment$GetTeachersInfo.doInBackground(ProfessoresFragment.java:1)
06-07 08:57:10.922: E/AndroidRuntime(1816):     at android.os.AsyncTask$2.call(AsyncTask.java:288)
06-07 08:57:10.922: E/AndroidRuntime(1816):     at java.util.concurrent.FutureTask.run(FutureTask.java:237)
06-07 08:57:10.922: E/AndroidRuntime(1816):     ... 4 more
06-07 08:57:11.642: E/WindowManager(1816): android.view.WindowLeaked: Activity info.androidhive.tabsswipe.MainActivity has leaked window com.android.internal.policy.impl.PhoneWindow$DecorView{b3126950 V.E..... R......D 0,0-456,144} that was originally added here
06-07 08:57:11.642: E/WindowManager(1816):  at android.view.ViewRootImpl.<init>(ViewRootImpl.java:346)
06-07 08:57:11.642: E/WindowManager(1816):  at android.view.WindowManagerGlobal.addView(WindowManagerGlobal.java:248)
06-07 08:57:11.642: E/WindowManager(1816):  at android.view.WindowManagerImpl.addView(WindowManagerImpl.java:69)
06-07 08:57:11.642: E/WindowManager(1816):  at android.app.Dialog.show(Dialog.java:286)
06-07 08:57:11.642: E/WindowManager(1816):  at info.androidhive.tabsswipe.ProfessoresFragment$GetTeachersInfo.onPreExecute(ProfessoresFragment.java:181)
06-07 08:57:11.642: E/WindowManager(1816):  at android.os.AsyncTask.executeOnExecutor(AsyncTask.java:587)
06-07 08:57:11.642: E/WindowManager(1816):  at android.os.AsyncTask.execute(AsyncTask.java:535)
06-07 08:57:11.642: E/WindowManager(1816):  at info.androidhive.tabsswipe.ProfessoresFragment$1.onClick(ProfessoresFragment.java:74)
06-07 08:57:11.642: E/WindowManager(1816):  at android.view.View.performClick(View.java:4438)
06-07 08:57:11.642: E/WindowManager(1816):  at android.view.View$PerformClick.run(View.java:18422)
06-07 08:57:11.642: E/WindowManager(1816):  at android.os.Handler.handleCallback(Handler.java:733)
06-07 08:57:11.642: E/WindowManager(1816):  at android.os.Handler.dispatchMessage(Handler.java:95)
06-07 08:57:11.642: E/WindowManager(1816):  at android.os.Looper.loop(Looper.java:136)
06-07 08:57:11.642: E/WindowManager(1816):  at android.app.ActivityThread.main(ActivityThread.java:5001)
06-07 08:57:11.642: E/WindowManager(1816):  at java.lang.reflect.Method.invokeNative(Native Method)
06-07 08:57:11.642: E/WindowManager(1816):  at java.lang.reflect.Method.invoke(Method.java:515)
06-07 08:57:11.642: E/WindowManager(1816):  at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:785)
06-07 08:57:11.642: E/WindowManager(1816):  at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:601)
06-07 08:57:11.642: E/WindowManager(1816):  at dalvik.system.NativeStart.main(Native Method)

全班代码:

public class TurmasFragment extends Fragment {

    private ListView listviewHorarios;
    private Spinner spinnerTurmas;
    private Spinner spinnerDia_Semana;
    private Button  buttonSearch;

    // array list for spinner adapter
    private ArrayList<Class> ClassList;
    private ArrayList<Schedule> ScheduleList;
    ProgressDialog pDialog;

    // API urls
    // Url to get all teachers
    private String URL_CLASSES = "http://10.0.2.2/android/GetAllClassesJson.php";
    private String URL_CLASSESINFO = "http://10.0.2.2/android/GetClassJson.php";


    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container,
            Bundle savedInstanceState) {

        View rootView = inflater.inflate(R.layout.fragment_turmas, container, false);

        listviewHorarios = (ListView) rootView.findViewById(R.id.list);
        spinnerTurmas = (Spinner) rootView.findViewById(R.id.spinner);
        spinnerDia_Semana = (Spinner) rootView.findViewById(R.id.spinner2);
        buttonSearch = (Button) rootView.findViewById(R.id.button);

        buttonSearch.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {  
                     // new category name

                String turma =  spinnerTurmas.getSelectedItem().toString();
                String dia_Semana = spinnerDia_Semana.getSelectedItem().toString();

                     //String newCategory = txtCategory.getText().toString();

                     // Call a sync task to create new category
                     new GetClassesInfo().execute(turma, dia_Semana);
            }
        });

        ClassList = new ArrayList<Class>();

        //spinnerProfessores.setOnItemSelectedListener(this);

        new GetClasses().execute();

        return rootView;
    }

    private void populateSpinner() {
        List<String> lables = new ArrayList<String>();


        for (int i = 0; i < ClassList.size(); i++) {
            lables.add(ClassList.get(i).getTurma());
        }

        // Creating adapter for spinner
        ArrayAdapter<String> spinnerAdapter = new ArrayAdapter<String>(getActivity(),
        android.R.layout.simple_spinner_item, lables);

        // Drop down layout style - list view with radio button
        spinnerAdapter
                .setDropDownViewResource(android.R.layout.simple_spinner_dropdown_item);

        // attaching data adapter to spinner
        spinnerTurmas.setAdapter(spinnerAdapter);
    }

    private void populateListView() {
        ListScheduleAdapter adapter = new ListScheduleAdapter(getActivity(), ScheduleList);
        listviewHorarios.setAdapter(adapter);
    }

    private class GetClasses extends AsyncTask<Void, Void, Void> {

        @Override
        protected void onPreExecute() {
            super.onPreExecute();
            pDialog = new ProgressDialog(getActivity());
            pDialog.setMessage("A processar turmas..");
            pDialog.setCancelable(false);
            pDialog.show();

        }

        @Override
        protected Void doInBackground(Void... arg0) {
            ServiceHandler jsonParser = new ServiceHandler();
            String json = jsonParser.makeServiceCall(URL_CLASSES, ServiceHandler.GET);

            Log.e("Response: ", "> " + json);

            if (json != null) {
                try {
                    JSONObject jsonObj = new JSONObject(json);
                    if (jsonObj != null) {
                        JSONArray classes = jsonObj
                                .getJSONArray("turmas");                        

                        for (int i = 0; i < classes.length(); i++) {
                            JSONObject ClaObj = (JSONObject) classes.get(i);
                            Class cla = new Class(ClaObj.getInt("Cod_Turma"),
                                    ClaObj.getString("Turma"));
                            ClassList.add(cla);
                        }
                    }

                } catch (JSONException e) {
                    e.printStackTrace();
                }

            } else {
                Log.e("JSON Data", "Didn't receive any data from server!");
            }

            return null;
        }

        @Override
        protected void onPostExecute(Void result) {
            super.onPostExecute(result);
            if (pDialog.isShowing())
                pDialog.dismiss();
            populateSpinner();
        } 
    }

     private class GetClassesInfo extends AsyncTask<String, Void, Void> {

            @Override
            protected void onPreExecute() {
                super.onPreExecute();
                pDialog = new ProgressDialog(getActivity());
                pDialog.setMessage("A processar horários..");
                pDialog.setCancelable(false);
                pDialog.show();

            }

            @Override
            protected Void doInBackground(String... arg) {

                String turma = arg[0];
                String dia_Semana = arg[1];

                List<NameValuePair> params = new ArrayList<NameValuePair>();
                params.add(new BasicNameValuePair("Turma", turma));
                params.add(new BasicNameValuePair("Dia_Semana", dia_Semana));

                ServiceHandler jsonParser = new ServiceHandler();
                String json = jsonParser.makeServiceCall(URL_CLASSESINFO, ServiceHandler.POST, params);

                Log.e("Response: ", "> " + json);

                if (json != null) {
                    try {
                        JSONObject jsonObj = new JSONObject(json);
                        if (jsonObj != null) {
                            JSONArray schedules = jsonObj
                                    .getJSONArray("horarios");                        

                            for (int i = 0; i < schedules.length(); i++) {
                                JSONObject ScheObj = (JSONObject) schedules.get(i);
                                Schedule Sche = new Schedule(ScheObj.getInt("Cod_Horario"),ScheObj.getString("T_Entrada"),
                                        ScheObj.getString("T_Saida"),ScheObj.getString("Dia_Semana"),ScheObj.getString("Professor"),ScheObj.getString("Disciplina"),ScheObj.getString("Sala"),ScheObj.getString("Turma"));
                                ScheduleList.add(Sche);
                            }
                        }

                    } catch (JSONException e) {
                        e.printStackTrace();
                    }

                } else {
                    Log.e("JSON Data", "Didn't receive any data from server!");
                }

                return null;
            }

            @Override
            protected void onPostExecute(Void result) {
                super.onPostExecute(result);
                if (pDialog.isShowing())
                    pDialog.dismiss();
                populateListView();
            } 
        }
}

0 个答案:

没有答案