android - 无法显示自定义对话框

标签 android dialog

编辑

我设法解决了我自己的问题。

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 的上下文。如果不是 Activity 上下文,我只是不知道要传递什么。

收到以下异常:

02-28 09:34:03.137: E/AndroidRuntime(20622): android.view.WindowManager$BadTokenException: Unable to add window -- token null is not for an application

编辑:原来我的问题可能更远。我将尝试在下面包含更多相关代码。

标签控制 Activity :

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

这里是激活我的 CallForward Activity 的 ActivityGroup:

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

最佳答案

您需要使用 CallForwardActivity.this 而不是 ctx。当涉及到用户界面时,传递 Activity 实例总是比传递 Context 更好。

关于android - 无法显示自定义对话框,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/9478671/

相关文章:

android - 谷歌应用开发者 : Change Default Language of App

android - 在发布中删除 Android 调试资源

javascript - 对话框应如何向屏幕阅读器用户宣布动态内容?

android - 检测后退按钮但不关闭对话框 fragment

dialog - 如何在 Installshield 2010 中添加安装语言对话框

android - 无法在某些关闭的 Genymotion 设备上运行 .apk

java - 将初始值设置为静态字段

android - 动态处理android中的子菜单项

Android 自定义弹出窗口/对话框

javascript - Jquery Bootstrap Modal...如何从另一个模式对话框中打开不同的模式对话框