android-context

Null Pointer exception starting IntentService

三世轮回 提交于 2019-11-30 13:11:29
问题 I've got an IntentService that I'm trying to start. When I do, it spits out this: java.lang.RuntimeException: Unable to start service com.pec.testapp.service.NewsService@406bd940 with Intent { cmp=com.pec.testapp/.service.NewsService }: java.lang.NullPointerException at android.app.ActivityThread.handleServiceArgs(ActivityThread.java:2173) ... (omitted for brevity) Caused by: java.lang.NullPointerException at android.app.IntentService.onStart(IntentService.java:110) at android.app

How to use getSystemService in a non-activity class?

若如初见. 提交于 2019-11-30 11:58:28
问题 I am building an application which triggers an alarm via AlarmManager. I would like to be able to call the Alarm via it's own non-activity class, but since I am not extending Activity, I don't appear to have any 'context'. This concept confuses me, and I've read the sdk docs. How would I go about using: alarmTest = (AlarmManager)getSystemService(Context.ALARM_SERVICE); in my non-activty class? Also, I'm assuming getting context will allow me to use SharedPrefs and Intents in my non-activity

How to get context for BaseAdapter in Android

末鹿安然 提交于 2019-11-30 08:06:00
问题 I have a class named BinderData which extends BaseAdapter . I want to get the base class context. I tried to use getContext() but that doesn't work in case of BaseAdapter . How can I get the Context of this class? 回答1: One more solution- If you have a parent, you can directly access the context like so- public class SampleAdapter extends BaseAdapter { LayoutInflater inflater; @Override public View getView(int position, View convertView, ViewGroup parent) { if(inflater == null){ Context

What is the difference between this, getContext() and getActivity()?

≯℡__Kan透↙ 提交于 2019-11-30 06:20:44
问题 I am very confused with the usage of all these that where should we use them. 回答1: this - return self reference getContext() - return Context getActivity() - return Activity Context. Quote from original answer : As the name suggests, its the context of current state of the application/object. It lets newly created objects understand what has been going on. Typically you call it to get information regarding another part of your program (activity, package/application) Activity Activity is a

Null Pointer exception starting IntentService

有些话、适合烂在心里 提交于 2019-11-30 06:06:19
I've got an IntentService that I'm trying to start. When I do, it spits out this: java.lang.RuntimeException: Unable to start service com.pec.testapp.service.NewsService@406bd940 with Intent { cmp=com.pec.testapp/.service.NewsService }: java.lang.NullPointerException at android.app.ActivityThread.handleServiceArgs(ActivityThread.java:2173) ... (omitted for brevity) Caused by: java.lang.NullPointerException at android.app.IntentService.onStart(IntentService.java:110) at android.app.IntentService.onStartCommand(IntentService.java:118) at android.app.ActivityThread.handleServiceArgs

Android JUnit4 Testing - Where to get Context from?

非 Y 不嫁゛ 提交于 2019-11-30 06:01:32
I have to build an app with sqlite usage. Now I want to write my unit tests. These unit tests should test my class SQLiteBridge . SQLiteBridge provides DAOs for every child class of Model. Now I got the problem that I need a context to create my SQLiteBridge . SQLiteBridge creates and handles a SQLite database on the system.. Where to get the Context-Object from? My setup is like here (so I'm using Junit4 [thanks god]): http://tools.android.com/tech-docs/unit-testing-support EDIT: I hope there is a way like the old AndroidTestCase to extend without losing Junit4. :) Dustin Thomson As described

AsyncTask and Contexts

社会主义新天地 提交于 2019-11-30 03:15:56
So I'm working out my first multi-threaded application using Android with the AsyncTask class. I'm trying to use it to fire off a Geocoder in a second thread, then update the UI with onPostExecute, but I keep running into an issue with the proper Context. I kind of hobbled my way through using Contexts on the main thread, but I'm not exactly sure what the Context is or how to use it on background threads, and I haven't found any good examples on it. Any help? Here is an excerpt of what I'm trying to do: public class GeoCode extends AsyncTask<GeoThread, Void, GeoThread> { @Override protected

Is it a bad practice to hold application Context instance?

时光总嘲笑我的痴心妄想 提交于 2019-11-30 02:54:34
问题 From my understanding, Application in Android is a singleton (correct me if I'm wrong) and we always have just one application Context instance. So, from this perspective, is it a bad practice to save the application Context in my Application class? Can it lead to a massive memory leak? Here is an example: public class MyApp extends Application { private static Context appContext = null; // <-- here is the thing! @Override public void onCreate() { appContext = this; } public static Context

How to use getSystemService in a non-activity class?

偶尔善良 提交于 2019-11-30 01:11:04
I am building an application which triggers an alarm via AlarmManager. I would like to be able to call the Alarm via it's own non-activity class, but since I am not extending Activity, I don't appear to have any 'context'. This concept confuses me, and I've read the sdk docs. How would I go about using: alarmTest = (AlarmManager)getSystemService(Context.ALARM_SERVICE); in my non-activty class? Also, I'm assuming getting context will allow me to use SharedPrefs and Intents in my non-activity class as well? You can pass the context to the non-activity class which is the preferred way or you

Bluetooth device discovery in Android — startDiscovery()

泄露秘密 提交于 2019-11-29 23:57:39
Goal: Build an Android app that discovers the names and addresses of BT devices within range and submits their values to a webservice. BT devices have not been previously bonded to the host device, I just want to poll everything as I walk about. What I've done: Pored over documentation. Implemented a local instance of the host device's BT adapter. Implemented a notification to enable BT if it isn't enabled. Registered Broadcast Receivers and Intents to parse the ACTION_FOUNDs coming off of startDiscovery() . Registered BLUETOOTH and BLUETOOTH_ADMIN permissions in the manifest. Things work (as