【问题标题】:Convert Text from EditText to Integer Value Error将文本从 EditText 转换为整数值错误
【发布时间】:2014-08-04 10:00:20
【问题描述】:

所以我目前正在开发我自己的私人应用程序,我只想为我和一些朋友使用它。 好吧,我是德国人,所以我的英语可能不是最好的,希望您能原谅我。

现在我的问题是我想在我的选项菜单中设置当月的预算以跟踪。我通过使用带有按钮的 EditText 来做到这一点。

现在我想将在我的 EditText 中输入的这个字符串保存为一个字符串值和一个整数值,因为我想在我的 MainPage 上的 TextView 中显示该值并使用整数值来计算我得到的当前预算等等。

现在我向你们展示我的代码,希望你们能告诉我它有什么问题。 我试图在与我的 OptionsMenu 相关的 Options 类中获取我的值,然后尝试将值从我的 Options 类中获取到我的 Main 类中。

public class Options extends Activity {
Button GoBack, SetBudget;
private int Budget;
String BudgetString = "";
EditText BudgetEdit;
@Override
protected void onCreate(Bundle savedInstanceState) {
    // TODO Auto-generated method stub
    super.onCreate(savedInstanceState);
    setContentView(R.layout.options);
    GoBack = (Button) findViewById(R.id.button2);
    GoBack.setOnClickListener(new View.OnClickListener() {

        @Override
        public void onClick(View v) {
            // TODO Auto-generated method stub
            Intent GoBackToMain = new Intent("com.lunarrepublic.STARTINGPOINT");
            startActivity(GoBackToMain);

        }
    });

    SetBudget = (Button) findViewById(R.id.button8);

    SetBudget.setOnClickListener(new View.OnClickListener() {

        @Override
        public void onClick(View v) {
            // TODO Auto-generated method stub
            BudgetEdit = (EditText) findViewById(R.id.editText1);
            BudgetString = EditText.getText();
            //In the Line above this is  the error "Type mismatch. Cannot convert Editable to String
            Budget = Integer.parseInt(BudgetString);

        }
    });
}

但如果我尝试将我的“BudgetString”设置为可编辑,它也不起作用。 GoBack 按钮对于我的问题来说是不必要的,所以你不必仔细阅读它。

所以我希望你们明白我的问题是什么,也许可以帮助我解决它

提前致谢

【问题讨论】:

  • BudgetString = EditText.getText().toString();
  • Editable 实际上不是String。您必须使用toString() 转换为parseInt() 可以接受的数据类型。
  • BudgetString = EditText.getText();,错了,改用你的对象BudgetEdit。还要在 try catch 中保留 Budget = Integer.parseInt(BudgetString); 以捕获任何数字格式异常。

标签: java android android-edittext setvalue


【解决方案1】:

这是错误的

BudgetString = EditText.getText();

使用下面一个

BudgetString = BudgetEdit.getText().toString();

【讨论】:

  • 我的错误消失了,所以我认为这就是答案 :) 非常感谢,但在我验证之前,我必须先修复我的应用程序。好像我昨晚做错了什么,因为在我的加载屏幕消失后,我的应用程序崩溃了。
  • @NhatNienne 快乐发展
  • 非常感谢你,你拯救了我的一天:D 我修复了我的错误 idk 为什么但我的应用程序崩溃了,因为我的主类中有一个未使用的变量和一个未使用的 EditText 参数哈哈
【解决方案2】:

改一下

    SetBudget.setOnClickListener(new View.OnClickListener() {

    @Override
    public void onClick(View v) {
        // TODO Auto-generated method stub
        BudgetEdit = (EditText) findViewById(R.id.editText1);
        BudgetString = EditText.getText();
        //In the Line above this is  the error "Type mismatch. Cannot convert Editable to String
        Budget = Integer.parseInt(BudgetString);

    }
});

   SetBudget.setOnClickListener(new View.OnClickListener() {

    @Override
    public void onClick(View v) {
        // TODO Auto-generated method stub
        BudgetEdit = (EditText) Options.this.findViewById(R.id.editText1);
        BudgetString = BudgetEdit.getText();
        //In the Line above this is  the error "Type mismatch. Cannot convert Editable to String
        Budget = Integer.parseInt(BudgetString);

    }
});

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2020-10-10
    • 1970-01-01
    • 1970-01-01
    • 2012-01-10
    • 1970-01-01
    • 1970-01-01
    • 2013-05-07
    • 1970-01-01
    相关资源
    最近更新 更多