无法显示CustomDialog

时间:2012-02-28 08:33:46

标签: 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的上下文。如果不是活动背景,我只是不知道该传递什么。

收到以下例外:

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

1 个答案:

答案 0 :(得分:2)

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