2017-04-02 63 views
1

我試圖改變活動的背景顏色,每當切換按鈕被按下,但我得到「引起:java.lang.NullPointerException:嘗試調用虛擬方法'void android.widget.LinearLayout。 setBackgroundColor(int)'在空對象引用「錯誤。setBackgroundColor exception

這是我在我的主要活動

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

} 

的onCreate代碼和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> 

回答

1

您沒有一個名爲R.id.main部件。

您的佈局嘗試設置該窗口小部件ID(main.xml)。但是,您的<include>標籤會使用不同的值覆蓋該小工具ID(@+id/include)。

要麼將​​Java代碼更改爲使用R.id.include,要麼可能是從<include>中刪除android:id屬性。