android-context

Application context returning null when using getFilesDir()

女生的网名这么多〃 提交于 2019-12-23 07:55:59
问题 I don't know why this is happening. When i check the DDMS there is no files dir too. I'm trying to access this folder at my Application subclass. Any idea why this is happening? I need the application context to be global so I can use on classes that doesn't extends Activity. package mci.multipratic; import java.io.File; import java.io.FileInputStream; import java.util.Properties; import android.app.Application; import android.content.Context; public class MultiPraticApp extends Application {

Unable to start a WebView from an AsyncTask

烂漫一生 提交于 2019-12-23 02:35:00
问题 I'd like to start a WebView from my AsyncTask but it doesn't seem to run. This is what my onPostExecute method looks like: public class Viewer extends AsyncTask<URI, Integer, URI> { private Activity objContext = null; public Viewer(Activity objContext) { this.objContext = objContext; } protected void onPostExecute(URI uriWebpage) { WebView wbvBrowser = new WebView(this.objContext); wbvBrowser.getSettings().setBuiltInZoomControls(true); wbvBrowser.getSettings().setJavaScriptEnabled(true);

context parameter for custom adapter - getApplicationContext() fails but 'this' works

天涯浪子 提交于 2019-12-23 02:04:26
问题 I have simple code snippet to implement custom list view. In this code, I have CustomAdapter class which extends ArrayAdapter : CustomAdapter adapter = new CustomerAdapter(getApplicationContext(),R.layout.listview_item_row, weather_data); The constructor of CustomAdapter is as below: public CustomerAdapter(Context context, int layoutResourceId, weather[] data) { super(context, layoutResourceId, data); mlayoutResourceId = layoutResourceId; mcontext = context; mdata = data; } If I write this,

context parameter for custom adapter - getApplicationContext() fails but 'this' works

亡梦爱人 提交于 2019-12-23 02:04:15
问题 I have simple code snippet to implement custom list view. In this code, I have CustomAdapter class which extends ArrayAdapter : CustomAdapter adapter = new CustomerAdapter(getApplicationContext(),R.layout.listview_item_row, weather_data); The constructor of CustomAdapter is as below: public CustomerAdapter(Context context, int layoutResourceId, weather[] data) { super(context, layoutResourceId, data); mlayoutResourceId = layoutResourceId; mcontext = context; mdata = data; } If I write this,

Getting the context in a Thread called by a Service

本秂侑毒 提交于 2019-12-22 16:38:07
问题 I have the following piece of code: public class DumpLocationLog extends Thread { LocationManager lm; LocationHelper loc; public void onCreate() { loc = new LocationHelper(); lm = (LocationManager) Context.getSystemService(Context.LOCATION_SERVICE); } public void run() { lm.requestLocationUpdates(LocationManager.GPS_PROVIDER, 1000L, 500.0f, loc); } } I want it to be run from a remote service but in the line lm = (LocationManager) Context.getSystemService(Context.LOCATION_SERVICE); I get a

How to get rid of java.lang.IllegalStateException while trying to getAuthToken from Account

北慕城南 提交于 2019-12-22 11:09:26
问题 I am trying to get the authToken for an account but getting this error: java.lang.IllegalStateException: calling this from your main thread can lead to deadlock This is how I'm getting the AuthToken: public class MyAccount { private final Account account; private final AccountManager manager; private final Context context; public MyAccount (Context context) { this.context = context; final Account[] accounts = AccountManager.get(context).getAccountsByType("com.myapp"); account = accounts

How to get rid of java.lang.IllegalStateException while trying to getAuthToken from Account

做~自己de王妃 提交于 2019-12-22 11:07:00
问题 I am trying to get the authToken for an account but getting this error: java.lang.IllegalStateException: calling this from your main thread can lead to deadlock This is how I'm getting the AuthToken: public class MyAccount { private final Account account; private final AccountManager manager; private final Context context; public MyAccount (Context context) { this.context = context; final Account[] accounts = AccountManager.get(context).getAccountsByType("com.myapp"); account = accounts

Error - No instrumentation registered! Must run under a registering instrumentation

孤人 提交于 2019-12-22 11:06:37
问题 A) Why when i try to run the app i get this error? My Crash Log Error java.lang.RuntimeException: Unable to start activity java.lang.IllegalStateException: No instrumentation registered! Must run under a registering instrumentation. at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2947) at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:3008) at android.app.ActivityThread.-wrap14(ActivityThread.java) at android.app.ActivityThread$H.handleMessage

Accessing strings.xml from ViewModel

心已入冬 提交于 2019-12-22 10:41:32
问题 I am using Dagger 2 DataBindng and the new Android Lifecycle components, which have ViewModels . Inside my ViewModel how could I get access to my strings.xml? I was thinking at first, to inject a Context into the viewModel , however, this will just leak memory. Are there any other ways? 回答1: There is an AndroidViewModel, which receives Application instance as parameter. From docs: Application context aware ViewModel . Subclasses must have a constructor which accepts Application as the only

How to pass (Android) application context to a Java class?

夙愿已清 提交于 2019-12-22 05:14:45
问题 I have the following code in which I am using the application context to retrieve needed information: public class Data{ private boolean VarA; public void setVarA(boolean B,Context ctx) { SharedPreferences CoreDataStorage = ctx.getSharedPreferences(PREFS_NAME, 0); SharedPreferences.Editor editor = CoreDataStorage.edit(); editor.putBoolean("PrefVarA", VarA); edit.commit(); } } Now I am calling the public method setVarA() from the below class public class MyActivity extends Activity{ Data cd =