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

发布于 2024-12-04 16:43:17 字数 3917 浏览 0 评论 0原文

我有一个问题,我试图让我的应用程序背景根据计时器每十秒改变一次...我已经做了我能做的事情,但无法解决它,因为我是 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 的帮助和我:)),现在几乎没问题了,但现在我的它只设置一个图像作为背景(每十秒它设置相同的图像),我想这取决于以下两件事之一:

要么:

-处理程序停止(我对此表示怀疑)-我现在已经确认它可以工作,并且每个swecond都会执行该过程,所以它取决于第二个问题

,或者:

它只使用第一个 问题列表中的图像(R.drawable.omletherb1),在这种情况下,我必须设置类似的内容,如果设置了 R.Drawable.zzz,则设置图像 R.drawable.ccc

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

public class CookBookActivity extends 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]);
}

}

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

}

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

末が日狂欢 2024-12-11 16:43:17
layout.setBackgroundDrawable(getDrawable(images[index++]));

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

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

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.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文