Java setBackgroundColor异常
每当按下切换按钮时,我都试图更改活动的背景色,但我遇到了“由以下原因引起的错误:java.lang.NullPointerException:在空对象引用上尝试调用虚拟方法'void android.widget.LinearLayout.setBackgroundColor(int)'”错误 这是我的主要活动中的onCreate代码Java setBackgroundColor异常,java,android,xml,android-layout,Java,Android,Xml,Android Layout,每当按下切换按钮时,我都试图更改活动的背景色,但我遇到了“由以下原因引起的错误:java.lang.NullPointerException:在空对象引用上尝试调用虚拟方法'void android.widget.LinearLayout.setBackgroundColor(int)'”错误 这是我的主要活动中的onCreate代码 delegate = AppCompatDelegate.create(this, this); delegate.onCreate(savedInsta
delegate = AppCompatDelegate.create(this, this);
delegate.onCreate(savedInstanceState);
delegate.setContentView(R.layout.activity_main);
Toolbar toolbar= (Toolbar) findViewById(R.id.toolbar);
delegate.setSupportActionBar(toolbar);
mScreen = (LinearLayout) findViewById(R.id.main);
mScreen.setBackgroundColor(getResources().getColor(R.color.red));
final ToggleButton start = (ToggleButton) findViewById(R.id.button);
start.setText("START");
start.setOnCheckedChangeListener(new CompoundButton.OnCheckedChangeListener() {
public void onCheckedChanged(CompoundButton buttonView, boolean isChecked) {
if (isChecked) {
startStepService();
bindStepService();
mScreen.setBackgroundColor(getResources().getColor(R.color.white));
mIsRunning = true;
start.setText("STOP");
} else {
unbindStepService();
stopStepService();
mScreen.setBackgroundColor(getResources().getColor(R.color.red));
start.setText("START");
mIsRunning = false;
}
}
});
}
以及我的布局的XML。一个是协调器布局,其中包含我正在更改的布局
activity_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"
android:fitsSystemWindows="true"
tools:context="com.sp.service9.Pedometer">
<android.support.design.widget.AppBarLayout
android:layout_height="wrap_content"
android:layout_width="match_parent"
android:theme="@style/AppTheme.AppBarOverlay">
<android.support.v7.widget.Toolbar
android:id="@+id/toolbar"
android:layout_width="match_parent"
android:layout_height="match_parent"
app:popupTheme="@style/AppTheme.PopupOverlay" />
</android.support.design.widget.AppBarLayout>
<include layout="@layout/main"
android:id="@+id/include" />
<android.support.design.widget.FloatingActionButton
android:id="@+id/fab"
android:layout_width="124dp"
android:layout_height="wrap_content"
android:layout_gravity="center_vertical|center_horizontal"
android:layout_margin="@dimen/fab_margin"
android:onClick="showAlert"
app:backgroundTint="?attr/colorPrimaryDark"
app:layout_anchor="@+id/include"
app:layout_anchorGravity="bottom|center_horizontal"
app:srcCompat="@drawable/giftboxblue" />
</android.support.design.widget.CoordinatorLayout>
和main.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/main"
android:paddingTop="50dp"
android:orientation="vertical"
android:layout_width="match_parent"
android:layout_height="match_parent"
tools:context=".Pedometer">
<ImageView
android:layout_width="match_parent"
android:layout_height="60dp"
app:srcCompat="@android:color/transparent"
android:id="@+id/imageView2" />
<LinearLayout
android:orientation="horizontal"
android:layout_width="match_parent"
android:layout_height="wrap_content">
<TextView
android:text="Welcome, "
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:gravity="right"
android:id="@+id/textView3"
android:layout_weight="1" />
<TextView
android:text="TextView"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:id="@+id/textView2"
android:layout_weight="1" />
</LinearLayout>
<TextView
android:textSize="30sp"
android:layout_width="wrap_content"
android:layout_height="59dp"
android:layout_span="4"
android:layout_gravity="center"
android:id="@+id/StepTracker" />
<RelativeLayout
android:layout_width="match_parent"
android:layout_height="268dp"
android:layout_gravity="center">
<ProgressBar
android:id="@+id/circular_progress_bar"
style="?android:attr/progressBarStyleHorizontal"
android:layout_width="275dp"
android:layout_height="260dp"
android:indeterminate="false"
android:progress="2"
android:foregroundGravity="center"
android:progressDrawable="@drawable/circular_progressbar"
android:layout_weight="1"
android:layout_alignParentTop="true"
android:layout_centerHorizontal="true" />
<TextView
android:id="@+id/step_value"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_span="4"
android:gravity="center_vertical|center_horizontal"
android:text="0"
android:textSize="60sp"
android:layout_weight="1"
android:layout_marginTop="88dp"
android:layout_alignParentTop="true"
android:layout_alignParentStart="true" />
</RelativeLayout>
<ToggleButton
android:id="@+id/button"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_centerHorizontal="true"
android:text="Button" />
</LinearLayout>
您没有名为
R.id.main
的小部件
您有一个试图设置小部件ID的布局(main.xml
)。但是,您的
标记会用不同的值(@+ID/include
)覆盖该小部件ID
将Java代码更改为使用R.id.include
,或者从
中删除android:id
属性