如何在android中解析数组索引超出绑定的异常

时间:2016-05-07 05:55:01

标签: java android

我在doInBackground(String... params)方法中遇到错误,而我正在调用网络服务。我正在尝试编写以下代码。

protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_add_colony_);
    new AddColony_LoaderTask().execute();

    add_colony_title = (TextView) findViewById(R.id.text_colony);
    note_Text=(TextView) findViewById(R.id.note_text);
    btn_add_Colony_back = (ImageButton) findViewById(R.id.add_colony_back);
    btnSave=(Button) findViewById(R.id.save_note);

    Typeface font1 = Typeface.createFromAsset(getAssets(), "fonts/cabin.regular.ttf");
    add_colony_title.setTypeface(font1); add_colony_title.setTextSize(18.0f);
    btnSave.setTypeface(font1);btnSave.setTextSize(18.0f);
    note_Text.setTypeface(font1);note_Text.setTextSize(15.0f);

    if (savedInstanceState == null) {
        Bundle extras = getIntent().getExtras();

        if (extras == null) {
            bookmarkid = null;
            note_type = null;


        } else {
            bookmarkid = extras.getString("bookmarkid");
            note_type = extras.getString("note_type");

        }
    } else {
        bookmarkid = (String) savedInstanceState.getSerializable("bookmarkid");
        note_type = (String) savedInstanceState.getSerializable("note_type");

    }

    System.out.println(bookmarkid);
    System.out.println(note_type);

    btnSave.setOnClickListener(new View.OnClickListener() {

        @Override
        public void onClick(View v) {
            Intent in = new Intent(getApplicationContext(), Dashboard.class);
            startActivity(in);

            new AddColony_LoaderTask().execute("https://www.mysites.com/secure-mobile/note?"," access_token",bookmarkid,note_type);
        }
    });


}




class AddColony_LoaderTask extends AsyncTask<String, String, String>
{

    protected String doInBackground(String... params) {

        bookmarkid = params[2];
        note_type = params[3];
        HttpParams httpParameters = new BasicHttpParams();
        HttpConnectionParams.setConnectionTimeout(httpParameters, 5000);
        HttpConnectionParams.setSoTimeout(httpParameters, 5000);
        HttpClient httpClient = new DefaultHttpClient(httpParameters);

        HttpPost httpPost = new HttpPost(params[0]);

        String jsonResult = "";
        try {

            List<NameValuePair> nameValuePairs = new ArrayList<NameValuePair>(3);

            nameValuePairs.add(new BasicNameValuePair("access_token", "94529e5dbc6234fc3bbfce7406b8dde9"));
            nameValuePairs.add(new BasicNameValuePair("bookmarkId",bookmarkid));
            nameValuePairs.add(new BasicNameValuePair("note",note_type));
            httpPost.setEntity(new UrlEncodedFormEntity(nameValuePairs));
            HttpResponse response = httpClient.execute(httpPost);

            int status = 200;

            if (status == 200) {
                HttpEntity entity = response.getEntity();
                String data = EntityUtils.toString(entity);
                System.out.println(data);

                System.out.println("fffff");
                JSONObject jsono = new JSONObject(data);

                for (int i = 0; i < jsono.length(); i++)
                {

                }
                return null;
            }

            //------------------>>

        } catch (ParseException e1) {
            e1.printStackTrace();
        } catch (IOException e) {
            e.printStackTrace();
        } catch (JSONException e) {
            e.printStackTrace();
        }
        return null;
    }
}

并在应用程序运行时在行bookmarkid = params[2];显示以下错误

FATAL EXCEPTION: AsyncTask #2
Process: com.example.mysite.myhive_app, PID: 13842
java.lang.RuntimeException: An error occured while executing doInBackground()
    at android.os.AsyncTask$3.done(AsyncTask.java:300)
    at java.util.concurrent.FutureTask.finishCompletion(FutureTask.java:355)
    at java.util.concurrent.FutureTask.setException(FutureTask.java:222)
    at java.util.concurrent.FutureTask.run(FutureTask.java:242)
    at android.os.AsyncTask$SerialExecutor$1.run(AsyncTask.java:231)
    at java.util.concurrent.ThreadPoolExecutor.runWorker(ThreadPoolExecutor.java:1112)
    at java.util.concurrent.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:587)
    at java.lang.Thread.run(Thread.java:841)
 Caused by: java.lang.ArrayIndexOutOfBoundsException: length=0; index=3
    at com.example.mysite.myhive_app.AddColony_Activity$AddColony_LoaderTask.doInBackground(AddColony_Activity.java:119)
    at com.example.mysite.myhive_app.AddColony_Activity$AddColony_LoaderTask.doInBackground(AddColony_Activity.java:114)
    at android.os.AsyncTask$2.call(AsyncTask.java:288)
    at java.util.concurrent.FutureTask.run(FutureTask.java:237)
    at android.os.AsyncTask$SerialExecutor$1.run(AsyncTask.java:231) 
    at java.util.concurrent.ThreadPoolExecutor.runWorker(ThreadPoolExecutor.java:1112) 
    at java.util.concurrent.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:587) 
    at java.lang.Thread.run(Thread.java:841)

2 个答案:

答案 0 :(得分:1)

检查您的onCreate方法。

protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_add_colony_);
    new AddColony_LoaderTask().execute(); // here is the cause

    add_colony_title = (TextView) findViewById(R.id.text_colony);

您正在执行任务而不传递任何参数。所以ArrayIndexOutOfBoundsException

希望它能提供帮助。

答案 1 :(得分:0)

你只是在不传递任何数据的情况下执行Asynchtask。

TextInputLayout inputLayout = (TextInputLayout) findViewById(R.id.yourTextInputLayout;
EditText editText = inputLayout.getEditText();
final OnFocusChangeListener existing = editText.getOnFocusChangeListener();

editText.setOnFocusChangeListener(new OnFocusChangeListener() {
    public void onFocusChange(View view, boolean focused) {
        existing.onFocusChange(view, focused);
        edittext.setHintTextColor(Color.RED);
    }
});

这样做

 new AddColony_LoaderTask().execute();