android-context

NullPointer exception in ContextWrapper

╄→尐↘猪︶ㄣ 提交于 2019-12-12 15:07:33
问题 I have a class called FileGeneration that extends Activity In FileGeneration I have a method called protected OutputStream openAndWriteFile() { // Set the Context-mode int cxt = Context.MODE_PRIVATE; // Check if we are not going to clear the file and the file exists if (!clearFile && (new File(this.fileName)).exists()) { // Append to the file cxt = Context.MODE_APPEND; } // Try to open the file to write to try { // Open the File using the Context this.os = openFileOutput(this.fileName, cxt);

How to initialize context? [closed]

天大地大妈咪最大 提交于 2019-12-12 13:23:59
问题 It's difficult to tell what is being asked here. This question is ambiguous, vague, incomplete, overly broad, or rhetorical and cannot be reasonably answered in its current form. For help clarifying this question so that it can be reopened, visit the help center. Closed 6 years ago . This is a really stupid question but what do I set context equal to in Context context = ... Just declaring it sets it to null and I need to use context for my app. 回答1: You cannot instantiate a Context object.

Can't make a toast from thread in an AppWidgetProvider class

给你一囗甜甜゛ 提交于 2019-12-12 12:27:11
问题 I have an appwidget that can download some data from a web service (in a thread in onReceive() ) by clicking on it. After that is finished the GUI of the widget gets updated in updateWidget(...) (= re-drawn). I want to make a toast when this is done. I tried a lot like to make a toast at the end of updateWidget(...) by passing the context from the thread to the toast, but that didn't work. The problem seems to be the context. Because my class inherits from AppWidgetProvider and NOT from

How to get context of an activity extending ListFragment from its adapter class?

萝らか妹 提交于 2019-12-12 10:55:31
问题 This is my adapter class where I want to call startActionMode. I call it inside setActionMode method but got these errors: Cannot cast from Context to ActivityFragment. The method startActionMode(ActivityFragment.ActionModeCallback) is undefined for the type ActivityFragment. public class ListAdapter extends ArrayAdapter<ListGettersSetters> { ArrayList<ListGettersSetters> arrayListGettersSetters; LayoutInflater layoutInflater; Context context; int Resource, i = 0, j = 0, checkedItemsCount = 0

Toast in AsyncTask in non Activity class

微笑、不失礼 提交于 2019-12-12 10:49:49
问题 I have a non Activity class which contains an AsyncTask which calls an URL. If the Connection Timeouts for some reasons, I want to let the user know this by posting a Toast. But I just can't get any Context. How can achive this? RPIcall.class import android.os.AsyncTask; import android.util.Log; import org.apache.http.HttpResponse; import org.apache.http.client.ClientProtocolException; import org.apache.http.client.methods.HttpGet; import org.apache.http.conn.HttpHostConnectException; import

Can a home widget have a Context?

坚强是说给别人听的谎言 提交于 2019-12-12 10:37:39
问题 In an activity it's (usually) easy to get the Context. What if I am working with a home widget class? These are classes that extends AppWidgetProvider, which don't contain a Context! 回答1: An instance of Context is passed to all the methods in AppWidgetProvider for you to use. 回答2: For those who can't access developer.android like me, here's where you can get context in app widget: Android provides context in onReceive method in widget class. @Override public void onReceive(Context context,

Pass Application Context to the view instead of Activity Context

南笙酒味 提交于 2019-12-12 09:48:13
问题 Why use Activity Context while we can use Application Context to load and access resource? Means if I use Application Context instead of Activity Context there is no Exception occur so why use Activity Context ? Example: In below example if I use getApplicationContext() instead of " this " pointer inside the Activities onCreate() it works fine without any exception. Button button = new Button(getApplicationContext()); 回答1: getApplicationContext() should be used with the view , which will be

When to use and not to use the android Application class?

不打扰是莪最后的温柔 提交于 2019-12-12 08:50:35
问题 I am considering using the android Application class as a place to store temporary state and common code shared by other (fragment) activities in the app. I would like to get more feedback as to whether it is a good place for: Shared constants like ID's, pref key names, etc. Global variables (i.e. setters/getters) reflecting current UI state, navigation, selected fragment, and, in general, temporary data that does not need to be persisted. Hooks for persisting data when certain conditions are

getDefaultSharedPreferences(Context) - any context?

早过忘川 提交于 2019-12-12 05:36:14
问题 I have a helper class for dealing with the default shared preferences. I retrieve the preferences once and I wrap all the SP methods I need, providing the same cached instance. It goes like: public final class AccessPreferences { private static SharedPreferences prefs; // cache private static SharedPreferences getPrefs(Context ctx) { // synchronized is really needed or volatile is all I need (visibility) SharedPreferences result = prefs; if (result == null) synchronized (AccessPreferences

how to use the filedialog of afollestad/material-dialogs

旧时模样 提交于 2019-12-12 04:35:04
问题 I am trying to call this dialog in my fragment, I visited some thread regarding this and I came to know about the overriding the OnAtach method but this method is deprecated and I can no longer use that method @override public void onAttach(Context context) { super.onAttach(context); context1=this.getActivity(); } this is how I use to call the method but still, it is showing error red line in the builder method of the dialog this is thelibrary that I am using I asked them here but no answer