活动首次启动时无法启动创建弹出窗口

时间:2011-08-22 07:08:27

标签: android

protected void onCreate(Bundle savedInstanceState) {
    // TODO Auto-generated method stub
    super.onCreate(savedInstanceState);
    setContentView(R.layout.question_layout);
    show_popup();
}

    private void show_popup(){
    LayoutInflater lf = (LayoutInflater)getSystemService(Context.LAYOUT_INFLATER_SERVICE);
    View m_view = lf.inflate(R.layout.popup_question, null);
    m_popup_window = new PopupWindow(m_view,500,150,false);
    m_popup_window.showAtLocation(m_full_page, Gravity.CENTER, 0, 0);
}

当我点击任何按钮来调用show_popup时它工作正常但是当我想要show_popup()调用onCreate()时它不起作用。我在这样的logcat上有一个错误显示

08-22 13:57:36.682: ERROR/AndroidRuntime(21860):     at tesingimage.com.testingimagemain.show_pupup(testingimagemain.java:41)
08-22 13:57:36.682: ERROR/AndroidRuntime(21860):     at tesingimage.com.testingimagemain.onCreate(testingimagemain.java:23)
08-22 13:57:36.682: ERROR/AndroidRuntime(21860):     at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1047)
请提前帮助我,谢谢!

1 个答案:

答案 0 :(得分:2)

使用Handler对象显示弹出窗口。

Handler hand = new Handler()
{
    @Override
    public void handleMessage(Message msg)
    {
        show_popup();

    }
};

并在onCreate()

中调用处理程序
protected void onCreate(Bundle savedInstanceState) {
    // TODO Auto-generated method stub
    super.onCreate(savedInstanceState);
    setContentView(R.layout.question_layout);
    hand.sendEmptyMessage(0);
}