在app中将图像设置为壁纸

时间:2014-05-07 06:51:57

标签: java android eclipse wallpaper

我无法将应用中的图片设置为壁纸。在运行它显示不幸的是testandroid停止工作。

我已在清单文件中设置权限,并将最低SDK设置为5.

Androidmanifest文件

<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
    package="com.example.testandroid"
    android:versionCode="1"
    android:versionName="1.0" >

    <uses-sdk
        android:minSdkVersion="5"
        android:targetSdkVersion="19" />
    <uses-permission android:maxSdkVersion="19" android:name="android.permission.SET_WALLPAPER"/>

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

                <category android:name="android.intent.category.LAUNCHER" />
            </intent-filter>
        </activity>
        <activity
            android:name=".Activity"
            android:label="@string/app_name" >
        </activity>
    </application>

</manifest>

fragment_main.xml

<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:tools="http://schemas.android.com/tools"
    android:orientation="vertical"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:paddingBottom="@dimen/activity_vertical_margin"
    android:paddingLeft="@dimen/activity_horizontal_margin"
    android:paddingRight="@dimen/activity_horizontal_margin"
    android:paddingTop="@dimen/activity_vertical_margin"
    tools:context="com.example.testandroid.MainActivity$PlaceholderFragment" >

    <TextView
        android:id="@+id/textView1"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="@string/hello_world" />

    <ImageView 
        android:id="@+id/imageview1"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:src="@drawable/bing"
        android:adjustViewBounds="true"/>    

    <Button
        android:id="@+id/button1"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_marginLeft="14dp"
        android:layout_marginTop="27dp"
        android:text="@string/btn"
        android:onClick="goForNext"/>

    <Button
        android:id="@+id/button2"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="@string/btn1"
        android:onClick="setWallpaper"/>


</LinearLayout>

MainActivity.java

package com.example.testandroid;

import android.app.WallpaperManager;
import android.content.Intent;
import android.os.Bundle;
import android.support.v4.app.Fragment;
import android.support.v7.app.ActionBarActivity;
import android.view.LayoutInflater;
import android.view.Menu;
import android.view.MenuItem;
import android.view.View;
import android.view.ViewGroup;
import android.widget.Button;
import android.widget.ImageView;

public class MainActivity extends ActionBarActivity {

    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);

        ImageView imgview = (ImageView) findViewById(R.id.imageview1);
        imgview.setImageResource(R.drawable.bing);

        if (savedInstanceState == null) {
            getSupportFragmentManager().beginTransaction()
            .add(R.id.container, new PlaceholderFragment()).commit();
        }
    }

    @Override
    public boolean onCreateOptionsMenu(Menu menu) {

        // Inflate the menu; this adds items to the action bar if it is present.
        getMenuInflater().inflate(R.menu.main, menu);
        return true;
    }

    @Override
    public boolean onOptionsItemSelected(MenuItem item) {
        // Handle action bar item clicks here. The action bar will
        // automatically handle clicks on the Home/Up button, so long
        // as you specify a parent activity in AndroidManifest.xml.
        int id = item.getItemId();
        if (id == R.id.action_settings) {
            return true;
        }
        return super.onOptionsItemSelected(item);
    }

    /**
     * A placeholder fragment containing a simple view.
     */
    public static class PlaceholderFragment extends Fragment {

        public PlaceholderFragment() {
        }

        @Override
        public View onCreateView(LayoutInflater inflater, ViewGroup container,
                Bundle savedInstanceState) {
            View rootView = inflater.inflate(R.layout.fragment_main, container,
                    false);
            return rootView;
        }
    }

    public void goForNext(View view){
        Button but = (Button) findViewById(R.id.button1);
        Intent i = new Intent(MainActivity.this, Activity.class);
        startActivity(i);
    }



    public void setWallpaper(View view)
    {
        Button bt = (Button) findViewById(R.id.button2);
        WallpaperManager wm = WallpaperManager.getInstance(getApplicationContext());
        try
        {
            wm.setResource(R.drawable.bing);
        }
        catch (Exception e)
        {
            e.printStackTrace();
        }
    }
}

这是activity_main.xml文件。请帮助我。

activity_main.xml中

<FrameLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:tools="http://schemas.android.com/tools"
    android:id="@+id/container"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    tools:context="com.example.testandroid.MainActivity"
    tools:ignore="MergeRootFrame" />

1 个答案:

答案 0 :(得分:0)

尝试从onCreate()

中删除以下内容
ImageView imgview = (ImageView) findViewById(R.id.imageview1);
imgview.setImageResource(R.drawable.bing);

并将其添加到Fragment中的onCreateView()方法

View rootView = inflater.inflate(R.layout.fragment_main, container,
                false);
ImageView imgview = (ImageView) rootView.findViewById(R.id.imageview1);
imgview.setImageResource(R.drawable.bing);
return rootView;