尝试将壁纸设置为活动背景时应用程序崩溃

时间:2017-01-06 12:54:23

标签: android android-layout android-theme

在尝试将活动主题设置为默认墙纸时,我的应用程序崩溃了。我不知道是什么导致了这一点,因为我是Android新手。感谢帮助!

的AndroidManifest.xml

 <application
        android:allowBackup="true"
        android:icon="@mipmap/ic_launcher"
        android:label="@string/app_name"
        android:supportsRtl="true"
        android:theme="@style/AppTheme">
        <uses-feature android:name="android.hardware.telephony"/>

        <activity android:name=".WelcomeActivity" >

            <intent-filter>
                <action android:name="android.intent.action.MAIN" />
                <category android:name="android.intent.category.HOME" />
                <category android:name="android.intent.category.DEFAULT" />
                <category android:name="android.intent.category.LAUNCHER" />

            </intent-filter>

        </activity>

        <activity 
            android:name=".Launcher"
            android:theme="@style/Wallpaper" > <!-- HERE IT IS -->
            <category android:name="android.intent.category.HOME" />
            <category android:name="android.intent.category.DEFAULT" />
        </activity>

    </application>

style.xml

<style name="AppTheme" parent="Theme.AppCompat.Light.NoActionBar">
    <item name="colorPrimary">@android:color/transparent</item>
    <item name="colorPrimaryDark">@color/colorPrimaryDark</item>
    <item name="colorAccent">@color/colorAccent</item>
</style>
<!--HERE IT IS -->
<style name="Wallpaper" parent="@android:style/Theme.Wallpaper"></style>

爪哇

package com.visualartsinternational.www.artui;

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

public class Launcher extends AppCompatActivity {

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_launcher);

        if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.KITKAT) {
            Window w = getWindow(); // in Activity's onCreate() for instance
            w.setFlags(WindowManager.LayoutParams.FLAG_LAYOUT_NO_LIMITS, WindowManager.LayoutParams.FLAG_LAYOUT_NO_LIMITS);
        }
    }

}

2 个答案:

答案 0 :(得分:0)

设置壁纸

 WallpaperManager wallpaperManager = WallpaperManager.getInstance(DetailActivity.this);
                                    try {
                                        wallpaperManager.setBitmap("Add your image");

                                        wallpaperManager.suggestDesiredDimensions(width, height);
                                        Toast.makeText(DetailActivity.this, "Wallpaper Set", Toast.LENGTH_SHORT).show();

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

<强>清单 在清单文件中添加权限

 <uses-permission android:name="android.permission.INTERNET" />
    <uses-permission android:name="android.permission.SET_WALLPAPER" />
    <uses-permission android:name="android.permission.SET_WALLPAPER_HINTS" />

答案 1 :(得分:0)

我通过将AppCompatActivity更改为Activity来完全修复此问题。