我在ArrayAdapter中收到illegalstateexception错误....我将通过这段代码超过两天但仍然得到了答案

时间:2015-03-20 10:14:52

标签: android android-arrayadapter illegalstateexception

public class Update extends Activity {

EditText edt_remark, edt_date;
Button but_save, but_cancel;
Spinner spin_stage, spin_status;
ImageButton but_date;
ListView lv;
String data, jobimporter, jobstage, jobstatus, jobdate, updatedremark,
        stagename, statusname, updateddate, updatedstage, updatedstatus;
String sending_data, sending_importer, sending_stage, sending_status,
        sending_remark, sending_date;
String stage1, parsedstage, receivedstage;
ArrayList<String> list, list_stage, list_status;
Calendar cal;
int day, month, year, stageid, parsedstageid1; 
public static int parsedstageid2;
private static String TagContacts = "Employee";
JSONArray contacts = null;
ImageView research;
EditText getnewsearch;
Statustask st;
Postingtask pt;
Stageidtask stid;


@Override
public void onCreate(Bundle savedInstanceState) {

    super.onCreate(savedInstanceState);
    setTitle("Update Page");
    setContentView(R.layout.copyofupdate);


    edt_remark = (EditText) findViewById(R.id.edit_remark);
    edt_date = (EditText) findViewById(R.id.edit_date);
    but_save = (Button) findViewById(R.id.button_save);
    but_cancel = (Button) findViewById(R.id.button_cancel);
    but_date = (ImageButton) findViewById(R.id.button_date);
    spin_stage = (Spinner) findViewById(R.id.spinner_stage);
    spin_status = (Spinner) findViewById(R.id.spinner_status);
    lv = (ListView) findViewById(R.id.listView1);

    research = (ImageView) findViewById(R.id.button1);
    getnewsearch = (EditText) findViewById(R.id.editText1);

    list = new ArrayList<String>();
    list_stage = new ArrayList<String>();

    data = getIntent().getExtras().getString("DATA");
    jobimporter = getIntent().getExtras().getString("JOBIMPORTER");
    jobstage = getIntent().getExtras().getString("JOBSTAGE");
    jobstatus = getIntent().getExtras().getString("JOBSTATUS");
    jobdate = getIntent().getExtras().getString("JOBDATE");


    cal = Calendar.getInstance();
    day = cal.get(Calendar.DAY_OF_MONTH);
    month = cal.get(Calendar.MONTH);
    year = cal.get(Calendar.YEAR);
    edt_date.setText(day + " / " + (month + 1) + " / " + year);

    list.add("Job/BE No. : " + data);
    list.add("Importer : " + jobimporter);
    list.add("Current Stage : " + jobstage);
    list.add("Current Status : " + jobstatus);
    list.add("Status Date : " + jobdate);

    ArrayAdapter<String> adapter = new ArrayAdapter<String>(Update.this,
            android.R.layout.simple_list_item_1, list);
    lv.setAdapter(adapter);

    new Stagetask().execute();

    research.setOnClickListener(new OnClickListener() {

        @Override
        public void onClick(View v) {
            // TODO Auto-generated method stub
            String newdata = getnewsearch.getText().toString();
            Log.d("NEWDATA",newdata);
            if(TextUtils.isEmpty(newdata)==true) {
                Toast.makeText(Update.this, "Enter BE/JOB No.", Toast.LENGTH_SHORT).show();
            }
            else {

            Intent ii = new Intent(Update.this,SearchActivity.class);
            ii.putExtra("NEWDATA", newdata);
            ii.putExtra("FLAG", 200);
            startActivity(ii);
            finish();
            }
        }
    });
    but_cancel.setOnClickListener(new OnClickListener() {

        @Override
        public void onClick(View v) {
            AlertDialog.Builder alert = new AlertDialog.Builder(
                    Update.this);
            alert.setTitle("Jobstage update");
            alert.setMessage("Are you sure you want to cancel???");
            alert.setCancelable(false);
            alert.setPositiveButton("Yes",
                    new DialogInterface.OnClickListener() {

                        @Override
                        public void onClick(DialogInterface dialog,
                                int which) {

                             Intent ii = new Intent(Update.this,SearchActivity.class);
                             ii.putExtra("FLAG",100);
                             startActivity(ii);
                             finish();
                        }
                    });
            alert.setNegativeButton("No",
                    new DialogInterface.OnClickListener() {

                        @Override
                        public void onClick(DialogInterface dialog,
                                int which) {
                            // TODO Auto-generated method stub
                            }
                    });
            alert.create().show();
        }
    });

    but_save.setOnClickListener(new OnClickListener() {

        @Override
        public void onClick(View v) {
            try {

                StringBuilder builder = new StringBuilder();
                builder.append(month + 1).append("-").append(day)
                        .append("-").append(year);
                updateddate = builder.toString();

                updatedremark = edt_remark.getText().toString();
                updatedstage = spin_stage.getSelectedItem().toString();
                updatedstatus = spin_status.getSelectedItem().toString();

                sending_data = data.trim();
                sending_importer = jobimporter.replaceAll(" ", "%20")
                        .trim();
                sending_stage = updatedstage.replaceAll(" ", "%20").trim();
                sending_status = updatedstatus.trim()
                        .replaceAll(" ", "%20");
                sending_remark = updatedremark.replaceAll(" ", "%20")
                        .trim();
                sending_date = updateddate.trim();

                if (sending_importer.isEmpty() || sending_stage.isEmpty()
                        || sending_status.isEmpty()
                        || sending_remark.isEmpty()
                        || sending_date.isEmpty()) {
                    Toast.makeText(Update.this, "Enter complete details",
                            Toast.LENGTH_SHORT).show();
                } else {
                    AlertDialog.Builder alert = new AlertDialog.Builder(
                            Update.this);
                    alert.setTitle("Jobstage update");
                    alert.setMessage("Do you want to save this record???");
                    alert.setCancelable(false);
                    alert.setPositiveButton("Yes",
                            new DialogInterface.OnClickListener() {

                                @Override
                                public void onClick(DialogInterface dialog,
                                        int which) {

                                     pt= new Postingtask();
                                     pt.execute();
                                     Toast.makeText(Update.this, "Successfull", Toast.LENGTH_SHORT).show();
                                     Intent ii = new Intent(Update.this,SearchActivity.class);
                                     ii.putExtra("FLAG",100);
                                     startActivity(ii);
                                     finish();
                                }
                            });
                    alert.setNegativeButton("No",
                            new DialogInterface.OnClickListener() {

                                @Override
                                public void onClick(DialogInterface dialog,
                                        int which) {
                                    // TODO Auto-generated method stub
                                    Toast.makeText(Update.this,
                                            "Not Saved", Toast.LENGTH_SHORT)
                                            .show();

                                }
                            });
                    alert.create().show();

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

    but_date.setOnClickListener(new OnClickListener() {

        @SuppressWarnings("deprecation")
        @Override
        public void onClick(View v) {

            showDialog(0);
        }
    });
    Log.d("parsedstageid2 in ONCREATE",String.valueOf(parsedstageid2));
    spin_stage
            .setOnItemSelectedListener(new AdapterView.OnItemSelectedListener() {

                @Override
                public void onItemSelected(AdapterView<?> arg0, View arg1,
                        int arg2, long arg3) {
                    // TODO Auto-generated method stub

                    String selectedstage = (String) arg0.getSelectedItem();
                    Log.d("SELECTED STAGE in Spinner", selectedstage);

                    stid = new Stageidtask();
                    stid.receivedstage = selectedstage;
                    stid.execute();
                }

                @Override
                public void onNothingSelected(AdapterView<?> arg0) {
                    // TODO Auto-generated method stub

                }
            });
    }

public void passing(Integer result) {
    // TODO Auto-generated method stub

    Log.d("Received parsedstageid2 in passing()",String.valueOf(result));


    st = new Statustask(result);
        st.execute();
        ArrayAdapter<String> adapter1 = new ArrayAdapter<String>(Update.this, android.R.layout.simple_spinner_dropdown_item,st.list_status);
        spin_status.setAdapter(adapter1);
}


@Override
protected Dialog onCreateDialog(int id) {
    return new DatePickerDialog(this, datePickerListener, year, month, day);
}

private DatePickerDialog.OnDateSetListener datePickerListener = new DatePickerDialog.OnDateSetListener() {
    public void onDateSet(DatePicker view, int selectedYear,
            int selectedMonth, int selectedDay) {
        edt_date.setText(selectedDay + " / " + (selectedMonth + 1) + " / "
                + selectedYear);
    }
};

public class Stagetask extends AsyncTask<Void, Void, Void> {


    @Override
    protected void onPreExecute() {
        // TODO Auto-generated method stub
        super.onPreExecute();
    }

    @Override
    protected Void doInBackground(Void... arg0) {
        // TODO Auto-generated method stub

        String url = "http://114.143.96.201/Job/Default.aspx?query=select%20*%20from%20M_Stage";
        ServiceHandler sh = new ServiceHandler();
        String jsonStr = sh.makeServiceCall(url, ServiceHandler.GET);

        if (jsonStr != null) {
            try {
                JSONObject jsonObj = new JSONObject(jsonStr);

                // Getting JSON Array node
                contacts = jsonObj.getJSONArray(TagContacts);
            //  list_stage.add(0, "Choose here");
                runOnUiThread(new Runnable() {

                    @Override
                    public void run() {
                        // TODO Auto-generated method stub
                        for (int i = 0; i < contacts.length(); i++) {
                            try {
                                JSONObject c = contacts.getJSONObject(i);

                                stagename = c.getString("StageName");
                                Log.d("STAGE",stagename);
                                list_stage.add(stagename);

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

            } catch (JSONException e) {
                e.printStackTrace();
            }
        } else {
            Log.e("ServiceHandler", "Couldn't get any data from the url");
        }

        return null;
    }

    @Override
    protected void onPostExecute(Void result) {
        // TODO Auto-generated method stub
        super.onPostExecute(result);
        ArrayAdapter<String> adapter = new ArrayAdapter<String>(
                Update.this, android.R.layout.simple_spinner_dropdown_item,
                list_stage);
        spin_stage.setAdapter(adapter);
    }
}

}

现在下面是StatusTask类.. 公共类Statustask扩展了AsyncTask {

private static String TagContacts = "Employee";
JSONArray contacts;
ArrayList<String> list_status = new ArrayList<String>();
String statusname;
int stageid1;

public Statustask(Integer result) {
    // TODO Auto-generated constructor stub
    stageid1 = result;
    Log.d("STAGEID1 value",String.valueOf(stageid1));
}

@Override
protected void onPreExecute() {
    // TODO Auto-generated method stub
    super.onPreExecute();
}

@Override
protected Void doInBackground(Void... arg0) {
    // TODO Auto-generated method stub
    String url = "http://114.143.96.201/Job/Default.aspx?query=select%20*%20from%20T_stagestatus%20where%20stageid="+stageid1;
    Log.d("URL IN STATUSTASK",url);
    ServiceHandler sh = new ServiceHandler();
    String jsonStr = sh.makeServiceCall(url, ServiceHandler.GET);

    if (jsonStr != null) {
        try {
            JSONObject jsonObj = new JSONObject(jsonStr);

            // Getting JSON Array node
            contacts = jsonObj.getJSONArray(TagContacts);

                    for (int i = 0; i < contacts.length(); i++) {
                        try {
                            JSONObject c = contacts.getJSONObject(i);

                            statusname = c.getString("StatusName");
                            list_status.add(statusname);
                            Log.d("LIST_STATUS",statusname);

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

                }


        } catch (JSONException e) {
            e.printStackTrace();
        }
    } else {
        Log.e("ServiceHandler", "Couldn't get any data from the url");
    }

    return null;
}

@Override
protected void onPostExecute(Void result) {
    super.onPostExecute(result);

}

}

现在我收到以下错误:

03-20 05:49:17.867: E/AndroidRuntime(2960): FATAL EXCEPTION: main
03-20 05:49:17.867: E/AndroidRuntime(2960): Process: com.vts.jobstage, PID: 2960
03-20 05:49:17.867: E/AndroidRuntime(2960): java.lang.IllegalStateException: System services not available to Activities before onCreate()
03-20 05:49:17.867: E/AndroidRuntime(2960):     at android.app.Activity.getSystemService(Activity.java:4532)
03-20 05:49:17.867: E/AndroidRuntime(2960):     at android.widget.ArrayAdapter.init(ArrayAdapter.java:310)
03-20 05:49:17.867: E/AndroidRuntime(2960):     at android.widget.ArrayAdapter.<init>(ArrayAdapter.java:153)
03-20 05:49:17.867: E/AndroidRuntime(2960):     at com.vts.jobstage.Update.passing(Update.java:289)
03-20 05:49:17.867: E/AndroidRuntime(2960):     at com.vts.jobstage.Stageidtask.onPostExecute(Stageidtask.java:83)
03-20 05:49:17.867: E/AndroidRuntime(2960):     at com.vts.jobstage.Stageidtask.onPostExecute(Stageidtask.java:1)
03-20 05:49:17.867: E/AndroidRuntime(2960):     at android.os.AsyncTask.finish(AsyncTask.java:632)
03-20 05:49:17.867: E/AndroidRuntime(2960):     at android.os.AsyncTask.access$600(AsyncTask.java:177)
03-20 05:49:17.867: E/AndroidRuntime(2960):     at android.os.AsyncTask$InternalHandler.handleMessage(AsyncTask.java:645)
03-20 05:49:17.867: E/AndroidRuntime(2960):     at android.os.Handler.dispatchMessage(Handler.java:102)
03-20 05:49:17.867: E/AndroidRuntime(2960):     at android.os.Looper.loop(Looper.java:136)
03-20 05:49:17.867: E/AndroidRuntime(2960):     at android.app.ActivityThread.main(ActivityThread.java:5001)
03-20 05:49:17.867: E/AndroidRuntime(2960):     at java.lang.reflect.Method.invokeNative(Native Method)
03-20 05:49:17.867: E/AndroidRuntime(2960):     at java.lang.reflect.Method.invoke(Method.java:515)
03-20 05:49:17.867: E/AndroidRuntime(2960):     at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:785)
03-20 05:49:17.867: E/AndroidRuntime(2960):     at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:601)
03-20 05:49:17.867: E/AndroidRuntime(2960):     at dalvik.system.NativeStart.main(Native Method)

1 个答案:

答案 0 :(得分:0)

      public void passing(Integer result) {
    /***************************   
     st = new Statustask(result);
    st.execute();
    /*******************************
      here your trying to set the result from task before the completion of task/***************(st.list_status)***********/
    ArrayAdapter<String> adapter1 = new ArrayAdapter<String>(Update.this,android.R.layout.simple_spinner_dropdown_item,st.list_status);
    spin_status.setAdapter(adapter1);

  }

移动以下内容:

ArrayAdapter<String> adapter1 = new ArrayAdapter<String>          (Update.this,android.R.layout.simple_spinner_dropdown_item,st.list_status);
    spin_status.setAdapter(adapter1); 

执行atTack后。