Reputation: 6666
EDIT
I managed to solve my own question.
new AlertDialog.Builder( getParent() )
This passes the correct context to the method.
EDIT
I'm sorry to bother everyone again with mundane questions but I have been struggling with this dialog for a day now and this is the closest I've gotten so far:
public class CallForwardActivity extends ListActivity
{
String[] callforwardLabels = {"Viderestillinger", "Altid", "Optaget", "Ingen svar", "Timeout"};
int position;
Context ctx;
static final private int CATEGORY_DETAIL = 1;
@Override
public void onCreate(Bundle savedInstanceState)
{
super.onCreate(savedInstanceState);
ctx = this;
ListView lv = getListView();
lv.setTextFilterEnabled(true);
setListAdapter(new ArrayAdapter<String>(this, R.layout.callforward_items, R.id.callforward_item_text, callforwardLabels));
lv.setOnItemClickListener(new OnItemClickListener() {
public void onItemClick(AdapterView<?> parent, View view,
int position, long id)
{
new AlertDialog.Builder( ctx )
.setTitle( "Viderestilling ved optaget" )
.setMessage( "Viderestilles til" )
.setPositiveButton( "Godkend", new DialogInterface.OnClickListener() {
public void onClick(DialogInterface dialog, int which) {
Log.d( "AlertDialog", "Positive" );
}
})
.setNeutralButton( "Slå fra", new DialogInterface.OnClickListener() {
public void onClick(DialogInterface dialog, int which) {
Log.d( "AlertDialog", "Neutral" );
}
})
.setNegativeButton( "Annullér", new DialogInterface.OnClickListener() {
public void onClick(DialogInterface dialog, int which) {
Log.d( "AlertDialog", "Negative" );
}
} )
.show();
}
});
}
@Override
public void onBackPressed()
{
// Go one back, if the history is not empty
// If history is empty, close the application
SettingsActivityGroup.group.back();
return;
}
}
Now, I think my problem is the context i'm passing the AlertDialog Builder. I just don't know what to pass if not the activitys context.
Receiving the following exception :
02-28 09:34:03.137: E/AndroidRuntime(20622): android.view.WindowManager$BadTokenException: Unable to add window -- token null is not for an application
EDIT: turns out my problem may be further out. I'll try to include more relevant Code below.
TabControlActivity :
public class TabControlActivity extends TabActivity
{
public static final int INSERT_ID = Menu.FIRST;
static TabHost tabHost;
public static TabControlActivity thisCtx;
@Override
public boolean onCreateOptionsMenu(Menu menu) {
MenuInflater inflater = getMenuInflater();
inflater.inflate(R.layout.more_menu, menu);
return true;
}
/** Called when the activity is first created. */
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
thisCtx = this;
CustomDialog cDialog = new CustomDialog(thisCtx);
TabHost tabHost = getTabHost();
TabHost.TabSpec spec; // Resusable TabSpec for each tab
Intent intent; // Reusable Intent for each tab
Resources res = getResources(); // Resource object to get Drawables
// Create and initialize the intent for each activity
final Button logoutButton = (Button) findViewById(R.id.ButtonLogout);
logoutButton.setOnClickListener(new OnClickListener()
{
public void onClick(View v)
{
LoginController loginControl = new LoginController(thisCtx);
Intent intent = new Intent(v.getContext(), LoginActivity.class);
loginControl.ResetUserInfo();
startActivityForResult(intent, 0);
}
});
// Tab(0)
intent = new Intent().setClass(this, FrontpageActivity.class);
spec = tabHost.newTabSpec("frontpage").setIndicator("Forside",
res.getDrawable(R.drawable.icon_frontpage_high)).setContent(intent);
tabHost.addTab(spec);
// Tab(1)
intent = new Intent().setClass(this, ChatActivity.class);
spec = tabHost.newTabSpec("chat").setIndicator("Chat",
res.getDrawable(R.drawable.icon_chat)).setContent(intent);
tabHost.addTab(spec);
//Tab(2)
intent = new Intent().setClass(this, ContactsActivity.class);
spec = tabHost.newTabSpec("contacts").setIndicator("Kontakter",
res.getDrawable(R.drawable.icon_contacts_high)).setContent(intent);
tabHost.addTab(spec);
//Tab(3)
intent = new Intent().setClass(this, SettingsActivityGroup.class);
spec = tabHost.newTabSpec("settings").setIndicator("Indstillinger",
res.getDrawable(R.drawable.icon_settings_high)).setContent(intent);
tabHost.addTab(spec);
tabHost.setCurrentTab(0);
}
public void switchTabSpecial(int tab)
{
tabHost.setCurrentTab(tab);
}
And here is the ActivityGroup that activates my CallForward Activity:
public class SettingsActivityGroup extends ActivityGroup
{
// Keep this in a static variable to make it accessible for all the nested activities, lets them manipulate the view
public static SettingsActivityGroup group;
// Need to keep track of the history if you want the back-button to work properly, don't use this if your activities requires a lot of memory.
private ArrayList<View> history;
@Override
protected void onCreate(Bundle savedInstanceState)
{
super.onCreate(savedInstanceState);
// Allocate history
this.history = new ArrayList<View>();
// Set group
group = this;
// Start root (first) activity
Intent myIntent = new Intent(this, SettingsActivity.class); // Change to the first activity of your ActivityGroup
myIntent.addFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP);
ReplaceView("SettingsActivity", myIntent);
}
/*
* Replace the activity with a new activity and add previous one to history
*/
public void ReplaceView(String pId, Intent pIntent)
{
Window window = getLocalActivityManager().startActivity(pId, pIntent);
View view = (window != null) ? window.getDecorView() : null;
// Add the old activity to the history
history.add(view);
// Set content view to new activity
setContentView(view);
}
/*
* Go back from previous activity or close application if there is no previous activity
*/
public void back()
{
if(history.size() > 1)
{
// Remove previous activity from history
history.remove(history.size()-1);
// Go to activity
View view = history.get(history.size() - 1);
Activity activity = (Activity) view.getContext();
// "Hack" used to determine when going back from a previous activity
// This is not necessary, if you don't need to redraw an activity when going back
activity.onWindowFocusChanged(true);
// Set content view to new activity
setContentView(view);
}
else
{
// Close the application
finish();
}
}
/*
* Overwrite the back button
*/
@Override
public void onBackPressed()
{
// Go one back, if the history is not empty
// If history is empty, close the application
SettingsActivityGroup.group.back();
return;
}
}
Upvotes: 1
Views: 171
Reputation: 13501
You need to use CallForwardActivity.this
instead of ctx. Its always better to pass Activity instance than Context when ever there is user interface involved.
Upvotes: 2