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
That answer is quite simple. Unfortunately,
getContentResolver()and any other methods that require aContextcannot 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 insideonCreate()(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:
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 ofContextand yourActivitycounts as one.getBaseContext()is best used for when you need to pass or hold aContextoutside of the Android Lifecycle.Hope this helps,
FuzzicalLogic