1
我有類顯示自定義對話框的Android輸入對話框
public class Add_Category_Dialog {
public String inputed_value;
private Context context;
public Add_Category_Dialog(Context context){
this.context=context;
}
public void showDialog(){
AlertDialog.Builder alert = new AlertDialog.Builder(context);
alert.setTitle("Title");
alert.setMessage("Message");
final EditText input = new EditText(context);
alert.setView(input);
alert.setPositiveButton("Ok", new DialogInterface.OnClickListener() {
public void onClick(DialogInterface dialog, int whichButton) {
inputed_value = input.getText().toString();
}
});
alert.setNegativeButton("Cancel", new DialogInterface.OnClickListener() {
public void onClick(DialogInterface dialog, int whichButton) {
return;
}
});
alert.show();
}
}
從主要活動呼喚:
@Override
public boolean onOptionsItemSelected(MenuItem item){
switch(item.getItemId()){
case R.id.add_category_item:
Add_Category_Dialog add_dialog=new Add_Category_Dialog(getBaseContext());
add_dialog.showDialog();
addCategory(add_dialog.inputed_value);
return true;
}
return false;
}
在模擬器運行時錯誤在運行時occures,logcat的:
android.view.WindowManager $ BadTokenException:無法添加窗口 - 標記null不是一個應用程序
UPD現在我有sqlite的錯誤代碼19,約束失敗
private void addCategory(String string){
SQLiteDatabase db=recipes.getWritableDatabase();
ContentValues values=new ContentValues();
values.put(CATEGORY_NAME, string);
db.insertOrThrow(CATEGORY_TABLE, null, values);
}