• After 15+ years, we've made a big change: Android Forums is now Early Bird Club. Learn more here.

Apps changing activity background in a loop.

arkashkin

Lurker
Hello,
I'm building a simple game.
While the user is playing the game, I want the background to be changed in a loop.
I have created I new thread which need should do the background replacing job.
The layout of the game is build from an xml file which looks like this:

Code:
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout android:layout_width="fill_parent"
                android:layout_height="fill_parent"
                android:padding ="20dp"
                  android:id="@+id/game_main_layout"
                  android:background="@drawable/game_3"
                xmlns:android="http://schemas.android.com/apk/res/android">
    <TableLayout android:layout_width="fill_parent"
                  android:layout_height="wrap_content"
                  android:layout_alignParentLeft="true"
                  android:id="@+id/tableLayout1"
                  android:layout_gravity="center"
                  android:background="@color/game_background"
                  >
          <TableRow>
              <Button android:text="Button" android:id="@+id/button1" android:layout_width="fill_parent" android:layout_height="fill_parent" android:layout_weight="1" />
              <Button android:text="Button" android:id="@+id/button2" android:layout_width="fill_parent" android:layout_height="fill_parent" android:layout_weight="1" />
              <Button android:text="Button" android:id="@+id/button3" android:layout_width="fill_parent" android:layout_height="fill_parent" android:layout_weight="1" />
              <Button android:text="Button" android:id="@+id/button4" android:layout_width="fill_parent" android:layout_height="fill_parent" android:layout_weight="1" />
          </TableRow>
          <TableRow>
              <Button android:text="Button" android:id="@+id/button5" android:layout_width="fill_parent" android:layout_height="wrap_content" android:layout_weight="1" />
              <Button android:text="Button" android:id="@+id/button6" android:layout_width="fill_parent" android:layout_height="wrap_content" android:layout_weight="1" />
              <Button android:text="Button" android:id="@+id/button7" android:layout_width="fill_parent" android:layout_height="wrap_content" android:layout_weight="1" />
              <Button android:text="Button" android:id="@+id/button8" android:layout_width="fill_parent" android:layout_height="wrap_content" android:layout_weight="1" />          
          </TableRow>
          <TableRow>
              <Button android:text="Button" android:id="@+id/button9" android:layout_width="fill_parent" android:layout_height="wrap_content" android:layout_weight="1" />
              <Button android:text="Button" android:id="@+id/button10" android:layout_width="fill_parent" android:layout_height="wrap_content" android:layout_weight="1" />
              <Button android:text="Button" android:id="@+id/button11" android:layout_width="fill_parent" android:layout_height="wrap_content" android:layout_weight="1" />
              <Button android:text="Button" android:id="@+id/button12" android:layout_width="fill_parent" android:layout_height="wrap_content" android:layout_weight="1" />          
          </TableRow>
          <TableRow>
              <Button android:text="Button" android:id="@+id/button13" android:layout_width="fill_parent" android:layout_height="wrap_content" android:layout_weight="1" />
              <Button android:text="Button" android:id="@+id/button14" android:layout_width="fill_parent" android:layout_height="wrap_content" android:layout_weight="1" />
              <Button android:text="Button" android:id="@+id/button15" android:layout_width="fill_parent" android:layout_height="wrap_content" android:layout_weight="1" />
              <Button android:text="Button" android:id="@+id/button16" android:layout_width="fill_parent" android:layout_height="wrap_content" android:layout_weight="1" />          
          </TableRow>
    </TableLayout>
    
</LinearLayout>

as you can see the is an liniar layout which id is - "game_main_layout"
In my thread, i'm trying to tell it to get this layout, like this:

Code:
LinearLayout someView = (LinearLayout)findViewById(R.id.game_main_layout);

but some how i get null.

Some body can explain to me why? I do I do this properly?

Thanks in advance.
 
You are doing that right ... the problem is probably your R reference did you import the R for your project

Or did you start that with the setContentView
 
You are doing that right ... the problem is probably your R reference did you import the R for your project

Or did you start that with the setContentView

I didn't import R, I use:

Code:
setContentView(R.layout.game);

This is how I load the activity... (my xml file is called - game.xml)
 
And make sure that setContentView() is the very first thing that happens in your onCreate, right after super.onCreate!.
Sometimes people tend to put initial stuff in before the setContentView, which in some cases are fine, but if you put any layout stuff there, it'll return null pointers...

And then, as #4 says, Android is not designed to let other threads than the UI thread handle UI changes - so that could be part of the problem. Either use a Handler as #4 suggests or make a "AsyncTask" instead of a thread. AsyncTask works like a thread but CAN handle UI...
 
Back
Top Bottom