NumberFormatException:无效的int:res / drawable-xhdpi-v4 / ic_launcher_background.png

时间:2018-08-24 05:23:20

标签: android android-resources numberformatexception

每次我尝试运行我的应用程序时,都会发生此错误。当我检查 Logcat 时,出现以下错误:

01-01 01:57:04.260 648-942/? E/SurfaceFlinger: ro.sf.lcd_density must be defined as a build property
01-01 01:57:08.390 3836-3836/payments.banamex.citi.com.banamexpayments E/Trace: error opening trace file: No such file or directory (2)
01-01 01:57:09.000 3836-3836/payments.banamex.citi.com.banamexpayments E/AndroidRuntime: FATAL EXCEPTION: main
    java.lang.RuntimeException: Unable to create application com.example.infonet.emonitor.ControlApplication: java.lang.NumberFormatException: Invalid int: "res/drawable-xhdpi-v4/ic_launcher_background.png"
        at android.app.ActivityThread.handleBindApplication(ActivityThread.java:4394)
        at android.app.ActivityThread.access$1300(ActivityThread.java:141)
        at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1294)
        at android.os.Handler.dispatchMessage(Handler.java:99)
        at android.os.Looper.loop(Looper.java:137)
        at android.app.ActivityThread.main(ActivityThread.java:5041)
        at java.lang.reflect.Method.invokeNative(Native Method)
        at java.lang.reflect.Method.invoke(Method.java:511)
        at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:793)
        at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:560)
        at dalvik.system.NativeStart.main(Native Method)
     Caused by: java.lang.NumberFormatException: Invalid int: "res/drawable-xhdpi-v4/ic_launcher_background.png"
        at java.lang.Integer.invalidInt(Integer.java:138)
        at java.lang.Integer.parse(Integer.java:375)
        at java.lang.Integer.parseInt(Integer.java:366)
        at java.lang.Integer.parseInt(Integer.java:332)
        at com.example.infonet.emonitor.ControlApplication.onCreate(ControlApplication.java:20)
        at android.app.Instrumentation.callApplicationOnCreate(Instrumentation.java:1000)
        at android.app.ActivityThread.handleBindApplication(ActivityThread.java:4391)
        at android.app.ActivityThread.access$1300(ActivityThread.java:141) 
        at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1294) 
        at android.os.Handler.dispatchMessage(Handler.java:99) 
        at android.os.Looper.loop(Looper.java:137) 
        at android.app.ActivityThread.main(ActivityThread.java:5041) 
        at java.lang.reflect.Method.invokeNative(Native Method) 
        at java.lang.reflect.Method.invoke(Method.java:511) 
        at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:793) 
        at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:560) 
        at dalvik.system.NativeStart.main(Native Method) 
01-01 01:57:12.920 648-749/? E/SurfaceFlinger: ro.sf.lcd_density must be defined as a build property
01-01 01:57:15.620 1905-1905/com.android.launcher E/emuglGLESv2_enc: a vertex attribute index out of boundary is detected. Skipping corresponding vertex attribute.

下面是 ControlApplication.java

ControlApplication.java:

package com.example.infonet.emonitor;

import android.app.Application;
import android.content.Context;
import android.util.Log;
import com.example.infonet.emonitor.util.Waiter;

public class ControlApplication extends Application {
    private static int IDLE_DELAY_MINUTES = 2;
    private static final String TAG = ControlApplication.class.getName();
    String app_timeout = "";
    private Context mContext;
    private Waiter waiter;

    public void onCreate() {
        super.onCreate();
        Log.d(TAG, "Starting application" + toString());
        this.app_timeout = getResources().getString(C0105R.string.app_timeout);
        if (this.app_timeout != null) {
            IDLE_DELAY_MINUTES = Integer.parseInt(this.app_timeout);
        }
        Log.d(TAG, "Application TimeOut " + IDLE_DELAY_MINUTES + " Minutes");
        this.mContext = getBaseContext();
        this.waiter = new Waiter(this.mContext, (long) ((IDLE_DELAY_MINUTES * 60) * 1000));
        this.waiter.start();
    }

    public void touch() {
        this.waiter.touch();
    }
}

请,所有这些错误是什么意思。我已经尝试了所有可能的方法来纠正问题,但无济于事。

1 个答案:

答案 0 :(得分:0)

根据您的堆栈跟踪

Integer.parseInt("res/drawable-xhdpi-v4/ic_launcher_background.png")

您已写

Integer.parseInt(this.app_timeout);

R.drawable.ic_laucher_background

这不是从文件中获取资源ID的方法。

您可以简单地引用getResources().getString(C0105R.string.app_timeout) .equals("res/drawable-xhdpi-v4/ic_launcher_background.png"); ,如果已经打算这样做,它已经是一个整数,但是似乎您正在尝试获取超时值,而不是图像。


具体来说,这会得出正确的结果

C0105R

我不知道app_timeout应该是什么,但是看起来很可疑


基本上,如果您不打算发生这种情况,则有几种选择

  1. 您需要更正strings.xml或该C0105R文件中./gradlew clean的值
  2. 您需要从终端运行<integer>并重建应用程序
  3. 将您的资源更改为<string>,而不是public static int
  4. 将超时存储为{{1}}而不是XML值