Java 安卓壁纸设置程序错误

Java 安卓壁纸设置程序错误,java,android,Java,Android,我试着创建一个简单的android应用程序,它可以让你拍照并将其设置为设备壁纸 我还插入了调整位图大小以匹配屏幕大小的代码(崩溃可能是因为这个) 但当我按下“设置壁纸”按钮时,应用程序崩溃了。你能帮我弄清楚怎么了吗 以下是JAVA: package com.example.androidwall; import java.io.IOException; import android.app.Activity; import android.app.WallpaperManager; impo

我试着创建一个简单的android应用程序,它可以让你拍照并将其设置为设备壁纸

我还插入了调整位图大小以匹配屏幕大小的代码(崩溃可能是因为这个)

但当我按下“设置壁纸”按钮时,应用程序崩溃了。你能帮我弄清楚怎么了吗

以下是JAVA:

package com.example.androidwall;

import java.io.IOException;

import android.app.Activity;
import android.app.WallpaperManager;
import android.content.Context;
import android.content.Intent;
import android.graphics.Bitmap;
import android.graphics.Matrix;
import android.os.Bundle;
import android.provider.MediaStore;
import android.util.DisplayMetrics;
import android.view.Menu;
import android.view.MenuItem;
import android.view.View;
import android.widget.Toast;

public class MainActivity extends Activity {
    public static final int REQUEST_IMAGE_CAPTURE = 1;
    Bitmap bmp;
    WallpaperManager myWallpaperManager;

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

    public void takePictureIntentStart(View view){
        Intent takePictureIntent = new Intent(MediaStore.ACTION_IMAGE_CAPTURE);
        if(takePictureIntent.resolveActivity(getPackageManager()) != null) {
            startActivityForResult(takePictureIntent, REQUEST_IMAGE_CAPTURE);
        }
    }

    @Override
    protected void onActivityResult(int requestCode, int resultCode, Intent data){
        if(requestCode == REQUEST_IMAGE_CAPTURE && resultCode == RESULT_OK) {
            Bundle extras = data.getExtras();
            bmp = (Bitmap) extras.get("data");
        }
    }

    public void setWallpaper(View view){
        int screenWidth = myWallpaperManager.getDesiredMinimumWidth();
        int screenHeight = myWallpaperManager.getDesiredMinimumHeight();
        Bitmap resizedBitmap = Bitmap.createScaledBitmap(bmp, screenWidth, screenHeight, true);

        if(resizedBitmap != null){
            myWallpaperManager = WallpaperManager.getInstance(getApplicationContext());
            try {
                myWallpaperManager.setBitmap(resizedBitmap);
            } catch (IOException e) {
                Toast.makeText(this, "Error setting wallpaper", Toast.LENGTH_SHORT).show();
                e.printStackTrace();
            }
        }

        else if(bmp == null){
            Toast.makeText(this, "Please try again after taking the picture", Toast.LENGTH_LONG).show();
        }
    }
}
下面是XML:

<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:tools="http://schemas.android.com/tools"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    tools:context="${relativePackage}.${activityClass}" >

    <Button
        android:id="@+id/takePicture"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_alignParentTop="true"
        android:layout_centerHorizontal="true"
        android:layout_marginTop="32dp"
        android:text="@string/takePicture"
        android:onClick="takePictureIntentStart" />

    <Button
        android:id="@+id/setWallpaper"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_below="@+id/takePicture"
        android:layout_centerHorizontal="true"
        android:layout_marginTop="45dp"
        android:onClick="setWallpaper"
        android:text="@string/setWallpaper" />

</RelativeLayout>


请帮我解决这个问题。

您的堆栈跟踪丢失,但我相信您有空指针异常。原因是,您正在使用:

WallpaperManager myWallpaperManager;
在初始化之前。请看这里:

public void setWallpaper(View view){
    int screenWidth = myWallpaperManager.getDesiredMinimumWidth();
    int screenHeight = myWallpaperManager.getDesiredMinimumHeight();
此时,您的
mywallpermanager
仍然是
null
。移动您的单例请求
mywallpermanager=wallpermanager.getInstance(getApplicationContext())在方法开始之后,例如:

public void setWallpaper(View view){
    myWallpaperManager = WallpaperManager.getInstance(getApplicationContext());  // <--- This line of code could be here or you could another object creation.
    int screenWidth = myWallpaperManager.getDesiredMinimumWidth();
    int screenHeight = myWallpaperManager.getDesiredMinimumHeight();
    Bitmap resizedBitmap = Bitmap.createScaledBitmap(bmp, screenWidth, screenHeight, true);

    if(resizedBitmap != null){
        try {
            myWallpaperManager.setBitmap(resizedBitmap);
        } catch (IOException e) {
            Toast.makeText(this, "Error setting wallpaper", Toast.LENGTH_SHORT).show();
            e.printStackTrace();
        }
    }

    else if(bmp == null){
        Toast.makeText(this, "Please try again after taking the picture", Toast.LENGTH_LONG).show();
    }
}
public void setWallpaper(视图){

MyWallpareManager=WallpareManager.getInstance(getApplicationContext());//如果出现内存不足错误,则此

 myWallpaperManager.setImageBitmap(decodeSampledBitmapFromResource( mytext.getName(),  screenWidth,screenHeight));



    private Bitmap decodeSampledBitmapFromResource(String filePath, int i, int j) {
        // TODO Auto-generated method stub
        // TODO Auto-generated method stub
                    // First decode with inJustDecodeBounds=true to check dimensions
                    final BitmapFactory.Options options = new BitmapFactory.Options();
                    options.inJustDecodeBounds = true;
                    BitmapFactory.decodeFile(filePath, options);
                    // Calculate inSampleSize
                    options.inSampleSize = calculateInSampleSize(options, i, j);

                 // Decode bitmap with inSampleSize set
                    options.inJustDecodeBounds = false;
                    return BitmapFactory.decodeFile(filePath, options);


    }


    private int calculateInSampleSize(BitmapFactory.Options options, int j, int k) {
        // TODO Auto-generated method stub
        // Raw height and width of image
        final int height = options.outHeight;
        final int width = options.outWidth;
        int inSampleSize = 1;

        if (height > j || width > k) {

            final int halfHeight = height / 2;
            final int halfWidth = width / 2;

            // Calculate the largest inSampleSize value that is a power of 2 and keeps both
            // height and width larger than the requested height and width.
            while ((halfHeight / inSampleSize) > j
                    && (halfWidth / inSampleSize) > k) {
                inSampleSize *= 2;
            }
        }

        return inSampleSize;


    }