【问题标题】:NumberFormatException: why is 12 an invalid integer?NumberFormatException:为什么 12 是无效整数?
【发布时间】:2018-04-25 07:18:03
【问题描述】:
FATAL EXCEPTION: main
                                               Process: ---, PID: 26657
                                               java.lang.NumberFormatException: Invalid int: "12"

这是我遇到的异常,我不知道为什么,因为 12 似乎是一个正常的整数,而我之前没有遇到过这个问题。它今天才开始,我什至没有接触有关的活动。我什至检查了引号是否是字符串的一部分,但它们不是。为什么这不起作用。这是上下文:

AsyncTask<Uri, Void, Void> asyncTask = new AsyncTask<Uri, Void, Void>() {
        @Override
        protected void onPreExecute() {
            super.onPreExecute();
            loginButtonLayout.setVisibility(View.GONE);
            bar.setVisibility(View.VISIBLE);
        }

        @Override
        protected void onPostExecute(Void aVoid) {
            super.onPostExecute(aVoid);
            ans = ans.replace(" ", "");
            ans = ans.replace("\"", "");
            Log.d("test2", ans);
            if (!(ans.equals("false")) || !(ans.equals(""))) {
                ans = ans.replace("\"", "");
                Intent intent = new Intent(LoginActivity.this, MainActivity.class);
                SharedPreferences.Editor editor = (getBaseContext().getSharedPreferences("USER_DATA", Context.MODE_PRIVATE)).edit();
                editor.putBoolean("login", true);
                int fahrer = Integer.parseInt(ans);
                editor.putInt("fahrerId", fahrer);
                editor.clear().apply();
                ((Alpacar) getApplication()).setLoginState(true);
                ((Alpacar) getApplication()).setFahrerId(Integer.parseInt(ans));
                Toast.makeText(getBaseContext(), "Login successful", Toast.LENGTH_LONG).show();
                finish();
                startActivity(intent);
            } else {
                Toast.makeText(getBaseContext(), "Login failed", Toast.LENGTH_LONG).show();
            }
        }

        @Override
        protected Void doInBackground(Uri... uris) {
            URL url;
            url = null;
            try {
                url = new URL(uri.toString());
            } catch (MalformedURLException e) {
                e.printStackTrace();
            }
            if (url == null) {
                Log.e("MainActivity.java", "Error creating URL");
                return null;
            }


            // new try
            HttpURLConnection urlConnection = null;
            InputStream inputStream = null;
            try {
                urlConnection = (HttpURLConnection) url.openConnection();
                urlConnection.setReadTimeout(10000 /* milliseconds */);
                urlConnection.setConnectTimeout(15000 /* milliseconds */);
                urlConnection.setRequestMethod("GET");
                urlConnection.connect();
                if (urlConnection.getResponseCode() == 200) {
                    inputStream = urlConnection.getInputStream();
                    ans = readFromStream(inputStream);
                    Log.d("test", ans);
                } else {
                    Log.e("QueryUtils", "Error response code: " + urlConnection.getResponseCode());
                }

            } catch (ProtocolException e) {
                Log.e("QueryUtils", "Problem with the protocol", e);
            } catch (IOException e) {
                Log.e("QueryUtils", "Problem establishing the connection", e);

            } finally {
                if (urlConnection != null) {
                    urlConnection.disconnect();
                }
            }
            return null;
        }
    };

问题发生在 onPostExecute...

【问题讨论】:

标签: android exception integer


【解决方案1】:

您的数据包含“12”之前的 UTF8 BOM 字节 ef bb bf,并且它们不会在 logcat 中以可见的方式打印。 (您可以通过将异常消息粘贴到 hexdump 来检查。)这些字节搞砸了到整数的转换。

修复发出该值的代码,或让您的代码删除 BOM 字节。例如,见Byte order mark screws up file reading in Java

【讨论】:

  • 为什么这个ef bb bf值会输入字符串?
【解决方案2】:

尝试使用 Integer.valueOf(ans.toString())

【讨论】:

猜你喜欢
  • 2012-06-29
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2017-09-24
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多