【问题标题】:Android input dialog安卓输入对话框
【发布时间】:2010-10-07 18:58:18
【问题描述】:

我有显示自定义对话框的类

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;
}

在模拟器运行时出现运行时错误,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);
}

【问题讨论】:

    标签: android sqlite input dialog


    【解决方案1】:

    尝试将getBaseContext() 替换为this

    【讨论】:

      猜你喜欢
      • 2012-01-10
      • 2013-02-08
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多