-5

这个想法是你可以水平滚动图像,如果你点击一个,它会在按钮上方显示放大,如果你按下按钮,它会将图像设置为墙纸。但是我的应用程序在运行时崩溃:

这是xml代码:

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:orientation="vertical" >

    <ImageView
        android:id="@+id/IVDisplay"
        android:layout_width="wrap_content"
        android:layout_height="220dp"
        android:layout_marginBottom="15dp"
        android:layout_marginTop="15dp"
        android:contentDescription="@string/button3_set"
        android:padding="10dp"
        android:src="@drawable/back_1" />

    <Button
        android:id="@+id/bSetBG"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_marginLeft="22dp"
        android:text="@string/button3_set" />

    <HorizontalScrollView
        android:id="@+id/gallery1"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_marginLeft="30dp"
        android:layout_marginRight="30dp"
        android:layout_marginBottom="10dp"
        android:scrollbarFadeDuration="200"
        android:scrollbarStyle="outsideOverlay"
        android:layout_gravity="center" >

        <LinearLayout
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:orientation="horizontal" >

            <ImageView
                android:id="@+id/Vimage1"
                android:layout_width="wrap_content"
                android:layout_height="153dp"
                android:contentDescription="@string/button3_imgprev"
                android:padding="10dp"
                android:src="@drawable/back_1" />

            <ImageView
                android:id="@+id/Vimage2"
                android:layout_width="wrap_content"
                android:layout_height="153dp"
                android:contentDescription="@string/button3_imgprev"
                android:padding="10dp"
                android:src="@drawable/back_2" />

            <ImageView
                android:id="@+id/Vimage3"
                android:layout_width="wrap_content"
                android:layout_height="153dp"
                android:contentDescription="@string/button3_imgprev"
                android:padding="10dp"
                android:src="@drawable/back_3" />

            <ImageView
                android:id="@+id/Vimage4"
                android:layout_width="wrap_content"
                android:layout_height="153dp"
                android:contentDescription="@string/button3_imgprev"
                android:padding="10dp"
                android:src="@drawable/back_4" />

            <ImageView
                android:id="@+id/Vimage5"
                android:layout_width="wrap_content"
                android:layout_height="153dp"
                android:contentDescription="@string/button3_imgprev"
                android:padding="10dp"
                android:src="@drawable/back_5" />

            <ImageView
                android:id="@+id/Vimage6"
                android:layout_width="wrap_content"
                android:layout_height="153dp"
                android:contentDescription="@string/button3_imgprev"
                android:padding="10dp"
                android:src="@drawable/back_6" />

            <ImageView
                android:id="@+id/Vimage7"
                android:layout_width="wrap_content"
                android:layout_height="153dp"
                android:contentDescription="@string/button3_imgprev"
                android:padding="10dp"
                android:src="@drawable/back_7" />

            <ImageView
                android:id="@+id/Vimage8"
                android:layout_width="wrap_content"
                android:layout_height="153dp"
                android:contentDescription="@string/button3_imgprev"
                android:padding="10dp"
                android:src="@drawable/back_8" />

            <ImageView
                android:id="@+id/Vimage9"
                android:layout_width="wrap_content"
                android:layout_height="153dp"
                android:contentDescription="@string/button3_imgprev"
                android:padding="10dp"
                android:src="@drawable/back_9" />

            <ImageView
                android:id="@+id/Vimage10"
                android:layout_width="wrap_content"
                android:layout_height="153dp"
                android:contentDescription="@string/button3_imgprev"
                android:padding="10dp"
                android:src="@drawable/back_wallpaper" />
        </LinearLayout>
    </HorizontalScrollView>

</LinearLayout>

和java:

package com.example.st;

import java.io.IOException;
import android.app.Activity;
import android.app.WallpaperManager;
import android.graphics.Bitmap;
import android.graphics.BitmapFactory;
import android.os.Bundle;
import android.view.View;
import android.widget.Button;
import android.widget.ImageView;
import android.widget.Toast;

public class Button3 extends Activity implements View.OnClickListener {

    ImageView display;
    int toPhone;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        // TODO Auto-generated method stub
        super.onCreate(savedInstanceState);
        setContentView(R.layout.button_3);

        display = (ImageView) findViewById(R.id.IVDisplay);
        ImageView image1 = (ImageView) findViewById(R.id.Vimage1);
        ImageView image2 = (ImageView) findViewById(R.id.Vimage2);
        ImageView image3 = (ImageView) findViewById(R.id.Vimage3);
        ImageView image4 = (ImageView) findViewById(R.id.Vimage4);
        ImageView image5 = (ImageView) findViewById(R.id.Vimage5);
        ImageView image6 = (ImageView) findViewById(R.id.Vimage6);
        ImageView image7 = (ImageView) findViewById(R.id.Vimage7);
        ImageView image8 = (ImageView) findViewById(R.id.Vimage8);
        ImageView image9 = (ImageView) findViewById(R.id.Vimage9);
        ImageView image10 = (ImageView) findViewById(R.id.Vimage10);

        Button setWall = (Button) findViewById(R.id.bSetBG);

        toPhone = R.drawable.back_1;

        image1.setOnClickListener(this);
        image2.setOnClickListener(this);
        image3.setOnClickListener(this);
        image4.setOnClickListener(this);
        image5.setOnClickListener(this);
        image6.setOnClickListener(this);
        image7.setOnClickListener(this);
        image8.setOnClickListener(this);
        image9.setOnClickListener(this);
        image10.setOnClickListener(this);

        setWall.setOnClickListener(this);

    }

    @Override
    public void onClick(View v) {
        // TODO Auto-generated method stub

        switch (v.getId()) {

        case R.id.Vimage1:
            display.setImageResource(R.drawable.back_1);
            toPhone = R.drawable.back_1;
            break;

        case R.id.Vimage2:
            display.setImageResource(R.drawable.back_2);
            toPhone = R.drawable.back_2;
            break;
        case R.id.Vimage3:
            display.setImageResource(R.drawable.back_3);
            toPhone = R.drawable.back_3;
            break;
        case R.id.Vimage4:
            display.setImageResource(R.drawable.back_4);
            toPhone = R.drawable.back_4;
            break;
        case R.id.Vimage5:
            display.setImageResource(R.drawable.back_5);
            toPhone = R.drawable.back_5;
            break;
        case R.id.Vimage6:
            display.setImageResource(R.drawable.back_6);
            toPhone = R.drawable.back_6;
            break;
        case R.id.Vimage7:
            display.setImageResource(R.drawable.back_7);
            toPhone = R.drawable.back_7;
            break;
        case R.id.Vimage8:
            display.setImageResource(R.drawable.back_8);
            toPhone = R.drawable.back_8;
            break;
        case R.id.Vimage9:
            display.setImageResource(R.drawable.back_9);
            toPhone = R.drawable.back_9;
            break;
        case R.id.Vimage10:
            display.setImageResource(R.drawable.back_wallpaper);
            toPhone = R.drawable.back_wallpaper;
            break;

        case R.id.bSetBG:

            WallpaperManager myWallpaperManager = WallpaperManager.getInstance(getApplicationContext());

            Bitmap whatever = BitmapFactory.decodeStream(getResources()
                    .openRawResource(toPhone));

            try {
                myWallpaperManager.setBitmap(whatever);
            } catch (IOException e) {
                e.printStackTrace();
                Toast.makeText(Button3.this, 
                           "Ooops, couldn't set the wallpaper", 
                           Toast.LENGTH_SHORT).show();
            }

        }

    }

}

以下是 LogCat 显示的错误:

- 07-24 00:53:44.524: E/AndroidRuntime(4663): FATAL EXCEPTION: main

- 07-24 00:53:44.524: E/AndroidRuntime(4663): java.lang.RuntimeException: Unable to start activity ComponentInfo{com.example.st/com.example.st.Button3}: android.view.InflateException: Binary XML file line #56: Error inflating class <unknown>

- 07-24 00:53:44.524: E/AndroidRuntime(4663):   at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2180)

- 07-24 00:53:44.524: E/AndroidRuntime(4663):   at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2230)

- 07-24 00:53:44.524: E/AndroidRuntime(4663):   at android.app.ActivityThread.access$600(ActivityThread.java:141)

- 07-24 00:53:44.524: E/AndroidRuntime(4663):   at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1234)

- 07-24 00:53:44.524: E/AndroidRuntime(4663):   at android.os.Handler.dispatchMessage(Handler.java:99)

- 07-24 00:53:44.524: E/AndroidRuntime(4663):   at android.os.Looper.loop(Looper.java:137)

- 07-24 00:53:44.524: E/AndroidRuntime(4663):   at android.app.ActivityThread.main(ActivityThread.java:5041)

- 07-24 00:53:44.524: E/AndroidRuntime(4663):   at java.lang.reflect.Method.invokeNative(Native Method)

- 07-24 00:53:44.524: E/AndroidRuntime(4663):   at java.lang.reflect.Method.invoke(Method.java:511)

- 07-24 00:53:44.524: E/AndroidRuntime(4663):   at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:793)

- 07-24 00:53:44.524: E/AndroidRuntime(4663):   at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:560)

- 07-24 00:53:44.524: E/AndroidRuntime(4663):   at dalvik.system.NativeStart.main(Native Method)

- 07-24 00:53:44.524: E/AndroidRuntime(4663): Caused by: android.view.InflateException: Binary XML file line #56: Error inflating class <unknown>

- 07-24 00:53:44.524: E/AndroidRuntime(4663):   at android.view.LayoutInflater.createView(LayoutInflater.java:613)

- 07-24 00:53:44.524: E/AndroidRuntime(4663):   at com.android.internal.policy.impl.PhoneLayoutInflater.onCreateView(PhoneLayoutInflater.java:56)

- 07-24 00:53:44.524: E/AndroidRuntime(4663):   at android.view.LayoutInflater.onCreateView(LayoutInflater.java:660)

- 07-24 00:53:44.524: E/AndroidRuntime(4663):   at android.view.LayoutInflater.createViewFromTag(LayoutInflater.java:685)

- 07-24 00:53:44.524: E/AndroidRuntime(4663):   at android.view.LayoutInflater.rInflate(LayoutInflater.java:746)

- 07-24 00:53:44.524: E/AndroidRuntime(4663):   at android.view.LayoutInflater.rInflate(LayoutInflater.java:749)

- 07-24 00:53:44.524: E/AndroidRuntime(4663):   at android.view.LayoutInflater.rInflate(LayoutInflater.java:749)

- 07-24 00:53:44.524: E/AndroidRuntime(4663):   at android.view.LayoutInflater.inflate(LayoutInflater.java:489)

- 07-24 00:53:44.524: E/AndroidRuntime(4663):   at android.view.LayoutInflater.inflate(LayoutInflater.java:396)

- 07-24 00:53:44.524: E/AndroidRuntime(4663):   at android.view.LayoutInflater.inflate(LayoutInflater.java:352)

- 07-24 00:53:44.524: E/AndroidRuntime(4663):   at com.android.internal.policy.impl.PhoneWindow.setContentView(PhoneWindow.java:270)

- 07-24 00:53:44.524: E/AndroidRuntime(4663):   at android.app.Activity.setContentView(Activity.java:1881)

- 07-24 00:53:44.524: E/AndroidRuntime(4663):   at com.example.st.Button3.onCreate(Button3.java:24)

- 07-24 00:53:44.524: E/AndroidRuntime(4663):   at android.app.Activity.performCreate(Activity.java:5104)

- 07-24 00:53:44.524: E/AndroidRuntime(4663):   at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1080)

- 07-24 00:53:44.524: E/AndroidRuntime(4663):   at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2144)

- 07-24 00:53:44.524: E/AndroidRuntime(4663):   ... 11 more

- 07-24 00:53:44.524: E/AndroidRuntime(4663): Caused by: java.lang.reflect.InvocationTargetException

- 07-24 00:53:44.524: E/AndroidRuntime(4663):   at java.lang.reflect.Constructor.constructNative(Native Method)

- 07-24 00:53:44.524: E/AndroidRuntime(4663):   at java.lang.reflect.Constructor.newInstance(Constructor.java:417)

- 07-24 00:53:44.524: E/AndroidRuntime(4663):   at android.view.LayoutInflater.createView(LayoutInflater.java:587)

- 07-24 00:53:44.524: E/AndroidRuntime(4663):   ... 26 more

- 07-24 00:53:44.524: E/AndroidRuntime(4663): Caused by: java.lang.OutOfMemoryError

- 07-24 00:53:44.524: E/AndroidRuntime(4663):   at android.graphics.BitmapFactory.nativeDecodeAsset(Native Method)

- 07-24 00:53:44.524: E/AndroidRuntime(4663):   at android.graphics.BitmapFactory.decodeStream(BitmapFactory.java:501)

- 07-24 00:53:44.524: E/AndroidRuntime(4663):   at android.graphics.BitmapFactory.decodeResourceStream(BitmapFactory.java:354)

- 07-24 00:53:44.524: E/AndroidRuntime(4663):   at android.graphics.drawable.Drawable.createFromResourceStream(Drawable.java:785)

- 07-24 00:53:44.524: E/AndroidRuntime(4663):   at android.content.res.Resources.loadDrawable(Resources.java:1965)

- 07-24 00:53:44.524: E/AndroidRuntime(4663):   at android.content.res.TypedArray.getDrawable(TypedArray.java:601)

- 07-24 00:53:44.524: E/AndroidRuntime(4663):   at android.widget.ImageView.<init>(ImageView.java:120)

- 07-24 00:53:44.524: E/AndroidRuntime(4663):   at android.widget.ImageView.<init>(ImageView.java:110)

- 07-24 00:53:44.524: E/AndroidRuntime(4663):   ... 29 more
4

1 回答 1

0

看起来你有一个OutOfMemory错误......

您的位图图像尺寸是多少?

也许您应该尝试android:largeHeap="true"在该部分中添加到您的 AndroidManifest 文件中application

于 2013-08-05T12:50:08.287 回答