我的return Car;
存在问题我认为它会返回“KEY_IMAGE”字符串。
KEY_IMAGE是一个字符串,将放在BitmapFactory中:
byte[] logoImage = getLogoImage(IMAGEURL);
private byte[] getLogoImage(String url){
try {
URL imageUrl = new URL(url);
URLConnection ucon = imageUrl.openConnection();
InputStream is = ucon.getInputStream();
BufferedInputStream bis = new BufferedInputStream(is);
ByteArrayBuffer baf = new ByteArrayBuffer(500);
int current = 0;
while ((current = bis.read()) != -1) {
baf.append((byte) current);
}
return baf.toByteArray();
} catch (Exception e) {
Log.d("ImageManager", "Error: " + e.toString());
}
return null;
}
logoImage.setImageBitmap(BitmapFactory.decodeByteArray( currentAccount.accImage,
0,currentAccount.accImage.length));
我的方法有错误:
public Car getCurrentCar() {
SQLiteDatabase db = getWritableDatabase();
String sql = "SELECT " + KEY_ID + "," + KEY_IMAGE + " FROM " + TABLE_CARS + "ORDER BY RANDOM() LIMIT 1";
Cursor cursor = db.rawQuery(sql, new String[] {});
if(cursor.moveToFirst()){
Car car_current = new Car(Integer.parseInt(cursor.getString(0)), cursor.getString(1), cursor.getString(2),cursor.getString(3));
}
if (cursor != null && !cursor.isClosed()) {
cursor.close();
}
db.close();
if(cursor.getCount() == 0){
return null;
} else {
return Car;
}
}
回车;给了我错误:
“表达预期”和“找不到符号变量Car”。
有人能够解释为什么我不能回车吗?
编辑:
public Car getCurrentCar() {
SQLiteDatabase db = getWritableDatabase();
String sql = "SELECT " + KEY_ID + "," + KEY_IMAGE + " FROM " + TABLE_CARS + "ORDER BY RANDOM() LIMIT 1";
Cursor cursor = db.rawQuery(sql, new String[] {});
Car car = null;
try {
if (cursor.moveToFirst()) {
car = new Car(Integer.parseInt(cursor.getString(0)), cursor.getString(1), cursor.getString(2), cursor.getString(3));
}
}
finally {
if (cursor != null && !cursor.isClosed()) {
cursor.close();
}
db.close();
}
return car;
}
答案 0 :(得分:2)
找不到符号:变量
Car
这意味着编译器无法找到名为Car
的变量。
return Car;
Car
是一个类,而不是一个对象。您必须返回一个对象(或变量)。
我认为这就是你的意思
return car_current;
但你必须在if
之外声明该变量:
Car car_current = null;
if(cursor.moveToFirst()){
car_current = new Car(Integer.parseInt(cursor.getString(0)), cursor.getString(1), cursor.getString(2),cursor.getString(3));
}
答案 1 :(得分:2)
您已在另一个car_current
内宣布if
,其范围不同。该变量在if
块之外没有范围。如果你想在`if block。
if
之外声明它
其次,您正在返回Car
这是一个类,因此它无效。相反,您可能想要返回car_current
我想。
答案 2 :(得分:0)
这是Java 101。
试试这样:
// Why not pass in a Car id?
public Car getCurrentCar() {
// Bad form here. Externalize these.
SQLiteDatabase db = getWritableDatabase();
String sql = "SELECT " + KEY_ID + "," + KEY_IMAGE + " FROM " + TABLE_CARS + "ORDER BY RANDOM() LIMIT 1";
Cursor cursor = db.rawQuery(sql, new String[] {});
Car car = null;
if(cursor.moveToFirst()) {
car = new Car(Integer.parseInt(cursor.getString(0)), cursor.getString(1), cursor.getString(2),cursor.getString(3));
}
// Wrong - close in a finally block.
if (cursor != null && !cursor.isClosed()) {
cursor.close();
}
db.close(); // Wrong - close in a finally block.
return car;
}
答案 3 :(得分:0)
我认为你需要返回car_current而不是Car。但是变量car_current不在范围内,因此您应该在if语句之外定义它。 试试这个:
Car car_current = null;
if(cursor.moveToFirst()){
car_current = new Car(Integer.parseInt(cursor.getString(0)), cursor.getString(1), cursor.getString(2),cursor.getString(3));
}
//after some code you could return only car_current without control
return car_current;