1

This is my non-activity class:

import android.app.Activity;
import android.content.Context;
import android.os.Bundle;
import android.util.Log;
import android.widget.Toast;

public class PostManager {

    private Bundle bundle;
    private Context context;
    private Session session;
    private String victimId=null;

    public PostManager() {
        SavedFriend savedFriend = new SavedFriend();
        bundle = savedFriend.getBundle();
        context = savedFriend.getContext();
        session = savedFriend.getSession();
        victimId = savedFriend.getfriendsId();

        Log.e("postManager", victimId);
        Log.e("postManager", bundle.getString("message"));
    }
}

This is my application class:

import java.util.List;

import android.app.Application;
import android.content.Context;
import android.os.Bundle;

import com.facebook.Session;
import com.facebook.model.GraphUser;

public class SavedFriend extends Application {
    private List<GraphUser> selectedUsers;
    private String friendsId;
    private Session session;
    private Bundle bundle;
    private Context context;

    public List<GraphUser> getSelectedUsers() {
        return selectedUsers;
    }

    public void setSelectedUsers(List<GraphUser> selectedUsers) {
        this.selectedUsers = selectedUsers;
    }

    public String getfriendsId() {
        return friendsId;
    }

    public void setfriendsId(String id) {
        this.friendsId = id;
    }

    public Session getSession(){
        return session;
    }

    public void setSession(Session session){
        this.session = session;
    }

    public void setContext(Context context){
        this.context = context;
    }

    public Context getContext(){
        return context;
    }

    public void setBundle(Bundle bundle){
        this.bundle = bundle;
    }

    public Bundle getBundle(){
        return bundle;
    }
}

I have used the data of the application class in a fragment class (friendsId) which is not null.

When I call the application class's data from PostManager it returning the value null.

I have tried to see the value of friendsId and message by Log.e. but it gives me nullPointer exception.

Does that means all of the values I call in my PostManager constructor from application class are null? If yes, what should I do?

I need the session, applicationcontext, message, friendsId value in my PostManager class. I can pass these values to PostManager, but this class is called by onReceive() of alarm class which class extends BroadcastReceiver, and this alarm is set from another class which extend Fragment. All the values I need are created by this class except friendsId, and I don't know how to pass these value from first class->alarm class->postmanager class.

In manifest for alarm class:

<application android:name=".SavedFriend"
  .......>
  ....
  <receiver android:name="package name.Alarm"/>
  ....
</application>
Shoshi
  • 2,254
  • 1
  • 29
  • 43

1 Answers1

1

You're creating a new instance of your Application subclass, so none of those member variables will be set.

Instead, you can use the approach from this question and store an easy to access static variable of your Application. Use something like this instead of creating a new instance.

Community
  • 1
  • 1
wsanville
  • 37,158
  • 8
  • 76
  • 101
  • thank you. do you know how to get the context of an activity rather than getApplicationContext() method ? @wsanville – Shoshi Dec 11 '12 at 13:02
  • If you need an Activity, pass one as an argument. It's generally not a good idea to keep references to Activities lying around, their lifecycle makes it easy to mistakenly have a reference to a destroyed Activity. – wsanville Dec 11 '12 at 14:37
  • one more question. if user hit the home button or back button what will happen to ApplicationContext? will it automatically get destroyed by the os or will it be alive ? @wsanville – Shoshi Dec 11 '12 at 15:07
  • The application will not be immediately destroyed when the user leaves the app (i.e. the Home button, or going to some other app). Instead, the OS will determine when the application should be destroyed. – wsanville Dec 11 '12 at 15:59