android - 动态背景(在 LinearLayout 上)- 我的错误是什么?

标签 android dynamic timer background android-linearlayout

我有一个问题,我正在尝试让我的应用程序背景根据计时器每十秒更改一次...我已经做了我能做但不能解决的事情,因为我是 Java 和编程的初学者:) 如果有人可以更正我的代码,我会很高兴;)(我可以将它打包到手机中等 eclipse 不显示错误,但我的应用程序强制关闭,当计时器结束时),这里是:

public class CookBookActivity extends Activity {
    /** Called when the activity is first created. */

    private static final long GET_DATA_INTERVAL = 10000;
    int images[] = {R.drawable.smothie1,R.drawable.omletherb1};
    int index = 0;
    ImageView img;
    Handler hand = new Handler();
    private LinearLayout layout;

    public void onCreate(Bundle savedInstanceState)
    {
        super.onCreate(savedInstanceState);
        this.setContentView(R.layout.main);
        layout = (LinearLayout)findViewById(R.id.linearLayout1);
        hand.postDelayed(run, GET_DATA_INTERVAL);
    }

    Runnable run = new Runnable() {
        public void run() {
            layout.setBackgroundDrawable(getDrawable(images[index++]));
            if (index == images.length)
                index = 0;
            hand.postDelayed(run, GET_DATA_INTERVAL);


        Typeface tf2 = Typeface.createFromAsset(getAssets(),
                "fonts/BPreplay.otf");
        TextView tv2 = (TextView) findViewById(R.id.textView2);
        tv2.setTypeface(tf2);


        Typeface tf = Typeface.createFromAsset(getAssets(),
                "fonts/BPreplay.otf");
        TextView tv = (TextView) findViewById(R.id.textView1);
        tv.setTypeface(tf);


        Button mainNext = (Button) findViewById(R.id.nextScreen1);
        mainNext.setOnClickListener(new OnClickListener() {
            public void onClick(View v) {
                Intent i = new Intent();
                i.setClassName("com.unKnown.cookbook", "com.unKnown.cookbook.screen1");
                startActivity(i);

            }
        });
        }
    };

    protected Drawable getDrawable(int i) {
        // TODO Auto-generated method stub
        return null;
    }
}

编辑:

现在我终于解决了我的问题,我的图像设置为背景(感谢@Yashwanth Kumar 和我的帮助 :)),现在几乎没问题,但现在我只设置一张图像作为背景(每十秒)它设置了相同的图像),我认为这取决于以下两件事之一:

或者:

-handler 停止(我对此表示怀疑)- 我现在已经确认它可以工作并且每一次扫描它都会执行该过程,所以它归结为第二个问题

或:

它只使用列表中的第一张图片 (R.drawable.omletherb1),在这种情况下,我必须设置类似如果设置了 R.Drawable.zzz 然后设置图片 R.drawable.ccc

请告诉我你的想法,这是我现在得到的代码:

公共(public)类 CookBookActivity 扩展 Activity { /** 在第一次创建 Activity 时调用。 */

private static final long GET_DATA_INTERVAL = 1000;
int images[] = {R.drawable.omletherb1,R.drawable.smothie1};
int index = 0;
LinearLayout img;
Handler hand = new Handler();
private LinearLayout layout;

public void onCreate(Bundle savedInstanceState)
{
    super.onCreate(savedInstanceState);
    this.setContentView(R.layout.main);
    layout = (LinearLayout)findViewById(R.layout.main);
    hand.postDelayed(run, GET_DATA_INTERVAL);

    Typeface tf2 = Typeface.createFromAsset(getAssets(),
            "fonts/BPreplay.otf");
    TextView tv2 = (TextView) findViewById(R.id.textView2);
    tv2.setTypeface(tf2);


    Typeface tf = Typeface.createFromAsset(getAssets(),
            "fonts/BPreplay.otf");
    TextView tv = (TextView) findViewById(R.id.textView1);
    tv.setTypeface(tf);


    Button mainNext = (Button) findViewById(R.id.nextScreen1);
    mainNext.setOnClickListener(new OnClickListener() {
        public void onClick(View v) {
            Intent i = new Intent();
            i.setClassName("com.unKnown.cookbook", "com.unKnown.cookbook.screen1");
            startActivity(i);

        }
    });
}

Runnable run = new Runnable() {
    public void run() {
        layout.setBackgroundDrawable(getDrawable(images[index++]));
        if (index == images.length)
            index = 0;
        hand.postDelayed(run, GET_DATA_INTERVAL);

    }
};

protected Drawable getDrawable(int i) {
    // TODO Auto-generated method stub
    return getResources().getDrawable(images[i%2]);
}

最佳答案

layout.setBackgroundDrawable(getDrawable(images[index++]));

protected Drawable getDrawable(int i) {
    // TODO Auto-generated method stub
    return null;
}

这就是问题所在,您将 null 设置为背景。返回一些有效的可绘制对象,它将起作用。

关于android - 动态背景(在 LinearLayout 上)- 我的错误是什么?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/7408920/

相关文章:

android - 如何处理选项卡 Activity 中启动的 Activity 的屏幕方向更改

android - 在 LinearLayout 中对齐两个选择器

.net - 尽管有 timeBeginPeriod(1),为什么最小 Threading.Timer 间隔为 15ms

android - RxJava 1 安卓 : How to create a sequence of timers?

vb.net - 在 Windows 窗体应用程序中设置 'Timeout' 功能

android - 如何使用 Espresso 计数 RecyclerView 项目

java - 在网络浏览器中运行 Android APK 文件 - 这可能吗?

c# - 基于动态属性创建 RavenDB 索引

c++ - 在 C++ 中应该以什么顺序释放内存?

json - Oracle JSON 查询中的动态列列表