gpt4 book ai didi

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

转载 作者:行者123 更新时间:2023-11-29 22:20:20 30 4
gpt4 key购买 nike

我有一个问题,我正在尝试让我的应用程序背景根据计时器每十秒更改一次...我已经做了我能做但不能解决的事情,因为我是 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/

30 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com