2012-04-11 3 views
1

new to new - 외부 데이터베이스의 데이터를 사용하여 textviews를 채우려고하는데이 오류가 발생합니다 (아래의 LogCat). 그물에 오랫동안 열심히 배어 든다. 그러나 위선적 인 말투는 나를 도와 줄 많은 것을 찾는 것처럼 보인다.java.lang.IndexOutOfBoundsException : 유효하지 않은 인덱스 0, 크기가 0입니다.

로그 캣

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

     }); 

    } 

} 

많은 감사

답변

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); 
+0

지연된 응답을 드려 죄송합니다. request.java를 게시하면 도움이 될까요? –

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가 비어 있으면 사용자가 언급 한 오류를 볼 수 있습니다.

0

jsondefs.get(0) 중 하나가 표시 될 가능성이 높습니다 ... if(!jsondefs.isEmpty())을 추가하고 문제가 무엇인지 알 수 있도록 다른 것을 인쇄해야합니다.

관련 문제