1

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

}

Bercik
  • 139
  • 1
  • 14

1 Answers1

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.

Yashwanth Kumar
  • 28,931
  • 15
  • 65
  • 69
  • wow what a quick answer :) so something like: proctected Drawable getDrawable (image,image) or is it something else – Bercik Sep 13 '11 at 21:52
  • or is it return drawable/imgage or something? – Bercik Sep 13 '11 at 21:54
  • return getResources().getDrawable(images[i%2]); – Yashwanth Kumar Sep 13 '11 at 21:59
  • sorry for troubling you further ;) but where do i put my drawable, and is it something R.drawable.zzz ??? – Bercik Sep 13 '11 at 22:13
  • k, there are some drawable mentioned in your images array, you have to find what they are and put them in drawable folder under res. – Yashwanth Kumar Sep 13 '11 at 22:46
  • http://stackoverflow.com/questions/6795249/android-eclipse-how-can-i-add-an-image-in-the-res-drawable-folder, follow this link – Yashwanth Kumar Sep 13 '11 at 22:46
  • yes i got that :D its just that when i added the code you suggested it still force closes when the timer goes :-/ im so close yet so far been trying to solve this for 3days now – Bercik Sep 13 '11 at 22:56
  • there must a problem, in getting the drawable resource then, i suggest you to search for "how to get a drawable resource programatcially in android" – Yashwanth Kumar Sep 13 '11 at 22:59
  • yes i though it would be something like that , thank you very much for your help (and your time ;)), ill get back to you tommorow if something happens as im going offline now thanks – Bercik Sep 13 '11 at 23:04
  • hello @Yashwanth Kumar could you tell me what you thinh (i have edited the post and added some more detail, and have fixed the force close issue) thanks – Bercik Sep 15 '11 at 08:19
  • seems ok for me as for the returning part. – Yashwanth Kumar Sep 15 '11 at 13:45
  • Same for me :) I dont know if you read my edit, but with the new issue know the timer works now, as I checked it, its just it only uses the first image from the list ... any ideas? – Bercik Sep 15 '11 at 16:27
  • replace i%2 by i , just as a try ,in case i was missing something. – Yashwanth Kumar Sep 15 '11 at 16:36
  • i think its not best to proceed this discussion any further here, you can email me at yashu336iitkgp@gmail.com , if you have any problems. – Yashwanth Kumar Sep 15 '11 at 16:53