java.lang.IndexOutOfBoundsException:索引0无效,大小为0

时间:2012-04-11 23:27:18

标签: java android database arrays json

我是android新手 - 我尝试使用外部数据库中的数据填充文本视图,但我收到此错误(下面的LogCat)我在网上有一个很长的硬脑袋但是似乎找不到太多帮助我。

logcat的

04-11 23:10:56.084: E/AndroidRuntime(333): FATAL EXCEPTION: main
04-11 23:10:56.084: E/AndroidRuntime(333): java.lang.RuntimeException: Unable to start activity ComponentInfo{com.thickcrustdesigns.ufood/com.thickcrustdesigns.ufood.recipePage}: java.lang.IndexOutOfBoundsException: Invalid index 0, size is 0
04-11 23:10:56.084: E/AndroidRuntime(333):  at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:1647)
04-11 23:10:56.084: E/AndroidRuntime(333):  at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:1663)
04-11 23:10:56.084: E/AndroidRuntime(333):  at android.app.ActivityThread.access$1500(ActivityThread.java:117)
04-11 23:10:56.084: E/AndroidRuntime(333):  at android.app.ActivityThread$H.handleMessage(ActivityThread.java:931)
04-11 23:10:56.084: E/AndroidRuntime(333):  at android.os.Handler.dispatchMessage(Handler.java:99)
04-11 23:10:56.084: E/AndroidRuntime(333):  at android.os.Looper.loop(Looper.java:123)
04-11 23:10:56.084: E/AndroidRuntime(333):  at android.app.ActivityThread.main(ActivityThread.java:3683)
04-11 23:10:56.084: E/AndroidRuntime(333):  at java.lang.reflect.Method.invokeNative(Native Method)
04-11 23:10:56.084: E/AndroidRuntime(333):  at java.lang.reflect.Method.invoke(Method.java:507)
04-11 23:10:56.084: E/AndroidRuntime(333):  at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:839)
04-11 23:10:56.084: E/AndroidRuntime(333):  at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:597)
04-11 23:10:56.084: E/AndroidRuntime(333):  at dalvik.system.NativeStart.main(Native Method)
04-11 23:10:56.084: E/AndroidRuntime(333): Caused by: java.lang.IndexOutOfBoundsException: Invalid index 0, size is 0
04-11 23:10:56.084: E/AndroidRuntime(333):  at java.util.ArrayList.throwIndexOutOfBoundsException(ArrayList.java:257)
04-11 23:10:56.084: E/AndroidRuntime(333):  at java.util.ArrayList.get(ArrayList.java:311)
04-11 23:10:56.084: E/AndroidRuntime(333):  at com.thickcrustdesigns.ufood.recipePage.onCreate(recipePage.java:44)
04-11 23:10:56.084: E/AndroidRuntime(333):  at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1047)
04-11 23:10:56.084: E/AndroidRuntime(333):  at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:1611)
04-11 23:10:56.084: E/AndroidRuntime(333):  ... 11 more

recipePage

package com.thickcrustdesigns.ufood;

import java.util.ArrayList;
import org.apache.http.NameValuePair;
import org.apache.http.message.BasicNameValuePair;
import org.json.JSONException;
import org.json.JSONObject;
import android.app.Activity;
import android.os.Bundle;
import android.content.Intent;
import android.view.View;
import android.widget.Button;
import android.widget.TextView;

public class recipePage extends Activity {

    TextView txt_Recipe;
    TextView txt_Ingredients;
    TextView txt_Method;
    Button btn_bk;

    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.recipepage);
        Bundle data = getIntent().getExtras();
        String recipe = data.getString("recipie");


        ArrayList<NameValuePair> nvp = new ArrayList<NameValuePair>();

        nvp.add(new BasicNameValuePair("request", "recipe"));
        nvp.add(new BasicNameValuePair("recipe", recipe));

        ArrayList<NameValuePair> nvp2 = new ArrayList<NameValuePair>();
        nvp2.add(new BasicNameValuePair("request", "ingredients"));
        nvp2.add(new BasicNameValuePair("recipe", recipe));

        ArrayList<JSONObject> jsondefs = Request.fetchData(this, nvp);
        String title = null;
        try {
            title = jsondefs.get(0).getString("Name");
        } catch (JSONException e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        }
        String method = null;
        try {
            method = jsondefs.get(0).getString("Method");
        } catch (JSONException e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        }

        ArrayList<JSONObject> jsonIngredients = Request.fetchData(this, nvp2);

        String ingredients = ""; 

        for (int i = 0; i < jsonIngredients.size(); i++){
            String ji = null;
            try {
                ji = jsonIngredients.get(i).getString("Name") + "\n";
            } catch (JSONException e) {
                // TODO Auto-generated catch block
                e.printStackTrace();
            }
            ingredients += ji; 
        }

        txt_Recipe.setText(title);
        txt_Method.setText(method);
        txt_Ingredients.setText(ingredients);



        // Listening to button event
        btn_bk.setOnClickListener(new View.OnClickListener() {

            public void onClick(View arg0) {
                Intent i = new Intent(getApplicationContext(),
                        UFoodAppActivity.class);
                startActivity(i);

            }
        });
    }
}

request.java

package com.thickcrustdesigns.ufood;

import java.util.ArrayList;

import org.apache.http.NameValuePair;
import org.apache.http.message.BasicNameValuePair;
import org.json.JSONException;
import org.json.JSONObject;
import android.app.ListActivity;
import android.content.Intent;
import android.os.Bundle;
import android.view.View;
import android.widget.AdapterView;
import android.widget.AdapterView.OnItemClickListener;
import android.widget.Button;

import android.widget.ListView;

//import android.widget.TextView;

public class CatogPage extends ListActivity {

    ListView listView1;
    Button btn_bk;
    String[] defs;

    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);

        setContentView(R.layout.definition_main);

        btn_bk = (Button) findViewById(R.id.btn_bk);

        listView1 = (ListView) findViewById(android.R.id.list);

        ArrayList<NameValuePair> nvp = new ArrayList<NameValuePair>();
        nvp.add(new BasicNameValuePair("request", "categories"));

        ArrayList<JSONObject> jsondefs = Request.fetchData(this, nvp);

        defs = new String[jsondefs.size()];

        for (int i = 0; i < jsondefs.size(); i++) {
            try {
                defs[i] = jsondefs.get(i).getString("Name");
            } catch (JSONException e) {
                // TODO Auto-generated catch block
                e.printStackTrace();
            }
        }

        uFoodAdapter adapter = new uFoodAdapter(this, R.layout.definition_list,
                defs);

        listView1.setAdapter(adapter);
        ListView lv = getListView();
        lv.setOnItemClickListener(new OnItemClickListener() {

            public void onItemClick(AdapterView<?> parent, View view,
                    int position, long id) {

                String item = defs[position];
                Intent i = new Intent(getApplicationContext(), Results.class);
                i.putExtra("category", item);
                startActivity(i);
            }
        });

        btn_bk.setOnClickListener(new View.OnClickListener() {
            public void onClick(View arg0) {
                Intent i = new Intent(getApplicationContext(), CatogPage.class);
                startActivity(i);
            }

        });

    }

}

非常感谢

3 个答案:

答案 0 :(得分:1)

开始查看recipePage.java的第44行

com.thickcrustdesigns.ufood.recipePage.onCreate(recipePage.java:44)
04-11 23:10:56.084: E/AndroidRuntime(333):  at     
android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1047)
04-11 23:10:56.084: E/AndroidRuntime(333):  at 
android.app.ActivityThread.performLaunchActivity(ActivityThread.java:1611)

更多细节:

以下行未填写jsondefs

 ArrayList<JSONObject> jsondefs = Request.fetchData(this, nvp);

答案 1 :(得分:0)

看起来这个电话不安全:

    ArrayList<JSONObject> jsondefs = Request.fetchData(this, nvp);
    String title = null;
    try {
        title = jsondefs.get(0).getString("Name"); //<--- Here jsondefs could be empty
    } catch (JSONException e) {
        // TODO Auto-generated catch block
        e.printStackTrace();
    }

如果jsondefs为空,您可以看到您提到的错误。

答案 2 :(得分:0)

最有可能被其中一个jsondefs.get(0)抛出......你应该添加一个if(!jsondefs.isEmpty())和一个打印出来的东西,以便你知道问题所在。

相关问题