如何处理在空对象引用上调用虚拟方法'void android.view.View.setBackground(Drawable)'

时间:2018-11-11 16:40:41

标签: android nullpointerexception

这是我的源代码,它在第41行接缝了程序引发异常。

公共类LaunchActivity扩展了ACtivity {

//Drawable
private ColorDrawable mBgDrawable;

@Override
protected int getContentLayoutId() {
    return R.layout.activity_launch;
}

@Override
protected void initWidows() {
    super.initWidows();
    //拿到根布局
    View root = findViewById(R.id.activity_launch);
    //获取颜色
    int color = UiCompat.getColor(getResources(),R.color.colorPrimary);
    //创建一个Drawable
    ColorDrawable drawable = new ColorDrawable(color);
    //设置给背景
    root.setBackground(drawable);  **//line 41**
    mBgDrawable = drawable;
}

布局源代码( activity_launch.xml

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout
    xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:app="http://schemas.android.com/apk/res-auto"
    xmlns:tools="http://schemas.android.com/tools"
    android:id="@+id/activity_launch"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:background="@color/colorPrimary"
    android:orientation="vertical"
    tools:context="net.qiujuer.italker.push.LaunchActivity">

    .....

这是错误信息。

(这是否意味着我得到的View(“根”)是空对象?)

FATAL EXCEPTION: main
                  Process: net.qiujuer.italker.push, PID: 3046
                  java.lang.RuntimeException: Unable to start activity ComponentInfo{net.qiujuer.italker.push/net.qiujuer.italker.push.LaunchActivity}: java.lang.NullPointerException: Attempt to invoke virtual method 'void android.view.View.setBackground(android.graphics.drawable.Drawable)' on a null object reference
                      at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2925)
                      at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:3060)
                      at android.app.servertransaction.LaunchActivityItem.execute(LaunchActivityItem.java:78)
                      at android.app.servertransaction.TransactionExecutor.executeCallbacks(TransactionExecutor.java:110)
                      at android.app.servertransaction.TransactionExecutor.execute(TransactionExecutor.java:70)
                      at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1800)
                      at android.os.Handler.dispatchMessage(Handler.java:106)
                      at android.os.Looper.loop(Looper.java:164)
                      at android.app.ActivityThread.main(ActivityThread.java:6649)
                      at java.lang.reflect.Method.invoke(Native Method)
                      at com.android.internal.os.RuntimeInit$MethodAndArgsCaller.run(RuntimeInit.java:493)
                      at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:826)
                   Caused by: java.lang.NullPointerException: Attempt to invoke virtual method 'void android.view.View.setBackground(android.graphics.drawable.Drawable)' on a null object reference
                      at net.qiujuer.italker.push.LaunchActivity.initWidows(LaunchActivity.java:41)
                      at net.qiujuer.italker.common.app.ACtivity.onCreate(ACtivity.java:16)
                      at android.app.Activity.performCreate(Activity.java:7130)
                      at android.app.Activity.performCreate(Activity.java:7121)
                      at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1262)
                      at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2905)
                      at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:3060) 
                      at android.app.servertransaction.LaunchActivityItem.execute(LaunchActivityItem.java:78) 
                      at android.app.servertransaction.TransactionExecutor.executeCallbacks(TransactionExecutor.java:110) 
                      at android.app.servertransaction.TransactionExecutor.execute(TransactionExecutor.java:70) 
                      at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1800) 
                      at android.os.Handler.dispatchMessage(Handler.java:106) 
                      at android.os.Looper.loop(Looper.java:164) 
                      at android.app.ActivityThread.main(ActivityThread.java:6649) 
                      at java.lang.reflect.Method.invoke(Native Method) 
                      at com.android.internal.os.RuntimeInit$MethodAndArgsCaller.run(RuntimeInit.java:493) 
                      at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:826) 

这是我第一次尝试在堆栈溢出时提出问题。谢谢您的帮助。

1 个答案:

答案 0 :(得分:0)

似乎您已经在root视图中声明了布局名称,因此视图未初始化。 请检查findViewById(R.id.activity_launch)的rootview并进行相应的更新。