0

我尝试将我的应用程序从 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);
}

}
    }

谢谢。

4

1 回答 1

0

检查游标对象不是 NULL ,然后只进去......

if(cursor !=null){
while (cursor.moveToNext()) {
.
.
.
}
}
于 2013-08-20T07:07:39.267 回答