Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/java/355.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代码更改文本和背景颜色?_Java_Android_Xml_Android Layout - Fatal编程技术网

我无法通过活动中不同版面的java代码更改文本和背景颜色?

我无法通过活动中不同版面的java代码更改文本和背景颜色?,java,android,xml,android-layout,Java,Android,Xml,Android Layout,Java代码已经给出,但我无法在相对布局中更改小部件的文本和颜色。在我的布局中,我在默认的相对布局上使用了5个相对布局。现在我无法更改这些布局中的背景色和文本。附加的xml文件 package care.e_eyepewsversion2; import android.graphics.Color; import android.support.v7.app.AppCompatActivity; import android.os.Bundle; import android.view.Vi

Java代码已经给出,但我无法在相对布局中更改小部件的文本和颜色。在我的布局中,我在默认的相对布局上使用了5个相对布局。现在我无法更改这些布局中的背景色和文本。附加的xml文件

package care.e_eyepewsversion2;

import android.graphics.Color;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.view.View;
import android.widget.Button;
import android.widget.RelativeLayout;
import android.widget.TextView;

public class Software_main_screen extends AppCompatActivity {
    int  comconnect=0;
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);

        setContentView(R.layout.activity_software_main_screen);
        Button Activebutton= (Button) findViewById(R.id.mainscreen_active_button);

        Button ComConnectButton= (Button) findViewById(R.id.mainscreen_connect_button);
        Button ComdisconnectButton = (Button) findViewById(R.id.mainscreen_disconnect_button);

        assert ComConnectButton != null;
        ComConnectButton.setOnClickListener(new Button.OnClickListener(){
            public void onClick(View V){
              update_status();

                                            }
                                        }
        );

        assert Activebutton != null;
        Activebutton.setOnClickListener(new Button.OnClickListener(){
            public void onClick(View V){
                RelativeLayout Mote1_mainScreen= (RelativeLayout) findViewById(R.id.Mote1_mainscreen);
                RelativeLayout Mote2_mainScreen= (RelativeLayout) findViewById(R.id.Mote2_mainscreen);
                RelativeLayout Mote3_mainScreen= (RelativeLayout) findViewById(R.id.Mote3_mainscreen);
                RelativeLayout Mote4_mainScreen= (RelativeLayout) findViewById(R.id.Mote4_mainscreen);
                RelativeLayout Mote5_mainScreen= (RelativeLayout) findViewById(R.id.Mote5_mainscreen);

                if(comconnect==1){
                    Mote1_mainScreen.setBackgroundColor(Color.GREEN);
                    Mote2_mainScreen.setBackgroundColor(Color.GREEN);
                    Mote3_mainScreen.setBackgroundColor(Color.GREEN);
                    Mote4_mainScreen.setBackgroundColor(Color.GREEN);
                    Mote5_mainScreen.setBackgroundColor(Color.GREEN);

                }
            }
        }




        );

    }
     public void update_status(){
         comconnect=1;
         TextView mote1_connect_status= (TextView) findViewById(R.id.mote1_connectstatus);
               TextView mote2_connect_status= (TextView) findViewById(R.id.mote2_connectstatus);
                TextView mote3_connect_status= (TextView) findViewById(R.id.mote3_connectstatus);
                TextView mote4_connect_status= (TextView) findViewById(R.id.mote4_connectstatus);
                TextView mote5_connect_status= (TextView) findViewById(R.id.mote5_connectstatus);

         mote1_connect_status.setBackgroundColor(Color.GREEN);
                    mote2_connect_status.setBackgroundColor(Color.GREEN);
                    mote3_connect_status.setBackgroundColor(Color.GREEN);
                    mote4_connect_status.setBackgroundColor(Color.GREEN);
                    mote5_connect_status.setBackgroundColor(Color.GREEN);


                    mote1_connect_status.setText("CONNECTTED");
                mote2_connect_status.setText("CONNECTTED");
                mote3_connect_status.setText("CONNECTTED");
                mote4_connect_status.setText("CONNECTTED");
                mote5_connect_status.setText("CONNECTTED");



 }
}

Xml文件

<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:tools="http://schemas.android.com/tools"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:paddingBottom="@dimen/activity_vertical_margin"
    android:paddingLeft="@dimen/activity_horizontal_margin"
    android:paddingRight="@dimen/activity_horizontal_margin"
    android:paddingTop="@dimen/activity_vertical_margin"
    tools:context="care.e_eyepewsversion2.Loading_Screen2">


    <TextView
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:textAppearance="?android:attr/textAppearanceLarge"
        android:text="E-EYE Platoon Early Warning System"
        android:textAlignment="center"
        android:textColor="@color/Email_password_text"
        android:textAllCaps="true"
        android:textStyle="bold"
        android:id="@+id/textView4"
        android:layout_alignParentTop="true"
        android:layout_centerHorizontal="true" />

    <GridLayout
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:layout_below="@+id/textView4"
        android:layout_centerHorizontal="true"
        android:layout_marginTop="60dp"
        android:id="@+id/motesview_mainscreen">

        <Button
            style="?android:attr/buttonStyleSmall"
            android:layout_width="100dp"
            android:layout_height="40dp"
            android:text="Connect"
            android:layout_gravity="center_horizontal"
            android:textAllCaps="true"
            android:textStyle="bold"
            android:background="@color/Email_password_text"
            android:id="@+id/mainscreen_connect_button"
            android:layout_row="0"
            android:layout_column="0" />

        <Button
            style="?android:attr/buttonStyleSmall"
            android:layout_width="100dp"
            android:layout_height="40dp"
            android:text="DisConnect"
            android:layout_gravity="center_horizontal"
            android:textAllCaps="true"
            android:textStyle="bold"
            android:background="#FF0000"
            android:id="@+id/mainscreen_disconnect_button"
            android:layout_row="0"
            android:layout_column="1" />

        <Button
            style="?android:attr/buttonStyleSmall"
            android:layout_width="100dp"
            android:layout_height="40dp"
            android:text="Active"
            android:layout_gravity="center_horizontal"
            android:textAllCaps="true"
            android:textStyle="bold"
            android:background="@color/Email_password_text"
            android:id="@+id/mainscreen_active_button"
            android:layout_row="0"
            android:layout_column="2"
            />

        <TextView
            android:layout_width="100dp"
            android:layout_height="30dp"
            android:text="Connected"
            android:layout_gravity="center_horizontal"
            android:textAllCaps="true"
            android:textAlignment="center"
            android:textColor="#FF0000"
            android:textStyle="bold"
            android:textSize="15dp"
            android:id="@+id/Connected_text_main_software"
            android:layout_row="1"
            android:layout_column="0" />

        <TextView
            android:layout_width="100dp"
            android:layout_height="30dp"
            android:text="DisConnected"
            android:layout_gravity="center_horizontal"
            android:textAllCaps="true"
            android:textAlignment="center"
            android:textColor="@color/Email_password_text"
            android:textStyle="bold"
            android:textSize="12dp"
            android:id="@+id/Disconnected_text_main_software"
            android:layout_row="1"
            android:layout_column="1" />

        <Button
            style="?android:attr/buttonStyleSmall"
            android:layout_width="100dp"
            android:layout_height="40dp"
            android:text="standby"
            android:layout_gravity="center"

            android:textAllCaps="true"
            android:textStyle="bold"
            android:background="#F0ff00"
            android:id="@+id/mainscreen_standby_button"
            android:layout_row="1"
            android:layout_column="2" />

        <RelativeLayout
            android:layout_width="110dp"
            android:layout_height="60dp"
            android:layout_row="2"
            android:layout_column="0"
            android:background="#F0ff00"
            android:id="@+id/Mote1_mainscreen">



            <TextView
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:textAppearance="?android:attr/textAppearanceSmall"
                android:text="MOTE 1"
                android:textStyle="bold"
                android:background="@color/login_but_text_clr"
                android:id="@+id/textView5"
                android:layout_alignParentTop="true"
                android:layout_centerHorizontal="true" />

            <TextView
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:textAppearance="?android:attr/textAppearanceSmall"
                android:background="#FF0000"
                android:textStyle="bold"
                android:text="DISCONNECTED"

                android:id="@+id/mote1_connectstatus"
                android:layout_centerVertical="true"
                android:layout_centerHorizontal="true" />
        </RelativeLayout>

        <RelativeLayout
            android:layout_width="110dp"
            android:layout_height="60dp"
            android:layout_row="2"
            android:layout_column="1"
            android:id="@+id/Mote2_mainscreen"
            android:background="#F0ff00">



            <TextView
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:textAppearance="?android:attr/textAppearanceSmall"
                android:text="MOTE 2"
                android:textStyle="bold"
                android:background="@color/login_but_text_clr"
                android:id="@+id/textView7"
                android:layout_alignParentTop="true"
                android:layout_centerHorizontal="true" />

            <TextView
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:textAppearance="?android:attr/textAppearanceSmall"
                android:background="#FF0000"
                android:textStyle="bold"
                android:text="DISCONNECTED"
                android:id="@+id/mote2_connectstatus"
                android:layout_centerVertical="true"
                android:layout_centerHorizontal="true" />
        </RelativeLayout>

        <RelativeLayout
            android:layout_width="110dp"
            android:layout_height="60dp"
            android:layout_row="2"
            android:layout_column="2"
            android:id="@+id/Mote3_mainscreen"
            android:background="#F0ff00">



            <TextView
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:textAppearance="?android:attr/textAppearanceSmall"
                android:text="MOTE 3"
                android:textStyle="bold"
                android:background="@color/login_but_text_clr"
                android:id="@+id/textView9"
                android:layout_alignParentTop="true"
                android:layout_centerHorizontal="true" />

            <TextView
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:textAppearance="?android:attr/textAppearanceSmall"
                android:background="#FF0000"
                android:textStyle="bold"
                android:text="DISCONNECTED"
                android:id="@+id/mote3_connectstatus"
                android:layout_centerVertical="true"
                android:layout_centerHorizontal="true" />
        </RelativeLayout>

        <RelativeLayout
            android:layout_width="110dp"
            android:layout_height="60dp"
            android:layout_row="3"
            android:layout_column="0"
            android:id="@+id/Mote4_mainscreen"
            android:background="#F0ff00">

            <ProgressBar
                style="?android:attr/progressBarStyleHorizontal"
                android:layout_width="90dp"
                android:layout_height="wrap_content"
                android:id="@+id/progressBar5"
                android:layout_alignParentBottom="true"
                android:layout_centerHorizontal="true"
                android:progressBackgroundTint="#FF0000"
                android:progressTint="#FF0000"
                android:progress="100"/>

            <TextView
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:textAppearance="?android:attr/textAppearanceSmall"
                android:text="MOTE 4"
                android:textStyle="bold"
                android:background="@color/login_but_text_clr"
                android:id="@+id/textView11"
                android:layout_alignParentTop="true"
                android:layout_centerHorizontal="true" />

            <TextView
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:textAppearance="?android:attr/textAppearanceSmall"
                android:background="#FF0000"
                android:textStyle="bold"
                android:text="DISCONNECTED"
                android:id="@+id/mote4_connectstatus"
                android:layout_centerVertical="true"
                android:layout_centerHorizontal="true" />
        </RelativeLayout>

        <RelativeLayout
            android:layout_width="110dp"
            android:layout_height="60dp"
            android:layout_row="3"
            android:layout_column="1"
            android:id="@+id/Mote5_mainscreen"
            android:background="#F0ff00">

            <ProgressBar
                style="?android:attr/progressBarStyleHorizontal"
                android:layout_width="90dp"
                android:layout_height="wrap_content"
                android:id="@+id/progressBar6"
                android:layout_alignParentBottom="true"
                android:layout_centerHorizontal="true"
                android:progressBackgroundTint="#FF0000"
                android:progressTint="#FF0000"
                android:progress="100"/>

            <TextView
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:textAppearance="?android:attr/textAppearanceSmall"
                android:text="MOTE 5"
                android:textStyle="bold"
                android:background="@color/login_but_text_clr"
                android:id="@+id/textView13"
                android:layout_alignParentTop="true"
                android:layout_centerHorizontal="true" />

            <TextView
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:textAppearance="?android:attr/textAppearanceSmall"
                android:background="#FF0000"
                android:textStyle="bold"
                android:text="DISCONNECTED"
                android:id="@+id/mote5_connectstatus"
                android:layout_centerVertical="true"
                android:layout_centerHorizontal="true" />
        </RelativeLayout>

        <RelativeLayout
            android:layout_width="110dp"
            android:layout_height="60dp"
            android:layout_row="3"
            android:layout_column="2"
            android:id="@+id/Mote6_mainscreen"
            android:background="#F0ff00">

            <ProgressBar
                style="?android:attr/progressBarStyleHorizontal"
                android:layout_width="90dp"
                android:layout_height="wrap_content"
                android:id="@+id/progressBar7"
                android:layout_alignParentBottom="true"
                android:layout_centerHorizontal="true"
                android:progressBackgroundTint="#FF0000"
                android:progressTint="#FF0000"
                android:progress="100"/>

            <TextView
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:textAppearance="?android:attr/textAppearanceSmall"
                android:text="MOTE 6"
                android:textStyle="bold"
                android:background="@color/login_but_text_clr"
                android:id="@+id/textView14"
                android:layout_alignParentTop="true"
                android:layout_centerHorizontal="true" />

            <TextView
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:textAppearance="?android:attr/textAppearanceSmall"
                android:background="#FF0000"
                android:textStyle="bold"
                android:text="DISCONNECTED"
                android:id="@+id/mote6_connectstatus"
                android:layout_centerVertical="true"
                android:layout_centerHorizontal="true" />
        </RelativeLayout>

        <RelativeLayout
            android:layout_width="110dp"
            android:layout_height="60dp"
            android:layout_row="4"
            android:layout_column="0"                                         android:id="@+id/Mote7_mainscreen"
            android:background="#F0ff00">

            <ProgressBar
                style="?android:attr/progressBarStyleHorizontal"
                android:layout_width="90dp"
                android:layout_height="wrap_content"
                android:id="@+id/progressBar8"
                android:layout_alignParentBottom="true"
                android:layout_centerHorizontal="true"
                android:progressBackgroundTint="#FF0000"
                android:progressTint="#FF0000"
                android:progress="100"/>

            <TextView
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:textAppearance="?android:attr/textAppearanceSmall"
                android:text="MOTE 7"
                android:textStyle="bold"
                android:background="@color/login_but_text_clr"
                android:id="@+id/textView15"
                android:layout_alignParentTop="true"
                android:layout_centerHorizontal="true" />

            <TextView
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:textAppearance="?android:attr/textAppearanceSmall"
                android:background="#FF0000"
                android:textStyle="bold"
                android:text="DISCONNECTED"
                android:id="@+id/mote7_connectstatus"
                android:layout_centerVertical="true"
                android:layout_centerHorizontal="true" />
        </RelativeLayout>

        <RelativeLayout
            android:layout_width="110dp"
            android:layout_height="60dp"
            android:layout_row="4"
            android:layout_column="1"                                         android:id="@+id/Mote8_mainscreen"
            android:background="#F0ff00">

            <ProgressBar
                style="?android:attr/progressBarStyleHorizontal"
                android:layout_width="90dp"
                android:layout_height="wrap_content"
                android:id="@+id/progressBar9"
                android:layout_alignParentBottom="true"
                android:layout_centerHorizontal="true"
                android:progressBackgroundTint="#FF0000"
                android:progressTint="#FF0000"
                android:progress="100"/>

            <TextView
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:textAppearance="?android:attr/textAppearanceSmall"
                android:text="MOTE 8"
                android:textStyle="bold"
                android:background="@color/login_but_text_clr"
                android:id="@+id/textView16"
                android:layout_alignParentTop="true"
                android:layout_centerHorizontal="true" />

            <TextView
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:textAppearance="?android:attr/textAppearanceSmall"
                android:background="#FF0000"
                android:textStyle="bold"
                android:text="DISCONNECTED"
                android:id="@+id/mote8_connectstatus"
                android:layout_centerVertical="true"
                android:layout_centerHorizontal="true" />
        </RelativeLayout>

        <RelativeLayout
            android:layout_width="110dp"
            android:layout_height="60dp"
            android:layout_row="4"
            android:layout_column="2"
            android:id="@+id/Mote9_mainscreen"
            android:background="#F0ff00">

            <ProgressBar
                style="?android:attr/progressBarStyleHorizontal"
                android:layout_width="90dp"
                android:layout_height="wrap_content"
                android:id="@+id/progressBar10"
                android:layout_alignParentBottom="true"
                android:layout_centerHorizontal="true"
                android:progressBackgroundTint="#FF0000"
                android:progressTint="#FF0000"
                android:progress="100"/>

            <TextView
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:textAppearance="?android:attr/textAppearanceSmall"
                android:text="MOTE 9"
                android:textStyle="bold"
                android:background="@color/login_but_text_clr"
                android:id="@+id/textView17"
                android:layout_alignParentTop="true"
                android:layout_centerHorizontal="true" />

            <TextView
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:textAppearance="?android:attr/textAppearanceSmall"
                android:background="#FF0000"
                android:textStyle="bold"
                android:text="DISCONNECTED"
                android:id="@+id/mote9_connectstatus"
                android:layout_centerVertical="true"
                android:layout_centerHorizontal="true" />
        </RelativeLayout>

        <RelativeLayout
            android:layout_width="110dp"
            android:layout_height="60dp"
            android:layout_row="5"
            android:layout_column="1"                                         android:id="@+id/Mote10_mainscreen"
            android:background="#F0ff00">

            <ProgressBar
                style="?android:attr/progressBarStyleHorizontal"
                android:layout_width="90dp"
                android:layout_height="wrap_content"
                android:id="@+id/progressBar11"
                android:layout_alignParentBottom="true"
                android:layout_centerHorizontal="true"
                android:progressBackgroundTint="#FF0000"
                android:progressTint="#FF0000"
                android:progress="100"/>

            <TextView
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:textAppearance="?android:attr/textAppearanceSmall"
                android:text="MOTE 10"
                android:textStyle="bold"
                android:background="@color/login_but_text_clr"
                android:id="@+id/textView18"
                android:layout_alignParentTop="true"
                android:layout_centerHorizontal="true" />

            <TextView
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:textAppearance="?android:attr/textAppearanceSmall"
                android:background="#FF0000"
                android:textStyle="bold"
                android:text="DISCONNECTED"
                android:id="@+id/mote10_connectstatus"
                android:layout_centerVertical="true"
                android:layout_centerHorizontal="true" />
        </RelativeLayout>
    </GridLayout>

    <Spinner
         android:layout_width="120dp"
         android:layout_height="50dp"
         android:id="@+id/Comport_Spinner"
         android:layout_below="@+id/textView4"
         android:layout_alignParentStart="true"
         android:prompt="@string/Port"
        android:spinnerMode="dropdown"
        android:entries="@array/ports_array"
        />

      <Spinner
        android:layout_width="120dp"
        android:layout_height="50dp"
        android:id="@+id/Mote_Spinner"
        android:layout_below="@+id/textView4"
        android:layout_alignParentEnd="true"
        android:prompt="@string/Mote"
        android:spinnerMode="dropdown"
        android:entries="@array/Mote_array"
        />

    <TextView
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="Danger"
        android:textAllCaps="true"
        android:textSize="20dp"
        android:textColor="@color/Email_password_text"
        android:textStyle="bold"
        android:id="@+id/mainscreen_danger"
        android:layout_alignBottom="@+id/Comport_Spinner"
        android:layout_centerHorizontal="true"
        android:layout_marginBottom="10dp"/>
</RelativeLayout>

代码中没有问题,只是我的模拟器已损坏