我正在开发app,我想解析xml文件并将其存储在db中并从表中检索特定记录并在列表视图中显示它,当我点击它时,通过intent将该值传递给下一个活动。我试过了。当我将安装该应用程序的.apk它工作正常,但当我回到列出视图活动并单击任何值时,它会给出错误“forse close”并终止应用程序。并且在log cat中它给出了如下所示的错误。
它从db获取记录并且每次都在列表视图中完美显示。我收到错误onclick事件,当我再次回到列表视图时。
请帮我解决问题... 提前谢谢你......!
Logcat错误
> *03-22 22:07:51.681: E/AndroidRuntime(336): FATAL EXCEPTION: main
03-22 22:07:51.681: E/AndroidRuntime(336): java.lang.IndexOutOfBoundsException: Invalid index 1, size is 0
03-22 22:07:51.681: E/AndroidRuntime(336): at java.util.ArrayList.throwIndexOutOfBoundsException(ArrayList.java:257)
03-22 22:07:51.681: E/AndroidRuntime(336): at java.util.ArrayList.get(ArrayList.java:311)
03-22 22:07:51.681: E/AndroidRuntime(336): at com.dod.test.List_of_Consumer$1.onItemClick(List_of_Consumer.java:130)
03-22 22:07:51.681: E/AndroidRuntime(336): at android.widget.AdapterView.performItemClick(AdapterView.java:284)
03-22 22:07:51.681: E/AndroidRuntime(336): at android.widget.ListView.performItemClick(ListView.java:3513)
03-22 22:07:51.681: E/AndroidRuntime(336): at android.widget.AbsListView$PerformClick.run(AbsListView.java:1812)
03-22 22:07:51.681: E/AndroidRuntime(336): at android.os.Handler.handleCallback(Handler.java:587)
03-22 22:07:51.681: E/AndroidRuntime(336): at android.os.Handler.dispatchMessage(Handler.java:92)
03-22 22:07:51.681: E/AndroidRuntime(336): at android.os.Looper.loop(Looper.java:123)
03-22 22:07:51.681: E/AndroidRuntime(336): at android.app.ActivityThread.main(ActivityThread.java:3683)
03-22 22:07:51.681: E/AndroidRuntime(336): at java.lang.reflect.Method.invokeNative(Native Method)
03-22 22:07:51.681: E/AndroidRuntime(336): at java.lang.reflect.Method.invoke(Method.java:507)
03-22 22:07:51.681: E/AndroidRuntime(336): at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:839)
03-22 22:07:51.681: E/AndroidRuntime(336): at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:597)
03-22 22:07:51.681: E/AndroidRuntime(336): at dalvik.system.NativeStart.main(Native Method)*
List_of_Consumer // 我从db检索reords并在listview中显示的第一个活动。
package com.dod.test;
import java.io.File;
import java.io.FileInputStream;
import java.io.InputStream;
import java.io.InputStreamReader;
import java.util.ArrayList;
import org.xmlpull.v1.XmlPullParser;
import org.xmlpull.v1.XmlPullParserFactory;
import android.os.Bundle;
import android.os.Environment;
import android.app.Activity;
import android.content.Intent;
import android.view.Menu;
import android.view.View;
import android.widget.AdapterView;
import android.widget.AdapterView.OnItemClickListener;
import android.widget.ArrayAdapter;
import android.widget.Button;
import android.widget.ListView;
import android.widget.TextView;
import android.widget.Toast;
public class List_of_Consumer extends Activity {
Button back;
Intent i;
TextView txt_xml;
String consumer_id,meter,or,current,s;
ListView lv;
ArrayList<String> cid;
ArrayList<String> a;
InputStream is;
DB_Adapter database;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_list_of_consumer);
back = (Button)findViewById(R.id.btnback);
lv=(ListView)findViewById(R.id.lv);
database = new DB_Adapter(getApplicationContext(), "EBS", null, 1);
try
{
a = new ArrayList<String>();
if(database.count())
{
getxmldata();
}else
{
File xml = new File(Environment.getExternalStorageDirectory(), "t21.xml");
is = new FileInputStream(xml);
//is = new FileInputStream("/data/data/com.dod.test/t21.xml");
InputStreamReader isr= new InputStreamReader(is);
XmlPullParserFactory xf=XmlPullParserFactory.newInstance();
XmlPullParser xp=xf.newPullParser();
xp.setInput(isr);
int event=xp.getEventType();
while(event!=XmlPullParser.END_DOCUMENT)
{
if(event==XmlPullParser.START_TAG)
{
current=xp.getName();
}
else if(event==XmlPullParser.TEXT)
{
if(current.equals("consumerid"))
{
a.add(xp.getText());
consumer_id=xp.getText();
Toast.makeText(getApplicationContext(), consumer_id, Toast.LENGTH_SHORT).show();
}
else if(current.equals("meter"))
{
meter=xp.getText();
Toast.makeText(getApplicationContext(), meter, Toast.LENGTH_SHORT).show();
}
else if(current.equals("or"))
{
or=xp.getText();
database.addConsumer(consumer_id, meter,or);
database.close();
}
}
event=xp.next();
}
getxmldata();
}
lv.setOnItemClickListener(new OnItemClickListener() {
@Override
public void onItemClick(AdapterView<?> arg0, View arg1,
int arg2, long arg3) {
String st=a.get(arg2);
Toast.makeText(getApplicationContext(), "Consumer:="+st, Toast.LENGTH_SHORT).show();
Intent i = new Intent(getApplicationContext(),Reading_Detail.class);
i.putExtra("key", st);
startActivity(i);
// TODO Auto-generated method stub
}
});
} catch (Exception e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
/*back.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
// TODO Auto-generated method stub
i = new Intent(getApplicationContext(),Home.class);
startActivity(i);
finish();
}
});*/
}
public void getxmldata()
{
cid=database.get_consumer_id();
ArrayAdapter<String> aa = new ArrayAdapter<String>(getApplicationContext(), R.layout.row,R.id.txt,cid);
lv.setAdapter(aa);
}
@Override
public boolean onCreateOptionsMenu(Menu menu) {
// Inflate the menu; this adds items to the action bar if it is present.
getMenuInflater().inflate(R.menu.list_of_consumer, menu);
return true;
}
}
DB_Adapter // 数据库文件
package com.dod.test;
import java.util.ArrayList;
import android.content.ContentValues;
import android.content.Context;
import android.database.Cursor;
import android.database.sqlite.SQLiteDatabase;
import android.database.sqlite.SQLiteDatabase.CursorFactory;
import android.database.sqlite.SQLiteOpenHelper;
public class DB_Adapter extends SQLiteOpenHelper {
public SQLiteDatabase db;
String s2,s3;
public static final String KEY_NAME = "USERNAME";
public static final String KEY_PASSWORD = "PASSWORD";
public static final String DATABASE_TABLE = "LOGIN";
public static final String DATABASE_TABLE1 = "CONSUMER";
public static final String DATABASE_TABLE2 = "CONSUMER_XML";
public DB_Adapter(Context context, String name, CursorFactory factory,
int version) {
super(context, name, factory, version);
// TODO Auto-generated constructor stub
}
@Override
public void onCreate(SQLiteDatabase arg0) {
// TODO Auto-generated method stub
arg0.execSQL("CREATE TABLE LOGIN(ID INTEGER PRIMARY KEY AUTOINCREMENT," +
"USERNAME TEXT NOT NULL," +
"PASSWORD TEXT NOT NULL)");
arg0.execSQL("CREATE TABLE CONSUMER(ID INTEGER PRIMARY KEY AUTOINCREMENT," +
"CONSUMER_ID INTEGER NOT NULL," +
"METER_NO INTEGER NOT NULL," +
"PREV_READING INTEGER NOT NULL," +
"CURR_READING INTEGER NOT NULL," +
"IMAGE TEXT NOT NULL," +
"STATUS INTEGER NOT NULL)");
arg0.execSQL("CREATE TABLE CONSUMER_XML(CONSUMER_ID INTEGER PRIMARY KEY NOT NULL," +
"METER_NO INTEGER NOT NULL," +
"PREV_READING INTEGER NOT NULL)");
}
@Override
public void onUpgrade(SQLiteDatabase arg0, int arg1, int arg2) {
// TODO Auto-generated method stub
arg0.execSQL("DROP TABLE IF EXIST LOGIN");
arg0.execSQL("DROP TABLE IF EXIST CONSUMER");
arg0.execSQL("DROP TABLE IF EXIST CONSUMER_XML");
onCreate(arg0);
}
public void addConsumer(String consumerid,String meter,String pr)
{
db=getWritableDatabase();
ContentValues cv=new ContentValues();
cv.put("CONSUMER_ID", consumerid);
cv.put("METER_NO", meter);
cv.put("PREV_READING", pr);
db.insert(DATABASE_TABLE2, null, cv);
}
public boolean count()
{
Cursor c = getReadableDatabase().rawQuery("SELECT CONSUMER_ID FROM " + DATABASE_TABLE2,null);
if (c.getCount()>0)
return true;
return false;
}
public ArrayList<String> get_consumer_id()
{
ArrayList<String>names1=new ArrayList<String>();
Cursor c = getReadableDatabase().rawQuery("Select CONSUMER_ID from " + DATABASE_TABLE2, null);
c.moveToFirst();
do
{
String s1=c.getString(c.getColumnIndex("CONSUMER_ID"));
names1.add(s1);
}while(c.moveToNext());
return names1;
}
}
答案 0 :(得分:0)
你的logcat清楚地说明你正在获得Index out of bounds exception。
java.lang.IndexOutOfBoundsException: Invalid index 1, size is 0
at com.dod.test.List_of_Consumer$1.onItemClick(List_of_Consumer.java:130)
您的代码中的第130行是什么?如果您可以使用更具体的代码部分编辑您的问题,那么就有可能根据这一点编辑我的答案。