无法显示CustomDialog

无法显示CustomDialog

编辑

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

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


这是激活我的CallForward活动的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 - 无法显示CustomDialog,我们在Stack Overflow上找到一个类似的问题:https://stackoverflow.com/questions/9478671/

10-09 01:34