开发者

Android input dialog

I have class for showing custom dialog

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

    开发者_如何学C  alert.show();  
}

}

Calling from main activity:

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

When running in emulator runtime error occures, LogCat:

android.view.WindowManager$BadTokenException: Unable to add window -- token null is not for an application

UPD Now I have sqlite error code 19, constraint failed

 private void addCategory(String string){
    SQLiteDatabase db=recipes.getWritableDatabase();
    ContentValues values=new ContentValues();
    values.put(CATEGORY_NAME, string);
    db.insertOrThrow(CATEGORY_TABLE, null, values);
}


Try replacing getBaseContext() with this.

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜