有 Java 编程相关的问题?

你可以在下面搜索框中键入要查询的问题!

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

我是安卓新手-我试图使用外部数据库的数据填充文本视图,但我收到了这个错误(下面是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 安卓.app.ActivityThread.performLaunchActivity(ActivityThread.java:1647)
04-11 23:10:56.084: E/AndroidRuntime(333):  at 安卓.app.ActivityThread.handleLaunchActivity(ActivityThread.java:1663)
04-11 23:10:56.084: E/AndroidRuntime(333):  at 安卓.app.ActivityThread.access$1500(ActivityThread.java:117)
04-11 23:10:56.084: E/AndroidRuntime(333):  at 安卓.app.ActivityThread$H.handleMessage(ActivityThread.java:931)
04-11 23:10:56.084: E/AndroidRuntime(333):  at 安卓.os.Handler.dispatchMessage(Handler.java:99)
04-11 23:10:56.084: E/AndroidRuntime(333):  at 安卓.os.Looper.loop(Looper.java:123)
04-11 23:10:56.084: E/AndroidRuntime(333):  at 安卓.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.安卓.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:839)
04-11 23:10:56.084: E/AndroidRuntime(333):  at com.安卓.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 安卓.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1047)
04-11 23:10:56.084: E/AndroidRuntime(333):  at 安卓.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 安卓.app.Activity;
import 安卓.os.Bundle;
import 安卓.content.Intent;
import 安卓.view.View;
import 安卓.widget.Button;
import 安卓.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);

            }
        });
    }
}

请求。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 安卓.app.ListActivity;
import 安卓.content.Intent;
import 安卓.os.Bundle;
import 安卓.view.View;
import 安卓.widget.AdapterView;
import 安卓.widget.AdapterView.OnItemClickListener;
import 安卓.widget.Button;

import 安卓.widget.ListView;

//import 安卓.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(安卓.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) 个答案

  1. # 1 楼答案

    开始查看recipePage的第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);
    
  2. # 2 楼答案

    很可能是其中一个jsondefs.get(0)抛出的…您应该添加一个if(!jsondefs.isEmpty())和一个打印某些内容的else,以便知道问题所在

  3. # 3 楼答案

    看起来这个电话不安全:

        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为空,则可以看到您提到的错误