Android 无法显示自定义对话框

Android 无法显示自定义对话框,android,dialog,Android,Dialog,编辑 我设法解决了自己的问题 new AlertDialog.Builder( getParent() ) 这会将正确的上下文传递给方法 编辑 很抱歉,我又用一些平常的问题来打扰大家,但我已经为这个对话挣扎了一天,这是我迄今为止最接近的一次对话: public class CallForwardActivity extends ListActivity { String[] callforwardLabels = {"Viderestillinger", "Altid", "Optaget"

编辑

我设法解决了自己的问题

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;
}
}
编辑:原来我的问题可能会更严重。我将尝试在下面包含更多相关代码

选项卡控制活动:

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;
}
}
公共类设置活动组扩展活动组
{
//将其保存在一个静态变量中,以使所有嵌套活动都可以访问它,并允许它们操纵视图
公共静态设置活动组组;
//如果希望“后退”按钮正常工作,则需要跟踪历史记录。如果您的活动需要大量内存,请不要使用此按钮。
私家侦探史;
@凌驾
创建时受保护的void(Bundle savedInstanceState)
{
super.onCreate(savedInstanceState);
//分配历史记录
this.history=new ArrayList();
//集合组
组=此;
//启动根(第一个)活动
Intent myIntent=new Intent(this,setingsactivity.class);//更改为ActivityGroup的第一个活动
myIntent.addFlags(Intent.FLAG\u ACTIVITY\u CLEAR\u TOP);
替换视图(“设置活动”,myIntent);
}
/*
*用新活动替换该活动,并将以前的活动添加到历史记录中
*/
public void ReplaceView(字符串pId,Intent pIntent)
{
Window Window=getLocalActivityManager().startActivity(pId,pIntent);
视图=(窗口!=null)?window.getDecorView():null;
//将旧活动添加到历史记录中
添加(查看);
//将内容视图设置为新活动
setContentView(视图);
}
/*
*从上一个活动返回,如果没有上一个活动,则关闭应用程序
*/
公众退票()
{
if(history.size()>1)
{
//从历史记录中删除以前的活动
history.remove(history.size()-1);
//参加活动
View=history.get(history.size()-1);
Activity=(Activity)view.getContext();
//“黑客”用于确定何时从以前的活动返回
//如果返回时不需要重新绘制活动,则无需执行此操作
activity.onWindowFocusChanged(true);
//将内容视图设置为新活动
setContentView(视图);
}
其他的
{
//关闭应用程序
完成();
}
}
/*
*覆盖后退按钮
*/
@凌驾
public void onBackPressed()
{
//如果历史记录不是空的,请返回一个
//如果历史记录为空,请关闭应用程序
SettingsActivityGroup.group.back();
返回;
}
}

您需要使用
CallForwardActivity。这是
而不是ctx。当涉及到用户界面时,传递活动实例总是比传递上下文好。

对不起-完全忘记了。它位于edit.use getApplicationContext()中,而不是AlertDialog.Builder()中的ctx。我尝试了ctx=this.getApplicationContext()和AlertDialog.Builder(getApplicationContext())。两者产生相同的异常。@litemode是否使用Tabhost?是。此活动由选项卡主机中的ActivityGroup(SettingsActivityGroup)启动。这是一个问题吗?当我尝试您的建议时,应用程序正在引发此异常:02-28 09:52:05.837:E/AndroidRuntime(21796):android.view.WindowManager$BadTokenException:无法添加窗口--token android.app.LocalActivityManager$LocalActivityRecord@40f0abc0无效;您的活动正在运行吗?@litemode。。创建对话框时,请确保此活动不在后台。我是否应该传递ActivityGroup的上下文?还是tabhost?@litemode。。你把这个方法放在所有标签上。。。或者在某个地方创建commom方法来获取活动实例……所以它与我在TabHost中使用ActivityGroups无关?您希望在所有选项卡活动中使用什么方法?为什么在所有的活动中都需要它,因为我只在这一个活动中需要它?
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;
}
}