0
當我在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));
}
'myActivity.setUI()''myActivity'實例不是用戶(或您)正在看到的活動。這不是Android問題,而是基本的Java問題。如果您是Android和Java的初學者,請正式參加Android版開始的gettting。 https://developer.android.com/training/index.html這些內容有寶貴的教程和技巧。 – ytRino 2014-10-30 04:50:48