1

私は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

レシピページ

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);
            }

        });

    }

}

どうもありがとう

4

3 に答える 3

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);
于 2012-04-11T23:31:05.403 に答える
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 が空の場合、言及しているエラーが表示される可能性があります。

于 2012-04-11T23:33:16.630 に答える
0

のいずれかによってスローされる可能性が最も高い…問題が何であるかがわかるように、何かを出力する および else をjsondefs.get(0)追加する必要があります。if(!jsondefs.isEmpty())

于 2012-04-11T23:34:21.410 に答える