简体   繁体   English

Android,如何定义onAttachedToWindow()方法?

[英]Android, how to define onAttachedToWindow() method?

I am using gradient as background in my activity. 我在活动中使用渐变作为背景。 on some android devices it doesn't look as good and smooth as in Photoshop, to fix this issue somebody told me use onAttachedToWindow() method. 在某些android设备上,它看起来不像在Photoshop中那样流畅。要解决此问题,有人告诉我使用onAttachedToWindow()方法。

I checked Android page ( http://developer.android.com/reference/android/app/Activity.html#onAttachedToWindow() ) and I found that this method is a part of android.app.Activity and I wrote following lines of code: 我检查了Android页面( http://developer.android.com/reference/android/app/Activity.html#onAttachedToWindow() ),发现此方法是android.app.Activity的一部分,并在码:

package com.test.test1;

import android.app.Activity;
import android.graphics.PixelFormat;
import android.os.Bundle;
import android.view.Window;

public class Mainctivity extends Activity {

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

    @Override
    public void onAttachedToWindow() { 
        super.onAttachedToWindow(); 
        Window window = getWindow(); 
        window.setFormat(PixelFormat.RGBA_8888); 
    }
}

but when run the emulator, it crashed and in DDMS I saw this error: 但是运行模拟器时,它崩溃了,在DDMS中我看到了这个错误:

11-25 10:48:13.353: E/dalvikvm(216): Could not find method android.app.Activity.onAttachedToWindow, referenced from method com.test.test1.MainActivity.onAttachedToWindow

What is my fault? 我怎么了

此方法自API级别5起可用。模拟器上正在运行什么版本的Android?

As per the comments above, I've had this code tested on an actual device and it worked smoothly. 根据上面的评论,我已经在实际设备上测试了此代码,并且运行顺利。 So this is an emulator problem. 因此,这是一个模拟器问题。 Hopefully this will be resolved in later versions of the sdk. 希望这将在更高版本的sdk中解决。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM