服务中的数字格式异常

时间:2018-10-18 13:47:17

标签: java android android-studio service numbers

代码

    final EditText wid = (EditText) myview.findViewById(R.id.width);
    final EditText hie = (EditText) myview.findViewById(R.id.height);
    Button mSave = (Button) myview.findViewById(R.id.save);
    Button mCancel = (Button) myview.findViewById(R.id.cancel);

    final String mWidth = wid.getText().toString();
    final String mHeight = hie.getText().toString();

    mSave.setOnClickListener(new View.OnClickListener() {
        @Override
        public void onClick(View v) {
            if (Integer.parseInt(mWidth)>= 500 && Integer.parseInt(mWidth) <= 1000 &&
                    Integer.parseInt(mHeight) >= 500 && Integer.parseInt(mHeight) <= 1500) {
                Intent intent = new Intent(ResizeService.this, UsersService.class);
                intent.putExtra("Width", mWidth);
                intent.putExtra("Height", mHeight);
                startService(intent);
            } else {
                Toast.makeText(getApplicationContext(), "Check whether the data you entered matches the requirements", Toast.LENGTH_LONG).show();
            }
        }
    });

输入的数字是不带小数的整数。但是仍然出现错误

  

java.lang.NumberFormatException:无效的int:“”                                                                                      在java.lang.Integer.invalidInt(Integer.java:138)                                                                                      在java.lang.Integer.parseInt(Integer.java:358)                                                                                      在java.lang.Integer.parseInt(Integer.java:334)                                                                                      在   com.appmaster.akash.messageplus.Services.ResizeService $ 1.onClick(ResizeService.java:57)                                                                                      在android.view.View.performClick(View.java:5215)                                                                                      在android.view.View $ PerformClick.run(View.java:21193)                                                                                      在android.os.Handler.handleCallback(Handler.java:742)                                                                                      在android.os.Handler.dispatchMessage(Handler.java:95)                                                                                      在android.os.Looper.loop(Looper.java:157)                                                                                      在android.app.ActivityThread.main(ActivityThread.java:5571)                                                                                      在java.lang.reflect.Method.invoke(本机方法)                                                                                      在   com.android.internal.os.ZygoteInit $ MethodAndArgsCaller.run(ZygoteInit.java:745)                                                                                      在com.android.internal.os.ZygoteInit.main(ZygoteInit.java:635)

editTexts也不是空的...正在输入数字,但仍然出现错误

此外,这还在服务中... 错误在if语句的行上

2 个答案:

答案 0 :(得分:1)

尝试移动

    def set_defaultfile(self):

        file = "C:/Users/PycharmProjects/MindNotez/Note.csv"
        df_content = {"title": [], "author": [], "year": [], "others": [], "note": []}
        try:
            with open(file, "r") as test:
                f_reader = csv.DictReader(test, delimiter=',')
                headers = f_reader.fieldnames
                if  headers == df_content:

                print("Headers match!")
        except FileNotFoundError:
            print("File created!")
            with open(file, "a+") as f:
                f_writer = csv.DictWriter(f, fieldnames=df_content)
                f_writer.writeheader()
                f.close()

        file_content = "C:/Users/PycharmProjects/MindNotez/Note.csv"
        return file_content

onclick内。这样,您可以在单击时(而不是在开始时)获得值。

答案 1 :(得分:0)

代替

    final String mWidth = wid.getText().toString();
    final String mHeight = hie.getText().toString();

我会

    final int mWidth = wid.getText();
    final int mHeight = hie.getText();

然后在xml中,将EditText的inputType设置为仅数字。

然后删除Integer.parseInt,将其放置在onClick内,因为您要按下一个按钮来获取EditText内的值,如果您尝试先获取值,则变量{ {1}}和mWidth将返回null,因为单击按钮之前它们中没有东西。