문제

Basically, I have an app with two Activities.

#1 - MainActivity
This has a solid black background and a button.
When the button is pressed TransparentActivity should be presented.

#2 - TransparentActivity
I want this to be transparent (so the phones normal UI can be seen through).

I've tried using the following code:

@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setTheme(android.R.style.Theme_Translucent);
    setContentView(R.layout.activity_trick);
}

But it causes the app to crash with an NullPointerException.

도움이 되었습니까?

해결책

Try1:

Make super.onCreate(savedInstanceState); call after setTheme(android.R.style.Theme_Translucent);. So do as:

setTheme(android.R.style.Theme_Translucent);
super.onCreate(savedInstanceState);

Try 2:

If that doesn't work, I find the following way easiest to make my activity transparent:

<activity android:name=".your.activity.declaration.here" 
android:theme="@android:style/Theme.Translucent.NoTitleBar" />

Basically add android:theme="@android:style/Theme.Translucent.NoTitleBar" to your activity declaration in manifest. I can see that you are trying to do a similar thing programatically but by specifying it in manifest never crashed for me. If it does, then there might be other reasons.

Hope it helps.

다른 팁

AppCompatActivity hasn't the Theme_Translucent(maybe the Theme_Translucent is null),you should create your own style.

라이센스 : CC-BY-SA ~와 함께 속성
제휴하지 않습니다 StackOverflow
scroll top