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

java.lang.IndexOutOfBoundsException:索引0无效,大小为0,java,android,database,arrays,json,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 Com

我是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);

            }
        });
    }
}
package com.designs.ufood;
导入java.util.ArrayList;
导入org.apache.http.NameValuePair;
导入org.apache.http.message.BasicNameValuePair;
导入org.json.JSONException;
导入org.json.JSONObject;
导入android.app.Activity;
导入android.os.Bundle;
导入android.content.Intent;
导入android.view.view;
导入android.widget.Button;
导入android.widget.TextView;
公共类recipePage扩展活动{
TextView txt_配方;
TextView txt_成分;
TextView txt_方法;
按钮btn_bk;
@凌驾
创建时的公共void(Bundle savedInstanceState){
super.onCreate(savedInstanceState);
setContentView(R.layout.recipepage);
Bundle data=getIntent().getExtras();
字符串配方=data.getString(“recipie”);
ArrayList nvp=新的ArrayList();
添加(新的BasicNameValuePair(“请求”、“配方”);
添加(新的BasicNameValuePair(“配方”,配方));
ArrayList nvp2=新的ArrayList();
添加(新的BasicNameValuePair(“请求”、“成分”);
添加(新的BasicNameValuePair(“配方”,配方));
ArrayList jsondefs=Request.fetchData(这是nvp);
字符串标题=null;
试一试{
title=jsondefs.get(0.getString(“名称”);
}捕获(JSONException e){
//TODO自动生成的捕捉块
e、 printStackTrace();
}
字符串方法=null;
试一试{
method=jsondefs.get(0.getString(“方法”);
}捕获(JSONException e){
//TODO自动生成的捕捉块
e、 printStackTrace();
}
ArrayList jsonIngredients=Request.fetchData(这是nvp2);
字符串=”;
对于(int i=0;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);
            }

        });

    }

}
package com.designs.ufood;
导入java.util.ArrayList;
导入org.apache.http.NameValuePair;
导入org.apache.http.message.BasicNameValuePair;
导入org.json.JSONException;
导入org.json.JSONObject;
导入android.app.ListActivity;
导入android.content.Intent;
导入android.os.Bundle;
导入android.view.view;
导入android.widget.AdapterView;
导入android.widget.AdapterView.OnItemClickListener;
导入android.widget.Button;
导入android.widget.ListView;
//导入android.widget.TextView;
公共类CatogPage扩展了ListActivity{
列表视图列表视图1;
按钮btn_bk;
字符串[]defs;
@凌驾
创建时的公共void(Bundle savedInstanceState){
super.onCreate(savedInstanceState);
setContentView(R.layout.definition_main);
btn_bk=(按钮)findviewbyd(R.id.btn_bk);
listView1=(ListView)findViewById(android.R.id.list);
ArrayList nvp=新的ArrayList();
nvp.add(新的BasicNameValuePair(“请求”、“类别”);
ArrayList jsondefs=Request.fetchData(这是nvp);
defs=新字符串[jsondefs.size()];
对于(int i=0;i

非常感谢

开始查看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);
ArrayList jsondefs=Request.fetchData(这是nvp);

开始查看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);
ArrayList jsondefs=Request.fetchData(这是nvp);

看起来此呼叫不安全:

    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();
    }
ArrayList jsondefs=Request.fetchData(这是nvp);
字符串标题=null;
试一试{

title=jsondefs.get(0).getString(“Name”);//看起来此调用不安全:

    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();
    }
ArrayList jsondefs=Request.fetchData(这是nvp);
字符串标题=null;
试一试{
title=jsondefs.get(0.getString(“Name”);//最有可能由