SQLite Android。管理数据
我会告诉你我应用程序这一部分的目的。我有一个SQLite数据库,它有3列。第一个是“数量”,第二个是“产品”,第三个是“价格”。嗯,我想做的是得到整个数据库并通过电子邮件发送。 这就是我现在拥有的:SQLite Android。管理数据,android,sqlite,email,android-intent,Android,Sqlite,Email,Android Intent,我会告诉你我应用程序这一部分的目的。我有一个SQLite数据库,它有3列。第一个是“数量”,第二个是“产品”,第三个是“价格”。嗯,我想做的是得到整个数据库并通过电子邮件发送。 这就是我现在拥有的: public class Visual extends Activity { TextView tv; Button sqlGetInfo; long l ; long b=1; int c,i; String returnedQuantity ,returnedProduct ,returned
public class Visual extends Activity {
TextView tv;
Button sqlGetInfo;
long l ;
long b=1;
int c,i;
String returnedQuantity ,returnedProduct ,returnedPrice;
String[] filas;
@Override
protected void onCreate(Bundle savedInstanceState) {
// TODO Auto-generated method stub
super.onCreate(savedInstanceState);
setContentView(R.layout.visual);
tv = (TextView) findViewById(R.id.tvSQLinfo);
sqlGetInfo = (Button) findViewById(R.id.EnviarPedido);
SQLManager info = new SQLManager(this);
info.open();
String data= info.getData();
info.close();
tv.setText(data);
到目前为止,我的代码运行良好,它在textView中显示数据。问题就在这里。我的数据库最多有15行。我想做的是将每一行存储在字符串数组(filas)的一个位置。第一行=filas(0),第二行=filas(1)…以便能够将此数组传递给另一个活动。如果数组的行数少于15行,我想它会给出一个异常。现在是开始另一项活动的时候了
final SQLManager hon = new SQLManager(this);
sqlGetInfo.setOnClickListener(new OnClickListener() {
@Override
public void onClick(View v) {
// TODO Auto-generated method stub
for (i = 1; i < 15; i++) {
try{
l = (long) i;
hon.open();
returnedQuantity = hon.getQuantity(l);
returnedProduct = hon.getProduct(l);
returnedPrice = hon.getPrice(l);
hon.close();
c=(int)(l-b);
filas[c]="" + returnedQuantity+" "+ returnedProduct+" "+ returnedPrice + "\n";
}catch (Exception e){
i = 16;
l = (long) i;
Intent abrePedidos = new Intent(Visual.this, Pedidos.class);
abrePedidos.putExtra("pedidoCompleto", filas);
startActivity(abrePedidos);
}
}
}
});
}
}
我收到的电子邮件信息是“空的”。我想你的问题在于:你从来没有初始化
String[]filas代码>。这意味着它始终保持为空。然后,转到代码:
try {
l = (long) i;
hon.open();
returnedQuantity = hon.getQuantity(l);
returnedProduct = hon.getProduct(l);
returnedPrice = hon.getPrice(l);
hon.close();
c=(int)(l-b);
// The next line throws null pointer exception
filas[c]="" + returnedQuantity+" "+ returnedProduct+" "+ returnedPrice + "\n";
} catch (Exception e) { // here you catch the null pointer exception
i = 16;
l = (long) i;
Intent abrePedidos = new Intent(Visual.this, Pedidos.class);
abrePedidos.putExtra("pedidoCompleto", filas); //filas is still null
startActivity(abrePedidos);
}
我为你的一些台词添加了评论。为什么只在catch子句中调用下一个活动?catch子句中包含这么多代码是非常糟糕的做法,它被称为catch子句。不要这样做。否则,如果您初始化了数组(我不确定您是否还没有初始化,但我猜您隐藏的异常是什么),您应该可以开始了。不过,不要忘记将代码放在catch块之外,因为我不希望修改后出现任何异常
编辑我不喜欢在数据库中迭代元素的方式。我不熟悉您使用的SQLManager
类。然而,标准的Android方式与JDBC模型非常相似。您执行一个查询,它会在结果上返回一个光标。请参见此处使用游标和SQLitehleprs
的示例:如果我正确理解了filas
isnull
这一行:String message=cabecera+filas代码>?或者你的电子邮件是指什么?当startActivity调用emailIntent时,手机上的电子邮件应用程序(我是gmail)会显示:电子邮件、主题和消息。空值是filas,而不是cabecera。我在catch块上使用该代码,因为我不知道数据库有多少行,所以当de cursor超出边界时,我可以猜测它占用了数据库的最后一个条目。你知道更好的方法吗?
try {
l = (long) i;
hon.open();
returnedQuantity = hon.getQuantity(l);
returnedProduct = hon.getProduct(l);
returnedPrice = hon.getPrice(l);
hon.close();
c=(int)(l-b);
// The next line throws null pointer exception
filas[c]="" + returnedQuantity+" "+ returnedProduct+" "+ returnedPrice + "\n";
} catch (Exception e) { // here you catch the null pointer exception
i = 16;
l = (long) i;
Intent abrePedidos = new Intent(Visual.this, Pedidos.class);
abrePedidos.putExtra("pedidoCompleto", filas); //filas is still null
startActivity(abrePedidos);
}