我想从单独的类中设置textfield

时间:2014-10-30 03:55:10

标签: android

当我在SQLiteOpenHelper中的onUpgrade中使用setUI()时,会显示NullPointerException。 为什么是这样? 我想在单独的课程中显示文本字段,所以如果有其他好方法,请告诉我。 (例如LayoutInflater?但是,当我使用LayoutInflater时,没有显示任何内容。)

public class MyActivity extends Activity {

@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_my);

       ・
       ・
       ・

public void setUI(String fileName,String word,String yen,String category){
        TextView categoryText=(TextView)findViewById(R.id.category);
        categoryText.setText(category);
        Bitmap image=loadImage(fileName,this);
        ImageView imageView=(ImageView)findViewById(R.id.image);
        imageView.setImageBitmap(image);
        TextView wordText=(TextView)findViewById(R.id.word);
        wordText.setText(word);
        TextView yenText=(TextView)findViewById(R.id.yen);
        yenText.setText(yen);
}

private static class DBHelper extends SQLiteOpenHelper {

    public DBHelper(Context context) {
        super(context,DB_NAME,null,4);
    }

    @Override
    public void onCreate(SQLiteDatabase db) {
        db.execSQL("create table if not exists "+DB_TABLE+"(id integer primary key autoincrement,image text,word text, yen text)");
    }

    @Override
    public void onUpgrade(SQLiteDatabase db, int oldVersion,int newVersion) {
        db.execSQL("create table if not exists "+CATEGORY_DB_TABLE+"(id integer primary key autoincrement, category text)");
        db.execSQL("insert into category(category) values('カテゴリー1')");
        Cursor c=db.rawQuery("select * from test inner join category on test.id = category.id",null);
        c.moveToFirst();
        MyActivity myActivity=new MyActivity();
        myActivity.setUI(c.getString(1),c.getString(2),c.getString(3),c.getString(5));
    }

1 个答案:

答案 0 :(得分:0)

您在myActivity.setUI课程即时后立即致电MyActivity。当你的view不会被夸大时。

相反,您应尝试将所需数据存储在某个变量的MyActivity中,并在onCreate调用后检查其在setContentView方法中的存在,然后调用您的{{1 }} 方法。此外,只有当您的活动显示在屏幕上时才会调用此项。

相关问题