2016-11-30 87 views
0

因此,我有一個夜間過濾器應用程序,它對主要活動具有透明活動,並且在屏幕中間有一個按鈕,它應該將透明活動更改爲夜間模式的黑色「屏幕過濾器」。我遇到的問題是 很多appriciated如果有人回答 W/System.err:java.lang.NullPointerException:嘗試調用虛擬方法'void android.view.View.setBackgroundColor(int)'在空對象引用 W /System.err:at com.example.kenert.allinoneapp.Nightmode.turnNightOn(Nightmode.java:91) W/System.err:at com.example.kenert.allinoneapp.Nightmode.nightmodeButtonClicked(Nightmode.java:78) W/System.err的:在java.lang.reflect.Method.invoke(本機方法) W/System.err的:在java.lang.reflect.Method.invoke(Method.java:372)透明色NullpointerException

Mainactivity代碼:

import android.content.Intent; 
import android.graphics.Color; 
import android.provider.Settings; 
import android.support.v7.app.AppCompatActivity; 
import android.os.Bundle; 
import android.view.View; 
import android.view.Window; 
import android.view.WindowManager; 
import android.widget.ImageButton; 
import android.widget.SeekBar; 

public class Nightmode extends AppCompatActivity { 
    private boolean nightmodeOnOff; 
    public ImageButton modeOnOffButton; 
    private int brightness; 

    @Override 
    protected void onCreate(Bundle savedInstanceState) { 
     super.onCreate(savedInstanceState); 

     startActivity(new Intent(this,NightmodeFilter.class)); 
     setContentView(R.layout.activity_nightmode); 

     modeOnOffButton = (ImageButton) findViewById(R.id.nightmodeOnOffButton); 
     nightmodeOnOff = false; 


     int prog; 
     //Seekbar 
     SeekBar skbar = (SeekBar) findViewById(R.id.nightModeBar); 
     skbar.setMax(255); 
     skbar.setKeyProgressIncrement(127); 

     try { 
      brightness = Settings.System.getInt(getContentResolver(), Settings.System.SCREEN_BRIGHTNESS); 
     } catch (Exception e) { 
      e.printStackTrace(); 
     } 
     skbar.setProgress(brightness); 

     skbar.setOnSeekBarChangeListener(new SeekBar.OnSeekBarChangeListener() { 
      @Override 
      public void onProgressChanged(SeekBar seekBar, int progress, boolean b) { 
       if (progress <= 25) { 
        brightness = 25; 
       } else { 
        brightness = progress; 
       } 
      } 

      @Override 
      public void onStartTrackingTouch(SeekBar seekBar) { 

      } 

      @Override 
      public void onStopTrackingTouch(SeekBar seekBar) { 
       android.provider.Settings.System.putInt(getContentResolver(), Settings.System.SCREEN_BRIGHTNESS, brightness); 
       WindowManager.LayoutParams lpp = getWindow().getAttributes(); 
       lpp.screenBrightness = brightness/(float)255; 
       getWindow().setAttributes(lpp); 
      } 
     }); 

    } 


    public void nightmodeButtonClicked(View view) { 
     try { 
      if (nightmodeOnOff) { 
       nightmodeOnOff = false; 

       turnNightOff(); 
      } else { 
       nightmodeOnOff = true; 
       turnNightOn(); 
      } 

     } catch (Exception e) { 
      e.printStackTrace(); 
     } 
    } 

    private void turnNightOn() { 

     try { 
      modeOnOffButton.setImageResource(R.drawable.nightmodeonbutton); 

      findViewById(R.id.activity_nightmode_filter).setBackgroundColor(Color.parseColor("#99000000")); 



     } catch (Exception e) { 
      e.printStackTrace(); 
     } 
    } 

    private void turnNightOff() { 

     try { 
      modeOnOffButton.setImageResource(R.drawable.nightmodeonoffbutton); 
      findViewById(R.id.activity_nightmode).setBackgroundColor(0x66000000); 

     } catch (Exception e) { 
      e.printStackTrace(); 
     } 
    } 

    @Override 
    protected void onResume() { 
     super.onResume(); 
     turnNightOff(); 
    } 

    @Override 
    protected void onStart() { 
     super.onStart(); 
    } 

    @Override 
    protected void onStop() { 
     super.onStop(); 
     turnNightOff(); 
    } 

    @Override 
    protected void onDestroy() { 
     super.onDestroy(); 
     turnNightOff(); 
    } 

    @Override 
    protected void onPause() { 
     super.onPause(); 
     turnNightOff(); 
    } 
} 

透明活動代碼:

import android.os.Bundle; 
import android.support.v7.app.AppCompatActivity; 
import android.view.Window; 
import android.view.WindowManager; 

public class NightmodeFilter extends AppCompatActivity { 

    @Override 
    protected void onCreate(Bundle savedInstanceState) { 
     super.onCreate(savedInstanceState); 
     Window window = getWindow(); 
     window.addFlags(WindowManager.LayoutParams.FLAG_NOT_TOUCHABLE|WindowManager.LayoutParams.FLAG_NOT_TOUCH_MODAL| 
       WindowManager.LayoutParams.FLAG_NOT_FOCUSABLE); 
     setContentView(R.layout.activity_nightmode_filter); 
    } 

    @Override 
    protected void onDestroy() { 
     super.onDestroy(); 
    } 

    @Override 
    protected void onStop() { 
     super.onStop(); 
    } 

    @Override 
    protected void onStart() { 
     super.onStart(); 
    } 

    @Override 
    protected void onPause() { 
     super.onPause(); 
    } 

    @Override 
    protected void onResume() { 
     super.onResume(); 
    } 
} 

透明活性XML和主題:

<?xml version="1.0" encoding="utf-8"?> 
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" 
    xmlns:tools="http://schemas.android.com/tools" 
    android:theme="@style/Theme.Transparent" 
    android:id="@+id/activity_nightmode_filter" 
    android:layout_height="wrap_content" 
    android:background="@color/Transparent" 





    tools:context="com.example.kenert.allinoneapp.NightmodeFilter" 
    android:layout_width="match_parent" 
    android:orientation="horizontal"> 


</LinearLayout> 

主題:

<style name="Theme.Transparent" parent="Theme.AppCompat.DayNight.NoActionBar"> 
     <item name="android:windowIsTranslucent">true</item> 
     <item name="android:windowBackground">@android:color/transparent</item> 
     <item name="android:windowContentOverlay">@null</item> 
     <item name="android:windowNoTitle">true</item> 

     <item name="android:backgroundDimEnabled">false</item> 

    </style> 
+0

findViewById(R.id.activity_nightmode_filter).setBackgroundColor(Color.parseColor(「#99000000」))這是獲取錯誤的行。 – Kenertj

回答

0

我不認爲你的主要活動有「知名度activity_nightmode_filter「,你不能把這個函數放在你的NightmodeFilter活動中嗎?

0

問題是您嘗試獲取引用的視圖是活動視圖本身,因此當您調用findViewById時,它將返回空引用。改變活動背景顏色

一種方法是這樣的:在您的onCreate方法,你叫setContentView(R.layout.activity_nightmode);後,得到decorView並設置其背景色:

getWindow().getDecorView().setBackgroundCo‌​lor(Color.parseColor‌​("#99000000")) 
0

任何更多的想法,將appriciated。我有一個想法,如果我在透明活動中製作了透明圖像按鈕,如果按鈕點擊改變背景顏色而沒有錯誤呢?