简体   繁体   English

尝试在空对象引用上调用虚拟方法“void android.widget.TextView.setBackgroundColor(int)”

[英]Attempt to invoke virtual method 'void android.widget.TextView.setBackgroundColor(int)' on a null object reference

This code is inside a while loop that moves the cursor in a database for each loop.此代码位于 while 循环中,该循环为每个循环在数据库中移动光标。 I want that if daysLeftInt is under 6, then the id (R.id.leftColor) should be the color of the variable redColor.我希望如果 daysLeftInt 小于 6,那么 id (R.id.leftColor) 应该是变量 redColor 的颜色。

            if(daysLeftInt < 6) {
                Context context = this;
                int redColor = ContextCompat.getColor(context, R.color.red);
                TextView rectangleView = (TextView)findViewById(R.id.leftColor);
                rectangleView.setBackgroundColor(redColor);

            }

I get this error message:我收到此错误消息:

Attempt to invoke virtual method 'void android.widget.TextView.setBackgroundColor(int)' on a null object reference尝试在空对象引用上调用虚拟方法“void android.widget.TextView.setBackgroundColor(int)”

This is how my layouts is setup:这是我的布局设置方式:

Do anyone know how i will get the result that I want?有谁知道我将如何得到我想要的结果?

activity_main.xml.活动_main.xml。

<?xml version="1.0" encoding="utf-8"?>
    <android.support.v4.widget.DrawerLayout 
        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/drawer_layout"
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:background="@drawable/bg"
        android:fitsSystemWindows="true"
        tools:openDrawer="start">


        <include
            layout="@layout/app_bar_main"
            android:layout_width="match_parent"
            android:layout_height="match_parent" />

        <android.support.design.widget.NavigationView
            android:id="@+id/nav_view"
            android:layout_width="wrap_content"
            android:layout_height="match_parent"
            android:layout_gravity="start"
            android:fitsSystemWindows="true"
            app:headerLayout="@layout/nav_header_main"
            app:menu="@menu/activity_main_drawer" />

    </android.support.v4.widget.DrawerLayout>

activity_main.xml includes this file called app_bar_main.xml activity_main.xml 包含这个名为 app_bar_main.xml 的文件

<?xml version="1.0" encoding="utf-8"?>
<android.support.design.widget.CoordinatorLayout 
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:layout_width="match_parent"
android:layout_height="match_parent"
tools:context="com.mysubscriptions.alexklug.mysubscriptions.MainActivity">

<android.support.design.widget.AppBarLayout
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:theme="@style/AppTheme.AppBarOverlay">

    <android.support.v7.widget.Toolbar
        android:id="@+id/toolbar"
        android:layout_width="match_parent"
        android:layout_height="?attr/actionBarSize"
        android:background="?attr/colorPrimary"
        app:popupTheme="@style/AppTheme.PopupOverlay" />

</android.support.design.widget.AppBarLayout>

<include layout="@layout/content_main" />
<include layout="@layout/content_subscription" />

<android.support.design.widget.FloatingActionButton
    android:id="@+id/fab"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:layout_gravity="bottom|end"
    android:layout_margin="16dp"
    app:srcCompat="@android:drawable/ic_menu_send"
    android:onClick="openSubscriptionActivity"
    />

</android.support.design.widget.CoordinatorLayout>

app_bar_main.xml include the layout file content_main.xml which is this file: app_bar_main.xml 包含布局文件 content_main.xml,它是这个文件:

<?xml version="1.0" encoding="utf-8"?>
<android.support.constraint.ConstraintLayout 
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:layout_width="match_parent"
android:layout_height="match_parent"
app:layout_behavior="@string/appbar_scrolling_view_behavior"
tools:context="com.mysubscriptions.alexklug.mysubscriptions.MainActivity"
tools:showIn="@layout/app_bar_main">



<ListView
    android:id="@+id/subscriptionsList"
    android:layout_width="368dp"
    android:textAllCaps="true"
    android:layout_alignParentStart="true"
    android:layout_alignParentTop="true"
    android:layout_height="wrap_content"
    tools:layout_editor_absoluteY="0dp"
    tools:layout_editor_absoluteX="8dp">
</ListView>

Inside the ListView an ArrayAdapter helps displaying the last file called list_item.xml.在 ListView 中,ArrayAdapter 有助于显示名为 list_item.xml 的最后一个文件。 The first TextView is the view with the id of leftColor.第一个TextView 是id 为leftColor 的视图。

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:tools="http://schemas.android.com/tools"
    android:orientation="horizontal"
    android:layout_width="match_parent"
    android:layout_height="wrap_content">

<LinearLayout
    android:layout_width="match_parent"
    android:layout_height="50dp"
    android:orientation="horizontal">

    <TextView

        android:id="@+id/leftColor"
        android:layout_width="3dp"
        android:layout_height="wrap_content"
        android:layout_weight="0.01"
        />


    <ImageView
        android:id="@+id/subscription_image"
        android:layout_height="wrap_content"
        android:layout_width="0dp"
        android:layout_weight="1"
        android:src="@mipmap/ic_netflix"
        android:padding="10dp"/>


    <TextView
        android:id="@+id/subscription_name"
        android:layout_weight="2"
        android:layout_width="0dp"
        android:layout_height="wrap_content"
        android:layout_marginTop="8dp"
        android:textColor="#000000"
        android:textSize="15sp"
        android:textStyle="bold"
        tools:text="First"
        android:ellipsize="end"
        android:maxLines="1"

        />


    <TextView
        android:id="@+id/subscription_price"
        android:layout_height="wrap_content"
        android:layout_marginTop="8dp"
        tools:text="Second"
        android:textSize="12sp"
        android:layout_width="0dp"
        android:layout_weight=".7"
        android:gravity="center"
        />


    <TextView
        android:id="@+id/subscription_days_left"
        android:layout_height="wrap_content"
        android:layout_marginTop="12dp"
        android:textSize="24sp"
        android:textColor="#000000"
        tools:text="12"
        android:layout_width="0dp"
        android:layout_weight=".7"
        android:textStyle="bold"
        android:gravity="center"
        />


    <View
        android:id="@+id/myRectangleView"
        android:layout_width="7dp"
        android:layout_height="7dp"
        android:layout_marginTop="24dp"
        android:layout_marginRight="20dp"
        android:layout_marginLeft="20dp"
        android:background="@drawable/circle" />


    </LinearLayout>
</LinearLayout>

This is happening because " R.id.leftColor " doesn't match with the resource in the XML file.发生这种情况是因为“ R.id.leftColor ”与 XML 文件中的资源不匹配。 In this case, what you can do is hold 'ctrl' and hover the mouse over " R.id.leftColor " in java code and click the link it points to which appears after hovering.在这种情况下,您可以做的是按住 'ctrl' 并将鼠标悬停在 Java 代码中的“ R.id.leftColor ”上,然后单击悬停后出现的链接。

  1. If it takes it to the resource in the layout XML file, then it's alright, just perform a如果它把它带到布局XML文件中的资源中,那么没关系,只需执行一个

Build->Clean Project构建->清理项目

Build->Rebuild Project构建->重建项目

  1. Else, if it takes to an id in the R.java file, like this否则,如果它需要 R.java 文件中的一个 id,就像这样

public static final int leftColor=0x7f0d008c; public static final int leftColor=0x7f0d008c;

then you might have mistakenly renamed the leftColor in the layout XML file to something else.那么您可能错误地将布局 XML 文件中的leftColor重命名为其他内容。 Correct that to whatever you have given in the java code and it should work fine.将其纠正为您在 java 代码中给出的任何内容,它应该可以正常工作。

It's android studios fault, just put in a delay of about 50 milliseconds.这是android studios的错,只是延迟了大约50毫秒。 Example:示例:

 Handler handler = new Handler();
        handler.postDelayed(new Runnable() {
            @Override
            public void run() {
                listView.getChildAt(0).setBackgroundColor(Color.BLUE);
                listView.getChildAt(1).setBackgroundColor(Color.WHITE);
                listView.getChildAt(2).setBackgroundColor(Color.RED);
                listView.getChildAt(3).setBackgroundColor(Color.BLACK);
            }
        }, 50);

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 尝试在 null object 引用上调用虚方法“void android.widget.TextView.setVisibility(int)” - Attempt to invoke virtual method 'void android.widget.TextView.setVisibility(int)' on a null object reference 我尝试在空对象引用上调用虚拟方法&#39;void android.widget.ImageView.setImageResource(int)&#39; - I got Attempt to invoke virtual method 'void android.widget.ImageView.setImageResource(int)' on a null object reference Android Studio: Attempt to invoke virtual method 'void android.widget.ImageButton.setImageResource(int)' on a null object reference - Android Studio: Attempt to invoke virtual method 'void android.widget.ImageButton.setImageResource(int)' on a null object reference NullPointerException:尝试在空对象引用上调用虚拟方法&#39;void android.widget.ImageView.setImageResource(int)&#39; - NullPointerException: Attempt to invoke virtual method 'void android.widget.ImageView.setImageResource(int)' on a null object reference Recycler View - 尝试在空对象引用上调用虚拟方法“void android.widget.TextView.setText(java.lang.CharSequence)” - Recycler View - Attempt to invoke virtual method 'void android.widget.TextView.setText(java.lang.CharSequence)' on a null object reference 尝试在 java 中的空对象引用上调用虚拟方法 &#39;void android.widget.TextView.setTypeface(android.graphics.Typeface)&#39; - Attempt to invoke virtual method 'void android.widget.TextView.setTypeface(android.graphics.Typeface)' on a null object reference in java 尝试在空对象引用上调用虚拟方法&#39;void android.widget.TextView.append(java.lang.CharSequence)&#39; - Attempt to invoke virtual method 'void android.widget.TextView.append(java.lang.CharSequence)' on a null object reference 如何解决对空对象引用调用虚拟方法&#39;void android.widget.TextView.setText(java.lang.CharSequence)&#39;的尝试 - How to resolve Attempt to invoke virtual method 'void android.widget.TextView.setText(java.lang.CharSequence)' on a null object reference (RecyclerView) 尝试在空对象引用上调用虚拟方法“void android.widget.TextView.setText(java.lang.CharSequence)” - (RecyclerView) Attempt to invoke virtual method 'void android.widget.TextView.setText(java.lang.CharSequence)' on a null object reference Java错误:尝试在空对象引用上调用虚拟方法&#39;void android.widget.TextView.setText(java.lang.CharSequence)&#39; - Java Error: Attempt to invoke virtual method 'void android.widget.TextView.setText(java.lang.CharSequence)' on a null object reference
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM