如何在android中始终显示昏暗的非活动屏幕

时间:2016-05-27 15:52:22

标签: android

我非常熟悉android。我试图将视图叠加在屏幕上的所有内容上。我把它变成了红色和半透明的。我希望通过覆盖半透明视图使屏幕比可能的最小亮度更暗。 所以我在服务中做了一个线性布局。 但我的应用程序每次启动时都会崩溃。我无法弄清楚出了什么问题。请帮忙!

  

我的主要活动

import android.content.Intent;
import android.os.Bundle;
import android.support.v7.app.AppCompatActivity;
import android.view.View;
import android.widget.Button;


public class MainActivity extends AppCompatActivity {

Button button1,button2;
@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    button1=(Button)findViewById(R.id.button);
    button2=(Button)findViewById(R.id.button2);

    final Intent intent=new Intent(this, OverlayService.class);
    button1.setOnClickListener(new View.OnClickListener() {
        @Override
        public void onClick(View v) {
            startService(intent);
        }
    });

    button2.setOnClickListener(new View.OnClickListener() {
        @Override
        public void onClick(View v) {
            stopService(intent);
        }
    });

}
  

服务类

package g2.screendimmer;



import android.app.Service;
import android.content.Intent;
import android.graphics.PixelFormat;
import android.os.IBinder;
import android.view.WindowManager;
import android.widget.LinearLayout;

public class OverlayService extends Service {

LinearLayout oView;

@Override
public IBinder onBind(Intent i) {
    return null;
}

@Override
public void onCreate() {
    super.onCreate();
    oView = new LinearLayout(this);
    oView.setBackgroundColor(0x88ff0000); // The translucent red color
    WindowManager.LayoutParams params = new WindowManager.LayoutParams(
            WindowManager.LayoutParams.MATCH_PARENT,
            WindowManager.LayoutParams.MATCH_PARENT,
            WindowManager.LayoutParams.TYPE_SYSTEM_OVERLAY,
            WindowManager.LayoutParams.FLAG_NOT_FOCUSABLE,
            PixelFormat.TRANSLUCENT);
    WindowManager wm = (WindowManager) getSystemService(WINDOW_SERVICE);
    wm.addView(oView, params);
}

@Override
public void onDestroy() {
    super.onDestroy();
    if(oView!=null){
        WindowManager wm = (WindowManager) getSystemService(WINDOW_SERVICE);
        wm.removeView(oView);
    }
 }
}
  

清单文件

<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
package="g2.screendimmer">

<uses-permission android:name="android.permission.SYSTEM_ALERT_WINDOW"/>

<application
    android:allowBackup="true"
    android:icon="@mipmap/ic_launcher"
    android:label="@string/app_name"
    android:supportsRtl="true"
    android:theme="@style/AppTheme">
    <activity android:name=".MainActivity">
        <intent-filter>
            <action android:name="android.intent.action.MAIN" />

            <category android:name="android.intent.category.LAUNCHER" />
        </intent-filter>
    </activity>

    <service android:name=".OverlayService"></service>
</application>

</manifest>
  

日志

05-27 21:27:35.651 1477-1477/g2.screendimmer E/AndroidRuntime: FATAL     EXCEPTION: main
                                                           Process: g2.screendimmer, PID: 1477
                                                           java.lang.RuntimeException: Unable to start activity ComponentInfo{g2.screendimmer/g2.screendimmer.MainActivity}: java.lang.NullPointerException: Attempt to invoke virtual method 'void android.widget.Button.setOnClickListener(android.view.View$OnClickListener)' on a null object reference
                                                               at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2339)
                                                               at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2413)
                                                               at android.app.ActivityThread.access$800(ActivityThread.java:155)
                                                               at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1317)
                                                               at android.os.Handler.dispatchMessage(Handler.java:102)
                                                               at android.os.Looper.loop(Looper.java:135)
                                                               at android.app.ActivityThread.main(ActivityThread.java:5343)
                                                               at java.lang.reflect.Method.invoke(Native Method)
                                                               at java.lang.reflect.Method.invoke(Method.java:372)
                                                               at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:905)
                                                               at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:700)
                                                            Caused by: java.lang.NullPointerException: Attempt to invoke virtual method 'void android.widget.Button.setOnClickListener(android.view.View$OnClickListener)' on a null object reference
                                                               at g2.screendimmer.MainActivity.onCreate(MainActivity.java:20)
                                                               at android.app.Activity.performCreate(Activity.java:6010)
                                                               at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1129)
                                                               at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2292)
                                                               at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2413) 
                                                               at android.app.ActivityThread.access$800(ActivityThread.java:155) 
                                                               at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1317) 
                                                               at android.os.Handler.dispatchMessage(Handler.java:102) 
                                                               at android.os.Looper.loop(Looper.java:135) 
                                                               at android.app.ActivityThread.main(ActivityThread.java:5343) 
                                                               at java.lang.reflect.Method.invoke(Native Method) 
                                                               at java.lang.reflect.Method.invoke(Method.java:372) 
                                                               at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:905) 
                                                               at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:700) 

1 个答案:

答案 0 :(得分:1)

当应用尝试访问按钮时,您收到null pointer exception,因为您没有设置活动的布局,请使用setContentView(R.layout.your_layout)来执行此操作