سؤال

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