РЕДАКТИРОВАТЬ
Мне удалось решить собственный вопрос.
new AlertDialog.Builder( getParent() )
Это передает правильный контекст методу.
РЕДАКТИРОВАТЬ
Извините, что беспокою всех снова мирскими вопросами, но я боролся с этим диалогом в течение дня, и это самое близкое, что я получил до сих пор:
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;
}
}
Теперь, я думаю, что моя проблема в том, что я передаю AlertDialog Builder.Я просто не знаю, что передать, если не контекст действий.
Получение следующего исключения:
02-28 09:34:03.137: E/AndroidRuntime(20622): android.view.WindowManager$BadTokenException: Unable to add window -- token null is not for an application
РЕДАКТИРОВАТЬ: оказывается, моя проблема может быть дальше.Я постараюсь включить более соответствующий код ниже.
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);
}
А вот группа ActivityGroup, которая активирует мою активность CallForward:
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;
}
}