android-context

Android Context from another class

天大地大妈咪最大 提交于 2019-12-25 02:23:55
问题 What I'm trying to do is the following... FileInputStream fIn; try { fIn = openFileInput("samplefile.txt"); InputStreamReader isr = new InputStreamReader(fIn); BufferedReader br = new BufferedReader(isr); isTrue = Boolean.parseBoolean(br.readLine()); Log.i("IN", "isTrue = " + isTrue); } But this is only going to work in the class that extends the Activity class within Android. I have a "Settings" class which writes and reads the current games settings from a file, but this file has a lot of

NullPointer on LayoutInflater - Android

淺唱寂寞╮ 提交于 2019-12-25 02:18:21
问题 I have a class in another java file that I'm calling in my MainActivity. I need to inflate some layouts in the external class. The problem I'm having is specifying the context, because when I try to inflate the layouts I get a Null Pointer Exception. The class doesn't have it's own onCreate() method so I need to pass the context from my MainActivity? Not sure how to go about it. This is causing me the NullPointerException : Context context = getApplicationContext(); LayoutInflater inflater =

How to avoid static context reference when I need to use a activity context?

青春壹個敷衍的年華 提交于 2019-12-24 15:06:20
问题 After read this topic avoiding memory leaks some doubts arouse. If I need to use an activity context (example: inflate a view in a PopupWindow class to show a popup) how can I hold the context of actual activity to do it? If I need to avoid a static context reference the only way to do it is creating an attribute in my class? And all the other classes I'll need the actual activity context I need to do it? update- I want to use this actual activity context in many classes that don't inherited

Android - how to pass context parameter to a method?

微笑、不失礼 提交于 2019-12-24 12:48:19
问题 I'm trying to develop a simple app for my daughter but I'm not a professional :) I was wondering how you can pass a context to a Boolean method? My issue is, when trying to merge both codes below private boolean isNetworkAvailable() { ConnectivityManager connectivityManager = (ConnectivityManager) getSystemService(Context.CONNECTIVITY_SERVICE); NetworkInfo activeNetworkInfo = connectivityManager.getActiveNetworkInfo(); return activeNetworkInfo != null;} with public static boolean

Argument this doesn´t work. [required: 'android.content.context'

泪湿孤枕 提交于 2019-12-24 12:06:03
问题 I use this: actionBar.setBackgroundColor(ContextCompat.getColor(this, R.color.action_bar)); To use colors from the color.xml . It works great, but in some Codes it says: Wrong 1st argument type. Found: 'org.telegram.ui.ActionBar.BaseFragment', required: 'android.content.Context' But I import android.content.Context: import android.content.Context; I tried to use instead of 'this': actionBar.setBackgroundColor(ContextCompat.getColor(context, R.color.action_bar)); But than Android Studio say :

null pointer in openFileOutput

柔情痞子 提交于 2019-12-24 09:50:09
问题 I'm trying to use openFileOutput from a class which is not Activity class. When I'm writing something following, it gives me null pointer exception- try { Context con = null; fosCAM = con.openFileOutput(camFile, Context.MODE_PRIVATE); } catch (FileNotFoundException e1) { // TODO Auto-generated catch block e1.printStackTrace(); } Can anyone help me please? 回答1: You're receiving a null pointer exception because you're setting the Context variable con to null and then referencing it with con

Unable to set proper Context to cancel alarmmanager

怎甘沉沦 提交于 2019-12-24 07:31:21
问题 I have created an alarm with a notification and a button to stop the alarm but it seems that there is some issue with the Context that I have to get in order to cancel the AlarmManager . I have researched a lot and I think I have applied most of the things I found but somehow can't get this to work. I am aware the PendingIntent has to be the same and it appears to me they are with the same request codes too. AlarmSlave.class - Sets Alarm public class AlarmSlave /*extends Service*/ { private

Android Contextual Action Bar not appearing after item selection

末鹿安然 提交于 2019-12-24 07:19:41
问题 In my android application, I am trying to show contextual action bar (CAB) in listview for item delete. The problem is that whenever an item is pressed for long, the item gets selected, but CAB doesn't appear. I have verified with so many tutorials, can't figure out what am I missing. Any help would be appreciated. Adapter Class public class DuasListAdapter extends ArrayAdapter<String>{ // class variables private final Context context;// to save context private final List<String> duas;// to

Toast.makeText(getApplicationContext(), “String”, Toast.LENGTH_LONG); ==>Here getApplicationContext() cannot change to “this”?

孤街浪徒 提交于 2019-12-24 02:56:14
问题 First the format of Toast.makeText(): public static Toast makeText (Context context, CharSequence text, int duration) the first argument is Context , the function getApplicationContext() also return the current context, everything is ok, but IMO, the getApplicationContext() can also be replaced with this, just as follows: public class ContextMenuResourcesActivity extends Activity { /** Called when the activity is first created. */ private Button b1; @Override public void onCreate(Bundle

Null Context in setUserVisibleHint

别说谁变了你拦得住时间么 提交于 2019-12-23 12:04:19
问题 A message needs to be displayed to the user when a fragment in a ViewPager becomes visible. Currently the call is: // Within a class that extends Fragment @Override public void setUserVisibleHint(boolean isVisibleToUser) { super.setUserVisibleHint(isVisibleToUser); if (isVisibleToUser) { MessageUtility.displayMessage(getContext()); } } MessageUtility.displayMessage() opens an alert dialog with a message. Context is required to display the message. However, the fragment is not guaranteed to be