Warning: file_get_contents(/data/phpspider/zhask/data//catemap/3/android/208.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Java 尝试在空对象引用上调用虚拟方法“void android.widget.TextView.setBackgroundColor(int)”_Java_Android_Xml - Fatal编程技术网

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

Java 尝试在空对象引用上调用虚拟方法“void android.widget.TextView.setBackgroundColor(int)”,java,android,xml,Java,Android,Xml,此代码位于while循环中,该循环在数据库中为每个循环移动光标。我希望如果daysLeftInt小于6,那么id R.id.leftColor应该是变量redColor的颜色 if(daysLeftInt < 6) { Context context = this; int redColor = ContextCompat.getColor(context, R.color.red);

此代码位于while循环中,该循环在数据库中为每个循环移动光标。我希望如果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);

            }
我收到以下错误消息:

尝试在空对象引用上调用虚拟方法“void android.widget.TextView.setBackgroundColorint” 以下是如何设置我的布局:

有人知道我将如何得到我想要的结果吗

activity_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包括名为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包括布局文件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>
在ListView中,ArrayAdapter帮助显示最后一个名为list_item.xml的文件。 第一个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>
这是因为R.id.leftColor与XML文件中的资源不匹配。在本例中,您可以按住ctrl键并将鼠标悬停在java代码中的R.id.leftColor上,然后单击鼠标悬停后显示的链接

如果它将它带到布局XML文件中的资源,那么就可以执行 构建->清洁项目

构建->重建项目

否则,如果在R.java文件中需要一个id,如下所示 公共静态final int leftColor=0x7f0d008c


然后,您可能错误地将布局XML文件中的leftColor重命名为其他名称。更正java代码中给出的任何内容,它应该可以正常工作。

这是android Studio的错误,只需延迟大约50毫秒。例如:

 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);

你在活动吗?你知道findViewById是如何工作的吗?嗯,我在同一个活动中使用的所有其他findViewById都是以完全相同的方式编写的,但是它们是如何工作的。也许我不明白它是怎么工作的,但你能解释一下吗?是因为它在一个循环中出错了吗?问题中没有循环。但是如果你有一个循环,你只需要设置一个布局并找到完全相同的视图。由setContentView设置的任何布局都不包含您正在搜索的id,因此它为空。那么如何查找id?如果按住ctrl键并单击R.id.leftColor,则会转到布局XML文件。Build->Clean项目和Build->Rebuild项目什么都不做。我收到与以前相同的错误消息。@Alex哪个布局文件?我想你不止一个。请编辑您的问题,将其包括在内。您现在可以在描述中看到我使用的XML文件。关于它,R.id.leftColor不在该活动的setContentView中的XML文件中。如果是setContentViewR.layout.activity_main,则R.id.leftColor也应位于activity_main.xml中。