动态背景(上的LinearLayout) - 什么是我的错误? [英] Dynamic Background (on LinearLayout) - what is my error?

查看:163
本文介绍了动态背景(上的LinearLayout) - 什么是我的错误?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个问题,我试图让我的应用程序的背景改变,每个十秒钟,基于计时器...我为我是一个初学者到java和程序做了,我可以和着它的工作了:)我希望,如果有人可以只纠正我的code,请;)(我可以packgage它变成手机等日食犯规显示错误,但我的应用程序的力量关闭,当计时器进入),在这里它是:

 公共类CookBookActivity延伸活动{
    / **第一次创建活动时调用。 * /

    私有静态最后长GET_DATA_INTERVAL = 10000;
    INT图像[] = {R.drawable.smothie1,R.drawable.omletherb1};
    INT索引= 0;
    ImageView的IMG;
    处理器手=新的处理程序();
    私人的LinearLayout布局;

    公共无效的onCreate(包savedInstanceState)
    {
        super.onCreate(savedInstanceState);
        this.setContentView(R.layout.main);
        布局=(的LinearLayout)findViewById(R.id.linearLayout1);
        hand.postDelayed(运行,GET_DATA_INTERVAL);
    }

    Runnable运行=新的Runnable(){
        公共无效的run(){
            layout.setBackgroundDrawable(getDrawable(图像[指数++]));
            如果(指数== images.length)
                索引= 0;
            hand.postDelayed(运行,GET_DATA_INTERVAL);


        字样TF2 = Typeface.createFromAsset(getAssets()
                字体/ B preplay.otf);
        TextView的TV2 =(TextView中)findViewById(R.id.textView2);
        tv2.setTypeface(TF2);


        字样TF = Typeface.createFromAsset(getAssets()
                字体/ B preplay.otf);
        TextView的电视=(TextView中)findViewById(R.id.textView1);
        tv.setTypeface(TF);


        按钮mainNext =(按钮)findViewById(R.id.nextScreen1);
        mainNext.setOnClickListener(新OnClickListener(){
            公共无效的onClick(视图v){
                意图I =新意图();
                i.setClassName(com.unKnown.cookbook,com.unKnown.cookbook.screen1);
                startActivity(ⅰ);

            }
        });
        }
    };

    受保护的可绘制getDrawable(int i)以{
        // TODO自动生成方法存根
        返回null;
    }
}
 

修改

现在我已经finnaly解决了我的问题,我的形象套到后台(感谢@Yashwanth库马尔的帮助,我:)),它几乎是细了,但现在我只设置一个图像作为背景(每个10秒它设置相同的图像),我认为这是下跌到两个的下列事项:

或者

-handler停止(whcich我对此表示怀疑) - 我现在已经证实了它的工作原理,每一个swecond它的程序,所以它下降到第二个问题

它仅使用第一个图像从列表(R.drawable.omletherb1),在这种情况下,我将不得不设置的东西一样,如果R.Drawable.zzz设置然后执行设置的图像R.drawable.ccc

请告诉我你的想法,这里是tyhe code我现在已经结束了:

公共类CookBookActivity延伸活动{     / **第一次创建活动时调用。 * /

 私有静态最后长GET_DATA_INTERVAL = 1000;
INT图像[] = {R.drawable.omletherb1,R.drawable.smothie1};
INT索引= 0;
的LinearLayout IMG;
处理器手=新的处理程序();
私人的LinearLayout布局;

公共无效的onCreate(包savedInstanceState)
{
    super.onCreate(savedInstanceState);
    this.setContentView(R.layout.main);
    布局=(的LinearLayout)findViewById(R.layout.main);
    hand.postDelayed(运行,GET_DATA_INTERVAL);

    字样TF2 = Typeface.createFromAsset(getAssets()
            字体/ B preplay.otf);
    TextView的TV2 =(TextView中)findViewById(R.id.textView2);
    tv2.setTypeface(TF2);


    字样TF = Typeface.createFromAsset(getAssets()
            字体/ B preplay.otf);
    TextView的电视=(TextView中)findViewById(R.id.textView1);
    tv.setTypeface(TF);


    按钮mainNext =(按钮)findViewById(R.id.nextScreen1);
    mainNext.setOnClickListener(新OnClickListener(){
        公共无效的onClick(视图v){
            意图I =新意图();
            i.setClassName(com.unKnown.cookbook,com.unKnown.cookbook.screen1);
            startActivity(ⅰ);

        }
    });
}

Runnable运行=新的Runnable(){
    公共无效的run(){
        layout.setBackgroundDrawable(getDrawable(图像[指数++]));
        如果(指数== images.length)
            索引= 0;
        hand.postDelayed(运行,GET_DATA_INTERVAL);

    }
};

受保护的可绘制getDrawable(int i)以{
    // TODO自动生成方法存根
    返回getResources()getDrawable(图像[则i%2])。
}
 

}

解决方案

  layout.setBackgroundDrawable(getDrawable(图像[指数++]));

受保护的可绘制getDrawable(int i)以{
    // TODO自动生成方法存根
    返回null;
}
 

这就是问题所在,要设置为空的background.return一些有效的绘制,它会工作。

I have a problem, I'm trying to get my apps background to change, each ten seconds, based on a timer... I have done what i could and cant work it out as im a beginner to java and programming :) I would love if someone could just correct my code please ;) (I can packgage it into a phone etc eclipse doesnt show an error, but my app force closes, when the timer goes), here it is:

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;
    }
}

EDIT:

Now I have finnaly solved my problem and my image sets to background (thanks to @Yashwanth Kumar 's help and me :) ), Its almost fine now, but now my it only sets one image as background (each ten seconds it sets the same image), I think it's down to either of two of the following things:

either:

-handler stops (whcich i doubt)- I have now confirmed it works and every swecond it does the procedure, so it's down to second issue

or:

it only uses the first image from the list (R.drawable.omletherb1), in which case I'll have to set something like if R.Drawable.zzz is set then do set image R.drawable.ccc

Please tell me what you think, and here is tyhe code I have now ended up with:

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

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;
}

This is the problem, you are setting null to the background.return some valid drawable and it will work.

这篇关于动态背景(上的LinearLayout) - 什么是我的错误?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆