我是新來的android - 我想填充textviews使用外部數據庫的數據,但我收到此錯誤(LogCat下面)我有一個網上的長硬堅果,但似乎找不到很多幫助我。java.lang.IndexOutOfBoundsException:無效的索引0,大小爲0
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);
}
});
}
}
非常感謝
對不起延遲迴復:如果我發佈我的request.java會有幫助嗎? – 2012-04-17 00:44:15