【问题标题】:Dialog method does not work对话框方法不起作用
【发布时间】:2012-12-28 21:12:24
【问题描述】:

我正在尝试创建一个对话框方法,但是当我调用它时,它会导致强制关闭。我的日志猫唯一得到的是:

dalvikvm 线程=1:线程以未捕获的异常退出(组=0x40ab5c08)

致命异常:主要 java.lang.NullPoinerExeption

我确定其余的都是标准的

这是我的代码片段:

 Dialog PercentDialog;

 private void CreateDialog() {
    // TODO Auto-generated method stub
    Context context = Home.this;
    PercentDialog = new Dialog(context);
    //PercentDialog.requestWindowFeature(Window.FEATURE_NO_TITLE);
    PercentDialog.setContentView(R.layout.percent_box);
    Button bOK = (Button) findViewById(R.id.bOK);
    Button bCancel = (Button) PercentDialog.findViewById(R.id.bCancel); 
    EditText etInt = (EditText) PercentDialog.findViewById(R.id.etInt); 
    TextView title = (TextView) PercentDialog.findViewById(R.id.tvTitle);   
    bOK.setOnClickListener(new OnClickListener(){

        public void onClick(View v) {
            // TODO Auto-generated method stub

        }

    });
    bCancel.setOnClickListener(new OnClickListener(){

        public void onClick(View v) {
            // TODO Auto-generated method stub
            PercentDialog.dismiss();
        }

    });

    PercentDialog.show();
}

然后我使用 CreateDialog();调用我的方法,但它被强制关闭。

【问题讨论】:

  • 您的布局中可能没有这些视图。向我们展示您的percent_box 布局代码。顺便说一句,异常跟踪的其余部分不是标准,它指出了问题发生的确切位置。

标签: android methods dialog forceclose


【解决方案1】:

从你现有的代码来看,这一行:

Button bOK = (Button) findViewById(R.id.bOK);

应该是:

Button bOK = (Button) PercentDialog.findViewById(R.id.bOK);

(另请阅读Java naming conventions 声明变量应以小写字母开头。)

【讨论】:

  • 谢谢,我认为这是我没有注意到的粗心错误,或者可能是一个简单的关闭和打开的日食(因为它有时无法正常工作)。无论哪种方式都感谢您和“java命名约定”链接
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 2020-01-21
  • 2013-09-10
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多