getApplicationContext returning null in TabActivity - android

public class MyTabActivity extends TabActivity {
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
dataManager = DataManager.getInstance(getApplicationContext());
}
}
I have a tab activity as shown above. It works fine on the initial run. After some time in the background where it's activity is deleted from memory, when you reopen the app, it will crash. The reason is because getApplicationContext() returns null.
I use this same setup in other Activities with no problems. I can't find anywhere in the documentation that says when/why/if it would return null.

You can simply pass "this" to your DataManager.getInstance because your MyTabActivity inherits from Context (three levels up)

Related

Cannot get a switch listener working with setOnCheckedChange(onCheckedChange)

I'm having a problem having a switch button listener to work correctly. It is in my main activity as is:
public class MainActivity extends WearableActivity {
private Switch mySwitch;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
// Enables Always-on
setAmbientEnabled();
// React to settings change
mySwitch= findViewById(R.id.mySwitch);
mySwitch.setOnCheckedChangeListener(this); // <--------
}
public void onCheckedChange(CompoundButton s, boolean isChecked) {
...
}
}
At the arrow line, I tried with this (like a sample on the internet). It is marked in Android Studio as an error. AS suggests me to cast to a CommpoundButton.onCheckedChange (or similar). When I start the app, it crashs saying MainActivity cannot be casted to that. I cannot do also setOnCheckedChangeListener(onCheckedChange);
What I am doing wrong?
You need to implement OnCheckedChangeListener if you want to use this as a option for setOnCheckedChangeListener().
public class MainActivity extends WearableActivity implements CompoundButton.OnCheckedChangeListener{
This is because setOnCheckedChangeListener() will only accept an instance of OnCheckedChangeListener, so you can't simply use this since the Activity that this is pointing to isn't an instance of OnCheckedChangeListener.
However, since OnCheckedChangeListener is an interface, this is easily rectified by implementing OnCheckedChangeListener. Thanks to how inheritance works in Java, Activity automatically becomes an instance of OnCheckedChangeListener once the interface has been implemented.
It looks like this was the option you were going for since you're also overriding the onCheckedChange() method inside your Activity.

Access button reference from other class

I have somthing like that:
public class A()
{
private Button myButton;
public void setMyButton(Button myButton)
{
this.myButton = myButton;
}
public Button getMyButton()
{
return myButton;
}
protected void onCreate(Bundle savedInstanceState)
{
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
Button button = (Button) findViewById(R.id.button);
setMyButton(button);
}
public class B()
{
public void exampleMethod()
{
A aObject = new A();
Button button = aObject.getMyButton();
}
}
Button in class B is always null. I tryied that:
In class A
public Button myButton;
In class B
A aObject = new A();
Button button = aObject.myButton;
but it is not working too.
I am sure that first is button initiate, then geting button reference. I checked that using logs.
I don't know what is going on, becasue "getMyButton" in class A work fine. How can I get reference of button from class A in class B?
What is the best way to do that?
Looks like you are creating new java object of your activity on your own. Please try to get existing object of an created activity; so that you will get activity context and activity properties as well.
The button is always null because its set in onCreate, which is not automatically called. Let me guess, A is supposed to be an Activity subclass, based on the signature, right? In that case you're doing things really wrong.
*You cannot create an Activity via new, it won't be properly initialized.
*Accessing views of an Activity like that is really weird. It makes more sense to pass the view into B, as B is likely owned by A. If the Activity A doesn't own B, you're probably doing something really wrong. Pretty much if you need to do this you've probably misarchitected something.
The way that you are doing will only works in JAVA and not in ANDROID.
Its because of ACTIVITY class. Activity class reference will be created only when its life cycle is created and you cannot pass the UI elements of one activity to Another.
As the button is associated with the 'A' Class in your case and if you want this in Class 'B' you have to start Activity and its life cycle and also setContentView(R.layout.activity_main);
Other way is get the data that you want from any class and publish the value in 'A' class itself. this can be done by startActivityForResult();

Android app: How to remove a view displayed by an Activity?

First I have a method which creates an activity which displays a view:
Intent intent = new Intent(viewController, TestActivity.class);
viewController.TestActivity(intent);
My TestActivity class calls setContentView(layout) and displays a view.
This works fine, my problem is trying to remove the Intent/View from the screen. I know I can call destroy() from within my Activity, however I am trying to remove the view from outside of the Activity class. I want the class which created the Activity/Intent to be able to remove it as well.
Any ideas? It seems like this should be a trivial fix however I'm unable to find the solution online for some reason. Thanks!
Make a sigleton-like pattern to get the instance of the started activity.
public class TestActivity extends Activity{
private static TestActivity instance;
public static TestActivity getInstance(){
return instance;
}
public void onCreate(Bundle savedInstanceState){
//xxxxx
super.onCreate(savedInstanceState);
instance = this;
}
protected void onDestroy(){
//xxxxx
super.onDestroy();
instance = null;
}
public void finishOutSide(){
this.finish();
}
}
Then in the outside,use these code to finish the activity:
if(TestActivity.getInstance() != null){
TestActivity.getInstance().finishOutSide();
}
All of your Activity instances can be destroyed when the sustem is running low on memory. Hence, it causes unexpected behaviour. Finish an Activity itside itself only or in a foreground Service that cannote be unexpectedly killed.
You have to rethink your application architecture to avoid such issue.

Android: NullPointerException in dialog when rotating

Sorry if this been asked before but I couldn't find an answer to my specific case. Also sorry that I'm new and a little stupid.
Problem:
I'm showing a dialog from a fragment and passing along a context in my constructor method because I need a context in my dialog to register for broadcastrecievers etc.
DialogFragment fragmentDialog = MyDialog.myConstructor(getActivity());
fragmentDialog.show(getFragmentManager(), "dialog");
Then in MyDialog class I store the context in a instance variable.
The problem arises when rotating the device and I get a nullPointerException when I try to use the context again in the dialog.
Can this be solved in some easy way?
If the device is rotated the Activity will be destroyed and recreated. So the Context you passed to your Fragment points on the Activity which was destroyed.
You could use setRetainInstance(true) in your Fragment. This way your Fragment will survive the recreation of the Activity.
To solve the NPE you have to pass the Context to the Fragment, if the Activity is recreated. Then the Context belongs to the new Activity.
In fact, without this update every line of code which points on the Activity like getActivity() or getFragmentManager() will lead in a NPE.
You get the NullPointerException because activites are destroyed and recreated when rotating the screen.
The SO post below gives more info...
https://stackoverflow.com/a/1673374/
Please be careful with the order of events if you rotate a FragmentActivity, because this can also be a source of NullPointerExceptions.
This is not documentated:
When the FragmentActivity is created the first time,
public class MyActivity extends FragmentActivity implements
MyFragment.OnFragmentInteractionListener {
private int var1;
private int var2;
#Override
protected void onCreate(Bundle savedInstanceState) {
//before
var1 = 3;
super.onCreate(Bundle savedInstanceState)
//after
var2 = 5;
}
//Interface Methods
public int getVar1() { return var1; }
public int getVar2() { return var2; }
}
both of the [before] and [after] code will be run before the fragments are attached and created. So, if you get the vars in the onCreate() call of the Fragment you get both vars. But when you rotate your device, the Activity is recreated from the savedInstanceState in the super call. Now, the fragments are reattached and created anew in this call! That means, this time the Methods of the Listener Interface are called before your [after] code. So, if you pass the Context of the activity to the fragment and get Information through the Interface like it is shown in: https://developer.android.com/training/basics/fragments/communicating.html
you get a NullPointerException for var2 because the interface methods are called from the fragments onCreate() onAttach() ... functions before the [after] code in the Activity's onCreate() is executed! So, take care that you set your Information the InterfaceFunctions are accessing before the super call.
Depending on what you're doing in your initialization you could consider creating a new class that extends Application and moving your initialization code into an overwridden onCreate method within that class.
public class MyApplicationClass extends Application {
#Override
public void onCreate() {
super.onCreate();
// TODO Put your application initialization code here.
}
}
And you are not stupid, even experts need help from time to time.

Starting an Android Activity from an Listener defined in an external class file

I have an Activity MyActivity with a Button MyButton.
I want to attach a MySpecialOnClickListener to MyButton.
I write MySpecialOnClickListener in an external class file.
public class MySpecialOnClickListener extends ButtonHandler implements OnClickListener {
public OnClickListenerWithSpeech (Context context)
{ super.context = context; }
#Override
public void onClick(View view) { handleClick(view); }
}
and ButtonHandler looks like this
public abstract class ButtonHandler {
protected Context context;
protected void handleClick (View view){
if (view.getid()==R.id.button_B) {
context.startActivity (new Intent(context, ActivityC.class));
}
}
}
I basically want to store all logic for Buttons in the ButtonHandler.
SO...as I said, I have the MySpecialOnClickListener defined in an external class file.
When I click MyButton I get the following fatal error.
Calling startActivity() from outside of an Activity context requires the FLAG_ACTIVITY_NEW_TASK flag. Is this really what you want?
So I can't start an activity normally from within a non-Activity. Fair enough.
However, if I change MySpecialOnClickListener to be an inner class in 'MyActivity' it works fine. Remember 'ButtonHandler' is still an external class file. So it (where ActivityC is ultimately started from) doesn't change.
My question (finally) is: can someone explain the logic of why one is allowed and the other isn't. I presume its a scoping thing or something but I'm a bit confused. It seems the code to start the process of starting an activity has to literally be inside another Activity.
EDIT - PROBLEM SOLVED
See below. The location of the class is irrelevant. I just didn't pass in the context properly.
because the ButtonHandler 'context' field isn´t associated with any activity context. So, when you attach the MySpecialOnClickListener instance to a button you create it passing the context parametener, isn´t???
something like this:
MySpecialOnClickListener listener = new MySpecialOnClickListener(MyActivity.this);
aButton.setOnClickListener( listener );
in this way you´re constructing the Button with the correct context...
It's likely that you are not passing the Activity context to MySpecialOnClickListener. Could you show me the difference in the way you invoke the inner-class approach?
Apologies to those of you who tried to answer. It was my fault (and I didn't include the following info initially for people)
When I was passing in the context to the 'MySpecialOnClickListener' I would do:
view.setOnClickListener(new MySpecialOnClickListener(getApplicationContext()));
when I should have done:
view.setOnClickListener(new MySpecialOnClickListener(this));
So getApplicationContext() doesn't seem to get the "correct" context for the app.
Which leads me to my next question as to what getApplicationContext() actually returns :)

Categories

Resources