I am trying to create an application with a widget. When the user places the widget on the desktop a listview should come up with a list of items. The user selects an item then the widget is created with the respective text related to that item. I thought I should do this by showing a dialog in the Service but it throws me
Caused by: android.view.WindowManager$BadTokenException: Unable to add window -- token null is not for an application
to the dialog_newitem.show(); line. For simplicity I am using now a simple alertdialog.
Is it the way to do this? I haven't found anyhing about this on the net.
public class UpdateWidgetService extends Service {
private static final String LOG = "de.vogella.android.widget.example";
public static String ACTION_WIDGET_CONFIGURE = "ConfigureWidget";
public static String ACTION_WIDGET_RECEIVER = "ActionReceiverWidget";
String value;
Dialog dialog_newitem;
EditText et_newitem;
@Override
public void onStart(Intent intent, int startId) {
Toast.makeText(this, "UpdateWidgetService", Toast.LENGTH_SHORT).show();
dialog_newitem = new Dialog(this); //I tried UpdateWidgetService.this, too
dialog_newitem.setContentView(R.layout.dialog_productlists_grp_capitalized);
dialog_newitem.setTitle("Select");
dialog_newitem.setCancelable(true);
et_newitem = (EditText) dialog_newitem.findViewById(R.id.et_item_name);
Button btn_Save = (Button) dialog_newitem.findViewById(R.id.btn_save_pr);
btn_Save.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
value = et_newitem.getText().toString();
}
});
Button btn_Cancel = (Button) dialog_newitem.findViewById(R.id.btn_cancel_pr);
btn_Cancel.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
dialog_newitem.dismiss();
}
});
dialog_newitem.show(); //error
Toast.makeText(this, "value: " + value, Toast.LENGTH_SHORT).show();
}
@Override
public IBinder onBind(Intent intent) {
return null;
}
}
I have used this alertdialog in some other part of the code, and there it is working fine. I think it has something to do with the service.