0

我正在尝试为浏览我的应用程序的用户实现将图像设置为壁纸的功能。我正在使用通用图像加载器,除了设置墙纸时,一切都很好。我从 Stackoverflow 和其他网站尝试了几种不同的方法,但都没有成功。最后一次尝试我使用了这里的解决方案:Android set image as wallpaper

在实现它之后,一切都运行良好,直到我选择调用 setWall 的按钮 (setwallbtn)。那时我的 logcat 中出现了一堆错误(完整的 logcat 错误发布在下面,在代码之后)。

下面是 pageview_item.xml 和 ImagePagerActivity.java。

任何人都可以提供的任何帮助将不胜感激!

谢谢!

pageview_item.xml

<?xml version="1.0" encoding="utf-8"?>
<FrameLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="fill_parent"
    android:layout_height="fill_parent"
    android:padding="1dip" >

    <ImageView
        android:id="@+id/image"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_gravity="center"
        android:adjustViewBounds="true"
        android:contentDescription="@string/descr_image" />

    <ProgressBar
        android:id="@+id/loading"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_gravity="center"
        android:visibility="gone" />

    <ImageView
        android:id="@+id/setwallbtn"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_gravity="bottom|center"
        android:adjustViewBounds="true"
        android:clickable="true"
        android:contentDescription="@string/setwall"
        android:maxHeight="55dp"
        android:onClick="setWall"
        android:src="@drawable/setwallpaper" />

</FrameLayout>

imagepageractivity.java

import java.io.IOException;
import android.app.WallpaperManager;
import android.graphics.Bitmap;
import android.graphics.Canvas;
import android.graphics.Color;
import android.graphics.drawable.Drawable;
import android.os.Bundle;
import android.os.Parcelable;
import android.support.v4.view.PagerAdapter;
import android.support.v4.view.ViewPager;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ImageView;
import android.widget.ProgressBar;
import android.widget.Toast;
import com.nostra13.universalimageloader.core.DisplayImageOptions;
import com.nostra13.universalimageloader.core.assist.FailReason;
import com.nostra13.universalimageloader.core.assist.ImageScaleType;
import com.nostra13.universalimageloader.core.assist.SimpleImageLoadingListener;
import com.rwilco12.wallpapergallery.Constants.Extra;

/**
 * @original author Sergey Tarasevich (nostra13[at]gmail[dot]com)
 */
public class ImagePagerActivity extends BaseActivity {

    private static final String STATE_POSITION = "STATE_POSITION";

    DisplayImageOptions options;

    ViewPager pager;

    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.pageview);

        Bundle bundle = getIntent().getExtras();
        String[] imageUrls = bundle.getStringArray(Extra.IMAGES);
        int pagerPosition = bundle.getInt(Extra.IMAGE_POSITION, 0);

        if (savedInstanceState != null) {
            pagerPosition = savedInstanceState.getInt(STATE_POSITION);
        }

        options = new DisplayImageOptions.Builder()
            .showImageForEmptyUri(R.drawable.error)
            .showImageOnFail(R.drawable.error)
            .resetViewBeforeLoading()
            .cacheOnDisc()
            .bitmapConfig(Bitmap.Config.RGB_565)
            .resetViewBeforeLoading()
            .imageScaleType(ImageScaleType.IN_SAMPLE_INT)
            .build();

        pager = (ViewPager) findViewById(R.id.pager);
        pager.setAdapter(new ImagePagerAdapter(imageUrls));
        pager.setCurrentItem(pagerPosition);
    }

     //Begin code from: https://stackoverflow.com/questions/11938182/android-set-image-as-wallpaper
     // fetch bitmap from view
    public static Bitmap getBitmapFromView(View view) {
        Bitmap returnedBitmap = Bitmap.createBitmap(view.getWidth(), view
                .getHeight(), Bitmap.Config.ARGB_8888);
        Canvas canvas = new Canvas(returnedBitmap);
        Drawable bgDrawable = view.getBackground();
        if (bgDrawable != null)
            bgDrawable.draw(canvas);
        else
            // if we unable to get background drawable then we will set white color as wallpaper
            canvas.drawColor(Color.WHITE);
        view.draw(canvas);
        return returnedBitmap;
    }

    public void setWall(int i) {
        WallpaperManager myWallpaperManager = WallpaperManager.getInstance(getApplicationContext());
        try {
            // below line of code will set your current visible pager item to wallpaper
            // first we have to fetch bitmap from visible view and then we can pass it to wallpaper
            myWallpaperManager.setBitmap(getBitmapFromView(pager.getChildAt(1)));

            // below line of code will set input stream data directly to wallpaper
            // myWallpaperManager.setStream(InputStream Data);

            // below line of code will set any image which is in the drawable folder 
            // myWallpaperManager.setResource(R.drawable.icon);
        } catch (IOException e) {
            e.printStackTrace();
        }
    }
    //End code from: https://stackoverflow.com/questions/11938182/android-set-image-as-wallpaper


    @Override
    public void onSaveInstanceState(Bundle outState) {
        outState.putInt(STATE_POSITION, pager.getCurrentItem());
    }

    private class ImagePagerAdapter extends PagerAdapter {

        private String[] images;
        private LayoutInflater inflater;

        ImagePagerAdapter(String[] images) {
            this.images = images;
            inflater = getLayoutInflater();
        }

        @Override
        public void destroyItem(ViewGroup container, int position, Object object) {
            ((ViewPager) container).removeView((View) object);
        }

        @Override
        public void finishUpdate(View container) {
        }

        @Override
        public int getCount() {
            return images.length;
        }





        @Override
        public Object instantiateItem(ViewGroup view, int position) {
            View imageLayout = inflater.inflate(R.layout.pageview_item, view, false);
            ImageView imageView = (ImageView) imageLayout.findViewById(R.id.image);
            final ProgressBar spinner = (ProgressBar) imageLayout.findViewById(R.id.loading);

            imageLoader.displayImage(images[position], imageView, options, new SimpleImageLoadingListener() {
                @Override
                public void onLoadingStarted(String imageUri, View view) {
                    spinner.setVisibility(View.VISIBLE);
                }

                @Override
                public void onLoadingFailed(String imageUri, View view, FailReason failReason) {
                    String message = null;
                    switch (failReason.getType()) {
                        case IO_ERROR:
                            message = "Input/Output error";
                            break;
                        case DECODING_ERROR:
                            message = "Image can't be decoded";
                            break;
                        case NETWORK_DENIED:
                            message = "Downloads are denied";
                            break;
                        case OUT_OF_MEMORY:
                            message = "Out Of Memory error";
                            break;
                        case UNKNOWN:
                            message = "Unknown error";
                            break;
                    }
                    Toast.makeText(ImagePagerActivity.this, message, Toast.LENGTH_SHORT).show();

                    spinner.setVisibility(View.GONE);
                }

                @Override
                public void onLoadingComplete(String imageUri, View view, Bitmap loadedImage) {
                    spinner.setVisibility(View.GONE);
                }
            });

            ((ViewPager) view).addView(imageLayout, 0);
            return imageLayout;
        }

        @Override
        public boolean isViewFromObject(View view, Object object) {
            return view.equals(object);
        }

        @Override
        public void restoreState(Parcelable state, ClassLoader loader) {
        }

        @Override
        public Parcelable saveState() {
            return null;
        }

        @Override
        public void startUpdate(View container) {
        }
    }

日志错误

09-28 10:01:36.334: W/dalvikvm(26948): threadid=1: thread exiting with uncaught exception (group=0x420772a0)
09-28 10:01:36.334: E/AndroidRuntime(26948): FATAL EXCEPTION: main
09-28 10:01:36.334: E/AndroidRuntime(26948): java.lang.IllegalStateException: Could not find a method setWall(View) in the activity class com.rwilco12.wallpapergallery.ImagePagerActivity for onClick handler on view class android.widget.ImageView with id 'setwallbtn'
09-28 10:01:36.334: E/AndroidRuntime(26948):    at android.view.View$1.onClick(View.java:3686)
09-28 10:01:36.334: E/AndroidRuntime(26948):    at android.view.View.performClick(View.java:4223)
09-28 10:01:36.334: E/AndroidRuntime(26948):    at android.view.View$PerformClick.run(View.java:17281)
09-28 10:01:36.334: E/AndroidRuntime(26948):    at android.os.Handler.handleCallback(Handler.java:615)
09-28 10:01:36.334: E/AndroidRuntime(26948):    at android.os.Handler.dispatchMessage(Handler.java:92)
09-28 10:01:36.334: E/AndroidRuntime(26948):    at android.os.Looper.loop(Looper.java:137)
09-28 10:01:36.334: E/AndroidRuntime(26948):    at android.app.ActivityThread.main(ActivityThread.java:4898)
09-28 10:01:36.334: E/AndroidRuntime(26948):    at java.lang.reflect.Method.invokeNative(Native Method)
09-28 10:01:36.334: E/AndroidRuntime(26948):    at java.lang.reflect.Method.invoke(Method.java:511)
09-28 10:01:36.334: E/AndroidRuntime(26948):    at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:1008)
09-28 10:01:36.334: E/AndroidRuntime(26948):    at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:775)
09-28 10:01:36.334: E/AndroidRuntime(26948):    at de.robv.android.xposed.XposedBridge.main(XposedBridge.java:106)
09-28 10:01:36.334: E/AndroidRuntime(26948):    at dalvik.system.NativeStart.main(Native Method)
09-28 10:01:36.334: E/AndroidRuntime(26948): Caused by: java.lang.NoSuchMethodException: setWall [class android.view.View]
09-28 10:01:36.334: E/AndroidRuntime(26948):    at java.lang.Class.getConstructorOrMethod(Class.java:460)
09-28 10:01:36.334: E/AndroidRuntime(26948):    at java.lang.Class.getMethod(Class.java:915)
09-28 10:01:36.334: E/AndroidRuntime(26948):    at android.view.View$1.onClick(View.java:3679)
09-28 10:01:36.334: E/AndroidRuntime(26948):    ... 12 more
4

1 回答 1

0

尝试设置 onclick 以匹配 lolcat 想要的

像这样的东西:

public void setWall(View v) {
    WallpaperManager myWallpaperManager = WallpaperManager.getInstance(getApplicationContext());
    try {
        // below line of code will set your current visible pager item to wallpaper
        // first we have to fetch bitmap from visible view and then we can pass it to wallpaper
        myWallpaperManager.setBitmap(getBitmapFromView(v));

    } catch (IOException e) {
        e.printStackTrace();
    }
}
于 2013-10-04T00:25:54.373 回答