java NullPointerException:尝试对空对象引用调用虚拟方法?

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/41411463/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-11-03 05:51:10  来源:igfitidea点击:

NullPointerException: Attempt to invoke virtual method on a null object reference?

javaandroidnullpointerexception

提问by Carlton

I get a NUllPointerException when trying to invoke a method on a button:

尝试调用按钮上的方法时出现 NUllPointerException:

private FButton fButton1;
...
...
fButton1 = (FButton) findViewById(R.id.FButton1);
fButton1.setButtonColor(ContextCompat.getColor(this, fbutton_color_turquoise));    //Exception here

I am using this library hereand I don't get why I get the exception?

我在这里使用这个库,但我不明白为什么会出现异常?

ERROR LOG:

错误日志:

FATAL EXCEPTION: main
                                                   Process: com.user.app, PID: 11209
                                                   java.lang.RuntimeException: Unable to start activity ComponentInfo{com.user.app/com.user.app.activities.StartUpScreen}: java.lang.NullPointerException: Attempt to invoke virtual method 'void info.hoang8f.widget.FButton.setButtonColor(int)' on a null object reference
                                                       at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2416)
                                                       at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2476)
                                                       at android.app.ActivityThread.-wrap11(ActivityThread.java)
                                                       at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1344)
                                                       at android.os.Handler.dispatchMessage(Handler.java:102)
                                                       at android.os.Looper.loop(Looper.java:148)
                                                       at android.app.ActivityThread.main(ActivityThread.java:5417)
                                                       at java.lang.reflect.Method.invoke(Native Method)
                                                       at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:726)
                                                       at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:616)
                                                    Caused by: java.lang.NullPointerException: Attempt to invoke virtual method 'void info.hoang8f.widget.FButton.setButtonColor(int)' on a null object reference
                                                       at com.user.app.activities.StartUpScreen.onCreate(StartUpScreen.java:44)
                                                       at android.app.Activity.performCreate(Activity.java:6237)
                                                       at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1107)
                                                       at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2369)
                                                       at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2476)?
                                                       at android.app.ActivityThread.-wrap11(ActivityThread.java)?
                                                       at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1344)?
                                                       at android.os.Handler.dispatchMessage(Handler.java:102)?
                                                       at android.os.Looper.loop(Looper.java:148)?
                                                       at android.app.ActivityThread.main(ActivityThread.java:5417)?
                                                       at java.lang.reflect.Method.invoke(Native Method)?
                                                       at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:726)?
                                                       at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:616)?

my layout.xml where FButton is:

我的 layout.xml 其中 FButton 是:

<info.hoang8f.widget.FButton
    fbutton:buttonColor="@color/fbutton_color_turquoise"
    fbutton:shadowColor="@color/fbutton_color_green_sea"
    fbutton:shadowEnabled="true"
    fbutton:shadowHeight="5dp"
    android:text="Celcius"
    android:textColor="#FFF"
    fbutton:cornerRadius="5dp"
    android:layout_marginTop="8dp"
    app:layout_constraintTop_toBottomOf="@+id/imageButton3"
    android:layout_marginLeft="8dp"
    app:layout_constraintLeft_toLeftOf="parent"
    android:layout_height="wrap_content"
    android:layout_marginStart="8dp"
    android:layout_width="match_parent"
    android:id="@+id/FButton1"
    app:layout_constraintRight_toLeftOf="@+id/FButton2"
    android:layout_marginRight="8dp"
    android:layout_marginEnd="8dp"
    app:layout_constraintHorizontal_bias="1.0" />

My ids.xml file that I had to create, since android did not recoginze the id:

我必须创建的 ids.xml 文件,因为 android 没有识别 id:

<?xml version="1.0" encoding="utf-8"?>
<resources>
    <item name="FButton1" type="id" />
</resources>

回答by Vyacheslav

It seems your fButton1is null. Just check where you call findViewById and check your xml file whether this element really exists

看来你fButton1是空的。只需检查您调用 findViewById 的位置并检查您的 xml 文件该元素是否真的存在

回答by gautam kumar

Your error is in setting Button color, use this to change the color of Button programmatically.

您的错误在于设置按钮颜色,使用它以编程方式更改按钮的颜色。

setBackgroundColor(getResources().getColor(R.color.holo_light_green))

One thing more, Starting from Android Support Library-23 getColor method has been deprecated, so instead use this

还有一件事,从 Android Support Library-23 开始,不推荐使用 getColor 方法,所以改用这个

ContextCompat.getColor(context, R.color.your_color);

Thus to support all versions use this,

因此为了支持所有版本使用这个,

public static final int getColor(Context context, int id) 
{
    final int version = Build.VERSION.SDK_INT;
    if (version >= 23) {
        return ContextCompatApi23.getColor(context, id);
    } 
    else {
        return context.getResources().getColor(id);
    }
}