getApplicationContext returns null in test case (unless you sleep first)

by Dianne Hackborn » Fri, 06 Mar 2009 04:12:05 GMT


Sponsored Links
 The framework runs the instrumentation independently of the app, so it can
do what it wants when it wants.  And an activity is not "the app".
Activities are created all the time, you can't wait for whatever one to be
created before running the instrumentation.

 http://developer.android.com/reference/android/app/Instrumentation.html 

 http://developer.android.com/reference/android/app/Instrumentation.html #onStart()

 http://developer.android.com/reference/android/app/Instrumentation.html #runOnMainSync(java.lang.Runnable)








-- 
Dianne Hackborn
Android framework engineer
hack...@android.com

Note: please don't send private questions to me, as I don't have time to
provide private support.  All such questions should be posted on public
forums, where I and others can see and answer them.

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



Other Threads

1. Routing and the Maps TOS

In the Google Maps TOS it says that you can't use it for route
guidance.  Does anyone know if displaying a line on a
MyLocationOverlay indicating a route would count as route guidance?
It's not real time guidance, but the map is synchronized to the user's
position.  Has anyone done this?

2. TableLayout possibly flawed in API Level 5, AVD 2.0

Still could not get it working.

To simplify the debugging, I have written some test code (downloaded
it from a forum actually:)). I wonder why even this does not work.
public class TestTableLayout extends Activity {

            int PROVINCE_Alberta = 0;
            int PROVINCE_BC = 1;
            int PROVINCE_Manitoba = 2;
            int PROVINCE_NewBrunswick = 3;
            int PROVINCE_Newfoundland = 4;
            int PROVINCE_Northwest = 5;
            int PROVINCE_NovaScotia = 6;
            int PROVINCE_Nunavut = 7;
            int PROVINCE_Ontario = 8;
            int PROVINCE_PEI = 9;
            int PROVINCE_Quebec = 10;
            int PROVINCE_Saskatchewan = 11;
            int PROVINCE_Yukon = 12;
            int numProvinces = 13;

            String[] provinces = new String[numProvinces];


    /** Called when the activity is first created. */
    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.main);

        provinces[PROVINCE_Alberta] = "Alberta";
        provinces[PROVINCE_BC] = "British Columbia";
        provinces[PROVINCE_Manitoba] = "Manitoba";
        provinces[PROVINCE_NewBrunswick] = "New Brunswick";
        provinces[PROVINCE_Newfoundland] = "Newfoundland and
Labrador";
        provinces[PROVINCE_Northwest] = "Northwest Territories";
        provinces[PROVINCE_NovaScotia] = "Nova Scotia";
        provinces[PROVINCE_Nunavut] = "Nunavut";
        provinces[PROVINCE_Ontario] = "Ontario";
        provinces[PROVINCE_PEI] = "Prince Edward Island";
        provinces[PROVINCE_Quebec] = "Quebec";
        provinces[PROVINCE_Saskatchewan] = "Saskatchewan";
        provinces[PROVINCE_Yukon] =  "Yukon";

     // Get the TableLayout
        TableLayout tl = (TableLayout) findViewById(R.id.maintable);

        // Go through each item in the array
        for (int current = 0; current < numProvinces; current++)
        {
            // Create a TableRow and give it an ID
            TableRow tr = new TableRow(this);
            tr.setId(100+current);
            tr.setLayoutParams(new LayoutParams(
                    LayoutParams.FILL_PARENT,
                    LayoutParams.WRAP_CONTENT));

            // Create a TextView to house the name of the province
            TextView labelTV = new TextView(this);
            labelTV.setId(200+current);
            labelTV.setText(provinces[current]);
            labelTV.setTextColor(Color.BLACK);
            labelTV.setLayoutParams(new LayoutParams(
                    LayoutParams.FILL_PARENT,
                    LayoutParams.WRAP_CONTENT));
            tr.addView(labelTV);

            // Create a TextView to house the value of the after-tax
income
            TextView valueTV = new TextView(this);
            valueTV.setId(current);
            valueTV.setText("$0");
            valueTV.setTextColor(Color.BLACK);
            valueTV.setLayoutParams(new LayoutParams(
                    LayoutParams.FILL_PARENT,
                    LayoutParams.WRAP_CONTENT));
            tr.addView(valueTV);

            // Add the TableRow to the TableLayout
            tl.addView(tr, new TableLayout.LayoutParams(
                    LayoutParams.FILL_PARENT,
                    LayoutParams.WRAP_CONTENT));
        }
        tl.invalidate();

    }
}

<?xml version="1.0" encoding="utf-8"?>
        <TableLayout xmlns:android=" http://www.***.com/ 
android"
                android:layout_width="fill_parent"
                android:layout_height="fill_parent"
                android:stretchColumns="0,1"
                android:id="@+id/maintable" >
        </TableLayout>


Please help
Siddharth












> > > > >

3. Exiting an onDraw()

4. Paid apps not visible again after Eris upgrade Sunday

5. Breaking News: The Market Still Needs Fixing...

6. MMS receiving/sending

7. Join Thailand Forums Thailand Message Boards Living, Work and Play in Thailand ...X.