Assistance with understanding LunarLander code

by steve_macleod » Fri, 12 Dec 2008 23:49:25 GMT


Sponsored Links
 Hi,
I have been working through the lunarlander example application, in
an
attempt to understand the source code. I have run into a couple of
problems, and would really appreciate if someone could space a couple
of minutes to assist in may understanding.

The onCreate method contains the following:


   protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);


        // turn off the window's title bar
        requestWindowFeature(Window.FEATURE_NO_TITLE);


        // tell system to use the layout defined in our XML file
        setContentView(R.layout.lunar_layout);


        // get handles to the LunarView from XML, and its LunarThread
        mLunarView = (LunarView) findViewById(R.id.lunar);
        mLunarThread = mLunarView.getThread();


        // give the LunarView a handle to the TextView used for
messages
        mLunarView.setTextView((TextView) findViewById(R.id.text));


        if (savedInstanceState == null) {
            // we were just launched: set up a new game
            mLunarThread.setState(LunarThread.STATE_READY);
            Log.w(this.getClass().getName(), "SIS is null");
        } else {
            // we are being restored: resume a previous game
            mLunarThread.restoreState(savedInstanceState);
            Log.w(this.getClass().getName(), "SIS is nonnull");
        }
    }


I can see that we are performing the following:
        Turn off the title bar in the window
        Set the activity content to the lunar_layour layout file
        gets a reference to the 'lunar' layout
        gets a reference to the thread member of the LunarView class
        give the LunarView a reference to the textview for game
messages
        we then set the state of the lunar thread to STATE_READY,
which also
outputs a bunch of status messages


The problem is that after executution of setState, the code seems to
end. I can see no sections of code that actually creates the
LunarView
or LunarThread objects, which are required to start the game proper.
Obviously I am missing something here, I was wondering if anyone
could
assist! Thanks lots!


--~--~---------~--~----~------------~-------~--~----~



Assistance with understanding LunarLander code

by RichardS » Sat, 13 Dec 2008 01:19:55 GMT


 Steve,

This is my understanding of it:

The file res/layout/lunar_layout.xml describes the layout of the view.
The src/R.java file is auto generated: it equates object ids to names.

setContentView(R.layout.lunar_layout)

'reads'  lunar_layout.xml file and creates and instance of the
LunarView class and hence the View itself.
If you look in the XML file it contains an Id string "@+id/lunar":
this is one of the ids that R.java
defines a name for - in this case R.id.lunar.

mLunarView = (LunarView) findViewById(R.id.lunar)

searches all view for the one identified by R.id.lunar ( == "@+id/
lunar").  This returns a reference to
the LunaView instance created above which we store in a member
variable.

The constructor for LunarView created a private LunarThread object so
doing:

mLunarThread = mLunarView.getThread()

enables us to retrieve a reference to the thread object.


Hope that's correct and helpful!

richard.






--~--~---------~--~----~------------~-------~--~----~


Sponsored Links


Assistance with understanding LunarLander code

by steve_macleod » Sat, 13 Dec 2008 22:04:59 GMT


 i Richard,
OK, so it looks like the line:

mLunarView = (LunarView) findViewById(R.id.lunar)

creates an instance of the LunarView object, running the constructor.
I didnt think that running the findViewById and casting to a LunarView
would have this effect.

Thanks for that

On Dec 12, 5:19pm, RichardS <richardswingw...@googlemail.com> wrote:
--~--~---------~--~----~------------~-------~--~----~



Assistance with understanding LunarLander code

by steve_macleod » Sat, 13 Dec 2008 22:57:42 GMT


 i,
At what point does the surfaceCreated() method fire? I presume its
after we add the callback interface with addCallback?



On Dec 12, 5:19pm, RichardS <richardswingw...@googlemail.com> wrote:
--~--~---------~--~----~------------~-------~--~----~



Assistance with understanding LunarLander code

by RichardS » Sat, 13 Dec 2008 23:51:56 GMT


 teve, I'm sure the instance is created by

setContentView(R.layout.lunar_layout)

so the constructor will have been run by the time we do

mLunarView = (LunarView) findViewById(R.id.lunar)


richard.

On 13 Dec, 14:04, steve_macleod <steven_macl...@hotmail.com> wrote:
--~--~---------~--~----~------------~-------~--~----~



Assistance with understanding LunarLander code

by Mark Murphy » Sat, 13 Dec 2008 23:58:06 GMT


 


Correct.

If you look in res/layout/lunar_layout.xml, you will see the declaration:

<com.example.android.lunarlander.LunarView
       android:id="@+id/lunar"
       android:layout_width="fill_parent"
       android:layout_height="fill_parent"/>

When the layout is inflated via setContentView(), the constructors for 
each widget and container are called. For built-in widgets like 
FrameLayout, Android knows they are in the package android.widget; for 
custom ones, the full namespace is spelled out in the layout 
(com.example.android.lunarlander.LunarView).

-- 
Mark Murphy (a Commons Guy)
 http://commonsware.com 
_The Busy Coder's Guide to Android Development_ Version 1.9 Available!

--~--~---------~--~----~------------~-------~--~----~



Assistance with understanding LunarLander code

by steve_macleod » Sun, 14 Dec 2008 01:06:30 GMT


 Cheers, that makes sense. I didnt notice the reference to the class in
the lunar_layour xml.





--~--~---------~--~----~------------~-------~--~----~



Assistance with understanding LunarLander code

by Ralf » Mon, 15 Dec 2008 00:59:41 GMT


 o actually what you describe is done by the setContentView() call. It
takes the XML layout and inflates (==creates) the associated view
instances in memory.

Then findViewById() is able to give you the view instance given its XML id.

HTH
R/

On Sat, Dec 13, 2008 at 6:04 AM, steve_macleod
<steven_macl...@hotmail.com> wrote:

--~--~---------~--~----~------------~-------~--~----~



Other Threads

1. Best approach for making one variable available in another activity / class

'm not sure if this's the best approach...
if u r using intents to transfer control between the activities then i'd
advice u to try using the putExtra method of the Intent class...
u can read the extras in ur target activity by using getIntent.getExtras()
method... jus give it a try...
i hope this helps u...






-- 
Regards,
Sujay
P. J. O'Rourke<http://www.brainyquote.com/quotes/authors/p/p_j_orourke.html>
- "If government were a product, selling it would be illegal."

--~--~---------~--~----~------------~-------~--~----~

2. BroadcastReceiver and Google Services Inquiry

Hi All,

I have been working on BroadcastReceiver code to capture GMAIL events
and try to extract data to create an email event log.

Although the registration of the BroadcastReceiver using
Intent.PROVIDER_CHANGED for gmail appears to be correctly formed and
no exceptions are generated.  I have my doubts whether the receiver is
able to capture gmail event for Intent.PROVIDER_CHANGED, generated
when unread email is available, since code in my BroadcastReceiver is
never activated.   Upon reading the BroadcastReciever API docs (see
excerpt below) I see that BroadcastReceivers are not designed to
capture intents used with startActivity() and that BroadcastReceivers
only receive intents generated from a sendBroadcast() method call.
For your reference see my code snippets below.

Is the BroadcastReceiver mechanism not the appropriate method to
capture email/gmail/sms events?



BroadcastReceiver code is the following:

public class EmailReceiver extends BroadcastReceiver {
       /**
        *
        */

       String TAG= "MY_TAG:RECEIVER";

       @Override
       public void onReceive(Context context, Intent intent) {
               //
               Log.i(TAG, "Action=" + intent.getAction());
             if (Intent.ACTION_PROVIDER_CHANGED.equals
(intent.getAction
())) {
                 Uri dataURI= intent.getData();
                 String dataURIPath=dataURI.getPath();
                 Log.i(TAG, dataURIPath);
              }
       }
}


BroadcastReceiver code registration for gmail is the following:
//

@override
public void onCreate(Bundle b){
....

EmailReceiver myMailReceiver = new EmailReceiver();

......

}


@Override
public void onResume(){
       super.onResume();

        try{
              IntentFilter mailFilter = new IntentFilter
( Intent.ACTION_PROVIDER_CHANGED, "content://gmail-ls/unread/^i");
          //     IntentFilter mailFilter = new IntentFilter
( Intent.ACTION_PROVIDER_CHANGED, "gmail-ls");
           registerReceiver( myMailReceiver, mailFilter);
           Log.i(myMailReceiver.TAG, "MailReceiver registered");
     }catch(Exception e){
       Log.e(myMailReceiver.TAG, "Intent Filter malformed");
     }

}

Excerpt from BroadcastReceiver Doc:

Base class for code that will receive intents sent by sendBroadcast
().
........
........

Note that, although the Intent class is used for sending and receiving
these broadcasts, the Intent broadcast mechanism here is completely
separate from Intents that are used to start Activities with
Context.startActivity(). There is no way for a BroadcastReceiver to
see or capture Intents used with startActivity(); likewise, when you
broadcast an Intent, you will never find or start an Activity. These
two operations are semantically very different: starting an Activity
with an Intent is a foreground operation that modifies what the user
is currently interacting with; broadcasting an Intent is a background
operation that the user is not normally aware of.

.........

Regards,

KK
--~--~---------~--~----~------------~-------~--~----~

3. Local Service won't stop working although stopService

4. permissions problem on x86 porting

5. Where is tools.jar file

6. Loading app on emulator

7. Enable/Disable LocationManager, Help?