我正在開發一個android應用程序。在我的應用我使用的LinearLayout類,我想開一個按鈕對話框click.So我寫我的如下代碼在線性佈局類上顯示對話框
public class LeftSlide extends LinearLayout {
private Context context;
Button clickMeBtn;
public LeftSlide(Context context) {
super(context);
init(context);
}
public LeftSlide(Context context, AttributeSet attrs) {
super(context, attrs);
init(context);
}
private void init(Context context) {
this.context = context;
LayoutInflater inflater = (LayoutInflater) context
.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
layout = (RelativeLayout) inflater.inflate(R.layout.left_slide, null);
layout.setLayoutParams(new LayoutParams(LayoutParams.MATCH_PARENT,
LayoutParams.WRAP_CONTENT));
addView(layout);
clickMeBtn= (Button) layout.findViewById(R.id.clickme);
clickMeBtn.setOnClickListener(new OnClickListener() {
@Override
public void onClick(View v) {
// TODO Auto-generated method stub
showActionSheet();
}
});
下面是對話
public void showActionSheet() {
final Dialog myDialog = new Dialog(context, R.style.CustomDialogTheme);
// final Dialog myDialog = new Dialog(MyBizcard.this,
// R.style.CustomTheme);
myDialog.setContentView(R.layout.pending_dialog);
myDialog.show();
myDialog.getWindow().setGravity(Gravity.CENTER);
}
當代碼我在執行應用程序我得到異常
:android.view.WindowManager $ BadTokenException:無法添加窗口 - 令牌null不是一個應用程序
我知道錯誤是因爲以下行
final Dialog myDialog = new Dialog(context, R.style.CustomDialogTheme);
我必須使用而不是上下文嗎? 我必須完成它的任何way.Please幫助我的朋友
使用undelaying活動的上下文替代 – Techfist
@ Techfist ...我該怎麼做?..您可以給代碼片段嗎? – sarath