【问题标题】:button and bundle with android to change text, application crash按钮并与 android 捆绑以更改文本,应用程序崩溃
【发布时间】:2013-10-23 11:19:10
【问题描述】:
        buttCalc.setOnClickListener(new OnClickListener() {
        public void onClick(View arg0) {

        EditText val1 = (EditText)findViewById(R.id.edit1);
        EditText val2 = (EditText)findViewById(R.id.edit2);
        EditText risultato = (EditText)findViewById(R.id.risultato);
        Bundle bundle = new Bundle();
            bundle.putString("Valore2", val2.getText().toString());
        risultato.setText(bundle.getString("Valore2"));

大家好,当我单击该按钮时,我的应用程序在 vm 中崩溃,我想: 1 点击按钮 2 risultato 文本框 = val2 文本

先谢谢你

【问题讨论】:

  • 设置文本为什么需要捆绑? risultato.setText(val2.getText().toString())就够了
  • 按钮的 findviewbyid 完成了吗?
  • 你有一些 ID 问题,你的代码工作正常。
  • 需要更多代码+ logcat..
  • @kalyanpvs 我不认为它在按钮单击中进行编辑文本初始化会导致 NPE。并且 setText 可以取 null grepcode.com/file/repository.grepcode.com/java/ext/…

标签: android button textbox crash bundle


【解决方案1】:

来自您的 cmets

 java.lang.ClassCastException: android.widget.TextView cannot be cast to android.widget.EditText

您拥有的是一个文本视图,但您正在转换为 edittext。所以改变你的初始化

如果您在 xml 中有一个 id 为 textView1 的 textview,则初始化如下

   TextView tv= (TextView)findViewById(R.id.textview1);

你得到 ClassCastException,因为你正在将 textview 转换为 edittext

同时在 onClick 之外初始化你的视图

【讨论】:

  • @myself 欢迎您在 onClick 之外初始化视图,无需每次单击按钮时都进行初始化
【解决方案2】:

在您的 xml 布局中,以下 id 之一

(R.id.edit1);
(R.id.edit2);
(R.id.risultato);

属于一个Textview。

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2017-12-22
    • 2017-02-14
    • 1970-01-01
    相关资源
    最近更新 更多