0

Literally all I have done is create a new project. import android.app.Activity; import android.content.ContentResolver; import android.os.Bundle; public class WebApp4Act extends Activity { /** Called when the activity is first created. */ public static final Uri BOOKMARKS_URI = Uri.parse("content://browser/bookmarks"); Context context = getBaseContext(); ContentResolver cr = getContentResolver();

@Override
public void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.main); 
}
}

when I run the project it says unfortunately webApp4c has stopped

if I comment out the line ContentResolver cr = getContentResolver(); then there is no problem I'm trying to run this on an AVD set to run on version 14

Martin
  • 133
  • 1
  • 11

2 Answers2

3
Context context = getBaseContext();
ContentResolver cr = getContentResolver();

need invoke after onCreate method or in onCreate method

idiottiger
  • 5,147
  • 2
  • 25
  • 21
1

That answer is quite simple. Unfortunately, getContentResolver() and any other methods that require a Context cannot do so during the Application Construction process or Initialization. On some versions of the AVD, this was not properly replicated, so that's why it works on some versions of the AVD. All you have to do is move your code to inside onCreate() (or any other function that runs AFTER construction) and you will be fine.

Note: There ARE ways to pass a custom View or Activity this information during construction, but there are two caveats:

  1. You cannot do so for the first Component launched in your Application without another Application or Application Component giving it to you...
  2. This CAN lead to unsafe code.

In most circumstances, it is just best to work within the Android Lifecycle. If you need some information on the Android Lifecycle, you may get it from the Android documentation or from the Javadoc.

Additional Note (Edit): You don't actually need the getBaseContext() up there. There are several kinds of Context and your Activity counts as one. getBaseContext() is best used for when you need to pass or hold a Context outside of the Android Lifecycle.

Hope this helps, FuzzicalLogic

Fuzzical Logic
  • 12,947
  • 2
  • 30
  • 58
  • @idiottiger and Fuzzical Logic thank you both very much this has fixed it – Martin Jan 05 '12 at 12:01
  • it's probably best to never use `getBaseContext()` anyway! – slinden77 Jun 08 '13 at 15:18
  • Not true, actually. In fact, google recommends using it whenever you need a context outside of the lifetime for an object that doesn't have getApplicationContext(). In many cases, getBaseContext() provides an ApplicationContext. However, it is agreed that one should do so sparingly and program accordingly. – Fuzzical Logic Jun 08 '13 at 22:41
  • For other uses and clarification: see http://stackoverflow.com/questions/5458156/diffinitive-rules-for-using-androids-getbasecontext-getapplicationcontext-or-u – Fuzzical Logic Jun 08 '13 at 22:44