奇怪的效果,无法启动活动

时间:2014-04-30 19:52:01

标签: android android-layout android-intent android-activity samsung-mobile

为何奇怪?虽然定制的应用程序在HTC One和HTC One X上都经过测试,但它们都表现良好,只有内存问题(HTC One X)。但是,当我在三星Galaxy S上测试这个应用程序时,它会加载所有内容,甚至是第二个菜单,但不会更进一步......任何想法? Java文件:

public class WallPaperTwoA extends Activity implements OnClickListener {

ImageView display;
int toPhone; 

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

    requestWindowFeature(Window.FEATURE_NO_TITLE);
    getWindow().setFlags(WindowManager.LayoutParams.FLAG_FULLSCREEN, WindowManager.LayoutParams.FLAG_KEEP_SCREEN_ON);

    setContentView(R.layout.wallpapertwoa);

    toPhone = R.drawable.art;

    display = (ImageView) findViewById(R.id.IVdisplay); 
    ImageView image1 = (ImageView) findViewById(R.id.IVimage1);
    ImageView image2 = (ImageView) findViewById(R.id.IVimage2);
    ImageView image3 = (ImageView) findViewById(R.id.IVimage3);
    ImageView image4 = (ImageView) findViewById(R.id.IVimage4);
    ImageView image5 = (ImageView) findViewById(R.id.IVimage5);
    ImageView image6 = (ImageView) findViewById(R.id.IVimage6);
    ImageView image7 = (ImageView) findViewById(R.id.IVimage7);
    ImageView image8 = (ImageView) findViewById(R.id.IVimage8);
    ImageView image9 = (ImageView) findViewById(R.id.IVimage9);
    ImageView image10 = (ImageView) findViewById(R.id.IVimage10);
    ImageView image11 = (ImageView) findViewById(R.id.IVimage11);
    ImageView image12 = (ImageView) findViewById(R.id.IVimage12);
    ImageView image13 = (ImageView) findViewById(R.id.IVimage13);
    ImageView image14 = (ImageView) findViewById(R.id.IVimage14);
    ImageView image15 = (ImageView) findViewById(R.id.IVimage15);
    ImageView image16 = (ImageView) findViewById(R.id.IVimage16);
    ImageView image17 = (ImageView) findViewById(R.id.IVimage17);
    ImageView image18 = (ImageView) findViewById(R.id.IVimage18);
    ImageView image19 = (ImageView) findViewById(R.id.IVimage19);
    ImageView image20 = (ImageView) findViewById(R.id.IVimage20);
    ImageView image21 = (ImageView) findViewById(R.id.IVimage21);
    Button setWall = (Button) findViewById(R.id.BsetWallpaper);

    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);
    image11.setOnClickListener(this);
    image12.setOnClickListener(this);
    image13.setOnClickListener(this);
    image14.setOnClickListener(this);
    image15.setOnClickListener(this);
    image16.setOnClickListener(this);
    image17.setOnClickListener(this);
    image18.setOnClickListener(this);
    image19.setOnClickListener(this);
    image20.setOnClickListener(this);
    image21.setOnClickListener(this);
    setWall.setOnClickListener(this);
}

@Override
public void onClick(View v) {
    // TODO Auto-generated method stub
    switch (v.getId()){
    case R.id.IVimage1:
        display.setImageResource(R.drawable.art);
        toPhone = R.drawable.art;
        break;
    case R.id.IVimage2:
        display.setImageResource(R.drawable.art1);
        toPhone = R.drawable.art1;
        break;
    case R.id.IVimage3:
        display.setImageResource(R.drawable.art2);
        toPhone = R.drawable.art2;
        break;
    case R.id.IVimage4:
        display.setImageResource(R.drawable.art3);
        toPhone = R.drawable.art3;
        break;
    case R.id.IVimage5:
        display.setImageResource(R.drawable.art4);
        toPhone = R.drawable.art4;
        break;
    case R.id.IVimage6:
        display.setImageResource(R.drawable.art6);
        toPhone = R.drawable.art6;
        break;
    case R.id.IVimage7:
        display.setImageResource(R.drawable.art7);
        toPhone = R.drawable.art7;
        break;
    case R.id.IVimage8:
        display.setImageResource(R.drawable.art8);
        toPhone = R.drawable.art8;
        break;
    case R.id.IVimage9:
        display.setImageResource(R.drawable.art9);
        toPhone = R.drawable.art9;
        break;
    case R.id.IVimage10:
        display.setImageResource(R.drawable.art10);
        toPhone = R.drawable.art10;
        break;
    case R.id.IVimage11:
        display.setImageResource(R.drawable.art11);
        toPhone = R.drawable.art11;
        break;
    case R.id.IVimage12:
        display.setImageResource(R.drawable.art12);
        toPhone = R.drawable.art12;
        break;
    case R.id.IVimage13:
        display.setImageResource(R.drawable.art13);
        toPhone = R.drawable.art13;
        break;
    case R.id.IVimage14:
        display.setImageResource(R.drawable.art14);
        toPhone = R.drawable.art14;
        break;
    case R.id.IVimage15:
        display.setImageResource(R.drawable.art15);
        toPhone = R.drawable.art15;
        break;
    case R.id.IVimage16:
        display.setImageResource(R.drawable.art16);
        toPhone = R.drawable.art16;
        break;
    case R.id.IVimage17:
        display.setImageResource(R.drawable.art17);
        toPhone = R.drawable.art17;
        break;
    case R.id.IVimage18:
        display.setImageResource(R.drawable.art18);
        toPhone = R.drawable.art18;
        break;
    case R.id.IVimage19:
        display.setImageResource(R.drawable.art19);
        toPhone = R.drawable.art19;
        break;
    case R.id.IVimage20:
        display.setImageResource(R.drawable.art20);
        toPhone = R.drawable.art20;
        break;
    case R.id.IVimage21:
        display.setImageResource(R.drawable.art21);
        toPhone = R.drawable.art21;
        break;
    case R.id.BsetWallpaper:

        DisplayMetrics metrics = new DisplayMetrics(); 
        getWindowManager().getDefaultDisplay().getMetrics(metrics);
        // get the height and width of screen 
        int height = metrics.heightPixels; 
        int width = metrics.widthPixels;

        try {
            WallpaperManager wallpaperManager = WallpaperManager.getInstance(this);
            int targetWidth = wallpaperManager.getDesiredMinimumWidth();
            int targetHeight = wallpaperManager.getDesiredMinimumHeight();
            Bitmap source = BitmapFactory.decodeResource(getResources(), toPhone);
            int sourceWidth = source.getWidth();
            int sourceHeight = source.getHeight();
            Bitmap target = Bitmap.createBitmap(targetWidth, targetHeight, Bitmap.Config.RGB_565);
            Canvas canvas = new Canvas(target);
            canvas.drawColor(Color.BLACK);
            canvas.drawBitmap(source, sourceWidth < targetWidth ? (targetWidth-sourceWidth)/2 : 0, sourceHeight < targetHeight ? (targetHeight-sourceHeight)/2 : 0, null);
            wallpaperManager.setBitmap(target);

                  wallpaperManager.suggestDesiredDimensions(width, height);
                 Toast.makeText(this, "Wallpaper Set", Toast.LENGTH_SHORT).show();
        }catch(IOException e){
            e.printStackTrace();
        }
        break;
    }

}

}

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"
    android:background="#000000" >

    <ImageView
        android:id="@+id/IVdisplay"
        android:layout_width="300dp"
        android:layout_height="280dp"
        android:layout_gravity="center"
        android:src="@drawable/art" />

    <Button android:id="@+id/BsetWallpaper"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:text="Set Image to background"
        android:background="#CC0000"
        android:textColor="#FFFFFF" />
<HorizontalScrollView android:layout_width="200dp" android:layout_height="wrap_content" android:layout_gravity="center" >
    <LinearLayout android:layout_width="match_parent" android:layout_height="wrap_content" android:orientation="horizontal" >

    <ImageView android:src="@drawable/art"
        android:id="@+id/IVimage1"
        android:layout_width="125dp"
        android:layout_height="125dp"
        android:padding="15dp" />
    <ImageView android:src="@drawable/art1"
        android:id="@+id/IVimage2"
        android:layout_width="125dp"
        android:layout_height="125dp"
        android:padding="15dp" />
    <ImageView android:src="@drawable/art2"
        android:id="@+id/IVimage3"
        android:layout_width="125dp"
        android:layout_height="125dp"
        android:padding="15dp" />
    <ImageView android:src="@drawable/art3"
        android:id="@+id/IVimage4"
        android:layout_width="125dp"
        android:layout_height="125dp"
        android:padding="15dp" />
    <ImageView android:src="@drawable/art4"
        android:id="@+id/IVimage5"
        android:layout_width="125dp"
        android:layout_height="125dp"
        android:padding="15dp" />
    <ImageView android:src="@drawable/art6"
        android:id="@+id/IVimage6"
        android:layout_width="125dp"
        android:layout_height="125dp"
        android:padding="15dp" />
    <ImageView android:src="@drawable/art7"
        android:id="@+id/IVimage7"
        android:layout_width="125dp"
        android:layout_height="125dp"
        android:padding="15dp" />
    <ImageView android:src="@drawable/art8"
        android:id="@+id/IVimage8"
        android:layout_width="125dp"
        android:layout_height="125dp"
        android:padding="15dp" />
    <ImageView android:src="@drawable/art9"
        android:id="@+id/IVimage9"
        android:layout_width="125dp"
        android:layout_height="125dp"
        android:padding="15dp" />
    <ImageView android:src="@drawable/art10"
        android:id="@+id/IVimage10"
        android:layout_width="125dp"
        android:layout_height="125dp"
        android:padding="15dp" />
    <ImageView android:src="@drawable/art11"
        android:id="@+id/IVimage11"
        android:layout_width="125dp"
        android:layout_height="125dp"
        android:padding="15dp" />
    <ImageView android:src="@drawable/art12"
        android:id="@+id/IVimage12"
        android:layout_width="125dp"
        android:layout_height="125dp"
        android:padding="15dp" />
    <ImageView android:src="@drawable/art13"
        android:id="@+id/IVimage13"
        android:layout_width="125dp"
        android:layout_height="125dp"
        android:padding="15dp" />
    <ImageView android:src="@drawable/art14"
        android:id="@+id/IVimage14"
        android:layout_width="125dp"
        android:layout_height="125dp"
        android:padding="15dp" />
    <ImageView android:src="@drawable/art15"
        android:id="@+id/IVimage15"
        android:layout_width="125dp"
        android:layout_height="125dp"
        android:padding="15dp" />
    <ImageView android:src="@drawable/art16"
        android:id="@+id/IVimage16"
        android:layout_width="125dp"
        android:layout_height="125dp"
        android:padding="15dp" />
    <ImageView android:src="@drawable/art17"
        android:id="@+id/IVimage17"
        android:layout_width="125dp"
        android:layout_height="125dp"
        android:padding="15dp" />
    <ImageView android:src="@drawable/art18"
        android:id="@+id/IVimage18"
        android:layout_width="125dp"
        android:layout_height="125dp"
        android:padding="15dp" />
    <ImageView android:src="@drawable/art19"
        android:id="@+id/IVimage19"
        android:layout_width="125dp"
        android:layout_height="125dp"
        android:padding="15dp" />
    <ImageView android:src="@drawable/art20"
        android:id="@+id/IVimage20"
        android:layout_width="125dp"
        android:layout_height="125dp"
        android:padding="15dp" />
    <ImageView android:src="@drawable/art21"
        android:id="@+id/IVimage21"
        android:layout_width="125dp"
        android:layout_height="125dp"
        android:padding="15dp" />
</LinearLayout>
</HorizontalScrollView>
</LinearLayout>

日志文件

04-30 21:27:13.614: D/skia(21641): --- decoder->decode returned false
04-30 21:27:13.614: D/AndroidRuntime(21641): Shutting down VM
04-30 21:27:13.614: W/dalvikvm(21641): threadid=1: thread exiting with uncaught exception (group=0x41cce700)
04-30 21:27:13.624: E/AndroidRuntime(21641): FATAL EXCEPTION: main
04-30 21:27:13.624: E/AndroidRuntime(21641): java.lang.RuntimeException: Unable to start activity ComponentInfo{nl.creation.fitwall/nl.creation.fitwall.WallPaperTwoA}: android.view.InflateException: Binary XML file line #119: Error inflating class <unknown>
04-30 21:27:13.624: E/AndroidRuntime(21641):    at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2295)
04-30 21:27:13.624: E/AndroidRuntime(21641):    at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2349)
04-30 21:27:13.624: E/AndroidRuntime(21641):    at android.app.ActivityThread.access$700(ActivityThread.java:159)
04-30 21:27:13.624: E/AndroidRuntime(21641):    at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1316)
04-30 21:27:13.624: E/AndroidRuntime(21641):    at android.os.Handler.dispatchMessage(Handler.java:99)
04-30 21:27:13.624: E/AndroidRuntime(21641):    at android.os.Looper.loop(Looper.java:176)
04-30 21:27:13.624: E/AndroidRuntime(21641):    at android.app.ActivityThread.main(ActivityThread.java:5419)
04-30 21:27:13.624: E/AndroidRuntime(21641):    at java.lang.reflect.Method.invokeNative(Native Method)
04-30 21:27:13.624: E/AndroidRuntime(21641):    at java.lang.reflect.Method.invoke(Method.java:525)
04-30 21:27:13.624: E/AndroidRuntime(21641):    at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:1    046)
04-30 21:27:13.624: E/AndroidRuntime(21641):    at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:862)
04-30 21:27:13.624: E/AndroidRuntime(21641):    at dalvik.system.NativeStart.main(Native Method)
04-30 21:27:13.624: E/AndroidRuntime(21641): Caused by: android.view.InflateException: Binary XML file line #119: Error inflating class <unknown>
04-30 21:27:13.624: E/AndroidRuntime(21641):    at android.view.LayoutInflater.createView(LayoutInflater.java:626)
04-30 21:27:13.624: E/AndroidRuntime(21641):    at com.android.internal.policy.impl.PhoneLayoutInflater.onCreateView(PhoneLayoutInflater.java:56)
04-30 21:27:13.624: E/AndroidRuntime(21641):    at android.view.LayoutInflater.onCreateView(LayoutInflater.java:675)
04-30 21:27:13.624: E/AndroidRuntime(21641):    at android.view.LayoutInflater.createViewFromTag(LayoutInflater.java:700)
04-30 21:27:13.624: E/AndroidRuntime(21641):    at android.view.LayoutInflater.rInflate(LayoutInflater.java:761)
04-30 21:27:13.624: E/AndroidRuntime(21641):    at android.view.LayoutInflater.rInflate(LayoutInflater.java:769)
04-30 21:27:13.624: E/AndroidRuntime(21641):    at android.view.LayoutInflater.rInflate(LayoutInflater.java:769)
04-30 21:27:13.624: E/AndroidRuntime(21641):    at android.view.LayoutInflater.inflate(LayoutInflater.java:498)
04-30 21:27:13.624: E/AndroidRuntime(21641):    at android.view.LayoutInflater.inflate(LayoutInflater.java:398)
04-30 21:27:13.624: E/AndroidRuntime(21641):    at android.view.LayoutInflater.inflate(LayoutInflater.java:354)
04-30 21:27:13.624: E/AndroidRuntime(21641):    at com.android.internal.policy.impl.PhoneWindow.setContentView(PhoneWindow.java:361)
04-30 21:27:13.624: E/AndroidRuntime(21641):    at android.app.Activity.setContentView(Activity.java:1956)
04-30 21:27:13.624: E/AndroidRuntime(21641):    at nl.creation.fitwall.WallPaperTwoA.onCreate(WallPaperTwoA.java:33)
04-30 21:27:13.624: E/AndroidRuntime(21641):    at android.app.Activity.performCreate(Activity.java:5372)
04-30 21:27:13.624: E/AndroidRuntime(21641):    at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:11    04)
04-30 21:27:13.624: E/AndroidRuntime(21641):    at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2257)
04-30 21:27:13.624: E/AndroidRuntime(21641):    ... 11 more
04-30 21:27:13.624: E/AndroidRuntime(21641): Caused by: java.lang.reflect.InvocationTargetException
04-30 21:27:13.624: E/AndroidRuntime(21641):    at java.lang.reflect.Constructor.constructNative(Native Method)
04-30 21:27:13.624: E/AndroidRuntime(21641):    at java.lang.reflect.Constructor.newInstance(Constructor.java:417)
04-30 21:27:13.624: E/AndroidRuntime(21641):    at android.view.LayoutInflater.createView(LayoutInflater.java:600)
04-30 21:27:13.624: E/AndroidRuntime(21641):    ... 26 more
04-30 21:27:13.624: E/AndroidRuntime(21641): Caused by: java.lang.OutOfMemoryError
04-30 21:27:13.624: E/AndroidRuntime(21641):    at android.graphics.BitmapFactory.nativeDecodeAsset(Native Method)
04-30 21:27:13.624: E/AndroidRuntime(21641):    at android.graphics.BitmapFactory.decodeStream(BitmapFactory.java:596)
04-30 21:27:13.624: E/AndroidRuntime(21641):    at android.graphics.BitmapFactory.decodeResourceStream(BitmapFactory.java:444)
04-30 21:27:13.624: E/AndroidRuntime(21641):    at android.graphics.drawable.Drawable.createFromResourceStream(Drawable.java:832)
04-30 21:27:13.624: E/AndroidRuntime(21641):    at android.content.res.Resources.loadDrawable(Resources.java:2988)
04-30 21:27:13.624: E/AndroidRuntime(21641):    at android.content.res.TypedArray.getDrawable(TypedArray.java:602)
04-30 21:27:13.624: E/AndroidRuntime(21641):    at android.widget.ImageView.<init>(ImageView.java:131)
04-30 21:27:13.624: E/AndroidRuntime(21641):    at android.widget.ImageView.<init>(ImageView.java:121)
04-30 21:27:13.624: E/AndroidRuntime(21641):    ... 29 more
04-30 21:32:22.599: I/Process(21641): Sending signal. PID: 21641 SIG: 9
04-30 21:32:23.574: D/dalvikvm(22039): GC_FOR_ALLOC freed 125K, 12% free 10211K/11500K, paused 26ms, total 26ms

1 个答案:

答案 0 :(得分:2)

三星Galaxy S是一款旧设备(4年前制造),只有512 MB的RAM可用,而HTC One X / One是更新的手机,提供1GB / 2GB的RAM。所以你的问题是你试图同时加载21张图片(它们是XML的硬编码),这就是你得到OutOfMemoryError的原因。他们确实会一起展示吗?我建议您以编程方式为ImageViews设置图像,当它们真正需要时 - 延迟加载。您可以通过使用Picasso

等图像加载库来获得优势