gpt4 book ai didi

java - setBackgroundColor 异常

转载 作者:行者123 更新时间:2023-11-30 00:37:31 25 4
gpt4 key购买 nike

每当按下切换按钮时,我都试图更改 Activity 的背景颜色,但我收到“Caused by: java.lang.NullPointerException: Attempt to invoke virtual method 'void android.widget.LinearLayout.setBackgroundColor( int)' 在空对象引用上“错误。

这是我在主要 Activity 中的 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。一个是 Coordinator Layout,其中包含我正在更改的布局。

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) 的布局。但是,您的 <include>然后标记用不同的值 ( @+id/include ) 覆盖该小部件 ID。

要么更改您的 Java 代码以使用 R.id.include ,或者删除 android:id来自 <include> 的属性.

关于java - setBackgroundColor 异常,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/43169659/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com