android-context

Passing Activity Context to constructors to use internally - is this bad

我的梦境 提交于 2019-12-02 22:18:38
Is it bad practice to pass the Context to a constructor and save it as a private variable for internal use? The other option is to pass the Context as a parameter to methods that need it. Which is a better option? I have a feeling that passing to the constructor might result in memory leaks accidentally. Often, all you need is the ApplicationContext , so what you can do is pass this.getApplicationContext() instead of just this . Your app context exists for the lifetime of the app anyway, so it's not a memory leak. It depends on the lifetime of your object. If you're sure the Object will only

Progress Dialog crashes in async task

谁都会走 提交于 2019-12-02 22:13:41
问题 As the title says the app crashes when encounters the progress dialog create instruction. The context is passed to async class through constructor. public class URLImageReader extends AsyncTask<URL, Void, Bitmap> { ImageView MyView = null; Activity context = null; private OnTaskComplete mlistener; public URLImageReader(Activity context,OnTaskComplete mlistener, ImageView view){ this.context = context; this.mlistener = mlistener; MyView = view; } ProgressDialog dialog = new ProgressDialog

passing context as argument of DialogFragment

无人久伴 提交于 2019-12-02 18:00:48
it's possible to pass a context variable to a DialogFragment? i've use this code inside dialog for passing a string: public static ConfirmDialog newInstance( String f) { ConfirmDialog d = new ConfirmDialog(); Bundle args = new Bundle(); args.putString("FILE_NAME", f); d.setArguments(args); return d; } but i don't find any function like putString for passing context. It's possible to do that? Your DialogFragment has a very handy method for getting a Context instance: getActivity() Fragment#getActivity() will return the instance of the Activity (which is a Context ) that the Fragment is attached

How to call Context and Intent objects inside onBindViewHolder

假装没事ソ 提交于 2019-12-02 13:27:18
I want to open a new Activity within RecyclerView , but I can't create an Intent object in there. val intent1 = Intent(this,Main2Activity::class.java) startActivity(intent1) Android Studio warns about Intent not being a Context . How can I still open a new Activity inside the RecyclerView I tried also the code below,it gives "startActivity(intent)" line gives error, "type mismatch, required Context, found Intent". Plus, also "this@MainActivity" gives "unresolved reference@MainActivity" error. override fun onBindViewHolder(holder: Main_Menu_Holder, position: Int) { var currentview = alldata.get

Android code starting from ContentProvider class rather than Main class

旧时模样 提交于 2019-12-02 11:43:17
Yesterday I posted a similar question on why my code started on my content provider class rather than the main class and I've gotten some feedback which I've updated but the problem still remains where the code starts with the ContentProvider class rather than the main class. I've run through the code with debugger and its puzzling why the code starts in the Content Provider and passes back to the main class at the Context stage. I hope to solicit some help here ! The main class is here: public class MedF1 extends Activity { /** Called when the activity is first created. */ @Override public

Progress Dialog crashes in async task

烂漫一生 提交于 2019-12-02 09:53:24
As the title says the app crashes when encounters the progress dialog create instruction. The context is passed to async class through constructor. public class URLImageReader extends AsyncTask<URL, Void, Bitmap> { ImageView MyView = null; Activity context = null; private OnTaskComplete mlistener; public URLImageReader(Activity context,OnTaskComplete mlistener, ImageView view){ this.context = context; this.mlistener = mlistener; MyView = view; } ProgressDialog dialog = new ProgressDialog(context); @Override protected void onPreExecute() { super.onPreExecute(); dialog.setMessage("Loading...");

Android calling IntentService class from a Service Class

人走茶凉 提交于 2019-12-02 09:32:19
Can we call IntentService class from a running Service class . Intent myintentservice = new Intent(this, MyIntentService.class); startService(myintentservice); I using above lines of code in service class to start IntentService class. But then I get following error:- Process: objectdistance.ankeshkjaisansaria.ram.sita.cameratag, PID: 21823 java.lang.NullPointerException: Attempt to invoke virtual method 'java.lang.String android.content.Context.getPackageName()' on a null object reference at android.content.ComponentName.<init>(ComponentName.java:77) at android.content.Intent.<init>(Intent

Error: java.lang.NullPointerException: Attempt to invoke virtual method 'android.content.res.XmlResourceParser

烈酒焚心 提交于 2019-12-02 04:52:44
问题 I am getting this error.I am new to android studio and i need to create this plugin for unity to install an apk at runtime Error - Attempt to invoke virtual method 'android.content.Context Android.content.Context.getApplicationContext()' on a null object reference Plugin Class - package com.example.unitypluginappinstall; import android.app.Activity; import android.content.Context; import android.content.Intent; import android.net.Uri; import android.support.v4.content.FileProvider; import

getApplicationContext() throws an exception when used

南笙酒味 提交于 2019-12-02 04:28:22
I've specified a class, based on another one in an existing Android project. The addRow() method is supposed to dynamically add rows to a table. When creating a new TextView to add to my row and also when creating that row, I'm supposed to specify the "context". The current way, trying "getApplicationContext()" throws a NullPointerException. So where am I supposed to get that context from? public class DistanceTableView extends ContactListActivity { public void addRow(LocationMessage locationMsg){ View messageView = theInflater.inflate(R.layout.homepage, null); TableLayout table = (TableLayout

Error: java.lang.NullPointerException: Attempt to invoke virtual method 'android.content.res.XmlResourceParser

十年热恋 提交于 2019-12-02 02:29:35
I am getting this error.I am new to android studio and i need to create this plugin for unity to install an apk at runtime Error - Attempt to invoke virtual method 'android.content.Context Android.content.Context.getApplicationContext()' on a null object reference Plugin Class - package com.example.unitypluginappinstall; import android.app.Activity; import android.content.Context; import android.content.Intent; import android.net.Uri; import android.support.v4.content.FileProvider; import java.io.File; public class PluginClass extends Activity { static String errMessage; public static String