我尝试将我的应用程序从MenuActivity启动到RecipesActivity。在RecipesActivity中,我尝试从表中选择元素,这是我在DataBase类(DataBase.java)中创建的。现在这个表是空的。调试器无法帮助我,我不知道为什么应用程序崩溃,可能因为表现在是空的。
MenuActivity.java的一部分
case R.id.recipes:
case R.id.iconrecipes:
Intent i2 = new Intent(this, RecipesActivity.class);
this.startActivity(i2);
break;
RecipesActivity.java的一部分
public class RecipesActivity extends Activity implements OnClickListener {
static final public int AddingRecipe = 0;
static final public int EditingRecipe = 1;
//static final public String EditRecipe = "EditRecipe";
static final public String RecipeId = "RecipeId";
static final public String ShowRecipe = "ShowRecipe";
LinearLayout ll;
DataBase sqh;
SQLiteDatabase sqdb;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_recipes);
ll = (LinearLayout)findViewById(R.id.llRecipes);
sqh = new DataBase(this);
sqdb = sqh.getWritableDatabase();
Cursor cursor = sqdb.rawQuery("SELECT _id, Name, Doctor FROM Disease", null);
while (cursor.moveToNext()) {
LinearLayout ll2 = new LinearLayout(this);
ll2.setOrientation(LinearLayout.HORIZONTAL);
final TextView tv = new TextView(this);
tv.setId(cursor.getInt(cursor.getColumnIndex("_id")));
tv.setLayoutParams(new LayoutParams(LayoutParams.WRAP_CONTENT, LayoutParams.WRAP_CONTENT));
String disease = cursor.getString(cursor.getColumnIndex("Name"));
String doctor = cursor.getString(cursor.getColumnIndex("Doctor"));
tv.setText(disease +"\nВрач: "+ doctor);
ll2.addView(tv);
ImageButton edit = new ImageButton(this);
edit.setId(cursor.getInt(cursor.getColumnIndex("_id")));
edit.setClickable(true);
edit.setOnClickListener(new OnClickListener() {
@Override
public void onClick(View v) {
// TODO Auto-generated method stub
//TextView tv2 = tv;
Intent i3 = new Intent(RecipesActivity.this, AddEditRecipesActivity.class);
i3.addFlags(EditingRecipe);
i3.putExtra(RecipeId, v.getId());
//i3.putExtra(EditRecipe, tv2.getText().toString());
//startActivityForResult(i3, EditingRecipe);
startActivity(i3);
}
});
edit.setBackgroundResource(R.drawable.edit);
ll2.addView(edit);
ImageButton go = new ImageButton(this);
go.setId(cursor.getInt(cursor.getColumnIndex("_id")));
go.setClickable(true);
go.setBackgroundResource(R.drawable.go);
go.setOnClickListener(new OnClickListener() {
@Override
public void onClick(View v) {
Intent i3 = new Intent(RecipesActivity.this, MedicineActivity.class);
i3.putExtra(ShowRecipe, v.getId());
RecipesActivity.this.startActivity(i3);
}
});
ll2.addView(go);
ll.addView(ll2);
}
}
}
感谢。
答案 0 :(得分:0)
检查游标对象是不是NULL,然后只进去...
if(cursor !=null){
while (cursor.moveToNext()) {
.
.
.
}
}