每当按下切换按钮时,我都会尝试更改活动的背景颜色,但我得到“引起:java.lang.NullPointerException:尝试调用虚方法”void android.widget.LinearLayout.setBackgroundColor( int)'在空对象引用上“错误。
这是我主要活动中的onCreate代码
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>
答案 0 :(得分:1)
您没有名为R.id.main
的小部件。
您有一个尝试设置该窗口小部件ID(main.xml
)的布局。但是,您的<include>
标记会覆盖具有不同值(@+id/include
)的窗口小部件ID。
更改您的Java代码以使用R.id.include
,或者从android:id
删除<include>
属性。