Amit Tiwari Amit Tiwari - 3 months ago 27
Android Question

NullPointerException while using context in Fragment and Adapter

I have a Navigation Drawer which contains a Fragment. This fragment has a ViewPager which is attached to 2 tabs. Both the tabs are also Fragments.

The problem is when I am switching between menu items in Navigation Drawer, 3-4 times everything works fine but then suddenly the app closes. There is no error that "Unfortunately your app has crashed". When I check all the open apps on the phone, the app is there. I don't know which state the app is but I am sure it is not minimized, although it looks like it has been minimized. As soon as this occurs, the outputs of the logcat disappear. So I piped the output of logcat to a file and this is the error that I get:

E/AndroidRuntime(16738): FATAL EXCEPTION: main
E/AndroidRuntime(16738): Process: com.galleri5.android, PID: 16738
E/AndroidRuntime(16738): java.lang.NullPointerException: Attempt to invoke virtual method 'android.app.Application android.support.v7.app.AppCompatActivity.getApplication()' on a null object reference
E/AndroidRuntime(16738): at com.galleri5.android.adapters.UserStudioHiFivesNavigationAdapter.<init>(UserStudioHiFivesNavigationAdapter.java:69)
E/AndroidRuntime(16738): at com.galleri5.android.fragments.ProfileHiFivesNavigationFragment$2.success(ProfileHiFivesNavigationFragment.java:81)
E/AndroidRuntime(16738): at com.galleri5.android.fragments.ProfileHiFivesNavigationFragment$2.success(ProfileHiFivesNavigationFragment.java:77)
E/AndroidRuntime(16738): at retrofit.CallbackRunnable$1.run(CallbackRunnable.java:45)
E/AndroidRuntime(16738): at android.os.Handler.handleCallback(Handler.java:739)
E/AndroidRuntime(16738): at android.os.Handler.dispatchMessage(Handler.java:95)
E/AndroidRuntime(16738): at android.os.Looper.loop(Looper.java:135)
E/AndroidRuntime(16738): at android.app.ActivityThread.main(ActivityThread.java:5268)
E/AndroidRuntime(16738): at java.lang.reflect.Method.invoke(Native Method)
E/AndroidRuntime(16738): at java.lang.reflect.Method.invoke(Method.java:372)
E/AndroidRuntime(16738): at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:902)
E/AndroidRuntime(16738): at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:697)


This is not the only error, but all the errors are a bit variation of this, which I assume is happening because the Context is null.

Here is the relevant part of my Fragment :

@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
// Inflate the layout for this fragment
View view = inflater.inflate(R.layout.fragment_profile_hi_fives_navigation, container, false);

listView = (ExpandableListView) view.findViewById(R.id.lvExp);

return view;
}

public void showDetails(String galleryId) {
Intent intent = new Intent(getActivity(), UserHiFiveDetailsActivity.class);
intent.putExtra("ID", galleryId);
startActivity(intent);
}

@Override
public void onAttach(Context context) {
super.onAttach(context);

Galleri5Application application = (Galleri5Application) getActivity().getApplication();
API = application.getAPI();
Tracker mTracker = application.getDefaultTracker();
mTracker.setScreenName("ProfileHiFivesNavigationFragment");
mTracker.send(new HitBuilders.ScreenViewBuilder().build());

RequestInterceptor requestInterceptor = new RequestInterceptor() {
@Override
public void intercept(RequestFacade request) {
request.addHeader("Accept", "application/json");
request.addHeader("Authorization", "Token " + token);
}
};
OkHttpClient okHttpClient = new OkHttpClient();
RestAdapter restAdapter = new RestAdapter.Builder()
.setClient(new OkClient(okHttpClient))
.setLogLevel(RestAdapter.LogLevel.FULL)
.setEndpoint(API)
.setRequestInterceptor(requestInterceptor)
.build();

StudioHiFivesAPI studioHiFivesAPI = restAdapter.create(StudioHiFivesAPI.class);
studioHiFivesAPI.getFeed(userId, "json", new Callback<List<StudioHiFives>>() {
@Override
public void success(List<StudioHiFives> studioHiFives, Response response) {
Log.i("USER", "HiFives successful");
adapter = new UserStudioHiFivesNavigationAdapter(getActivity(), studioHiFives, ProfileHiFivesNavigationFragment.this);
listView.setAdapter(adapter);
}

@Override
public void failure(RetrofitError error) {
Log.i("USER", "Profile HiFives Fragment Failed");
}
});
}


Here is the constructor of my Adapter :

private Context context;
private List<StudioHiFives> list;
private Fragment fragment;
private static LayoutInflater inflater = null;
private Picasso picasso;
Typeface opensans;
String API;
private String token;
private TinyDB tinyDB;
private boolean[] followed;
RequestInterceptor requestInterceptor;
RestAdapter restAdapter;
static int radius = Utils.dpToPx(40);
static int imageSize = Utils.dpToPx(57);
Galleri5Application application;
Tracker mTracker;

public UserStudioHiFivesNavigationAdapter(Context context, List<StudioHiFives> list, Fragment fragment) {
this.context = context;
this.list = list;
this.fragment = fragment;
inflater = (LayoutInflater) context.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
opensans = Typeface.createFromAsset(context.getAssets(), "fonts/OpenSans-Regular.ttf");
OkHttpClient okHttpClient = new OkHttpClient();
picasso = new Picasso.Builder(context)
.downloader(new OkHttpDownloader(okHttpClient))
.build();
application = (Galleri5Application) ((AppCompatActivity)context).getApplication();
API = application.getAPI();
mTracker = application.getDefaultTracker();
tinyDB = new TinyDB(context);
token = tinyDB.getString("Galleri5 Access Token");
requestInterceptor = new RequestInterceptor() {
@Override
public void intercept(RequestFacade request) {
request.addHeader("Accept", "application/json");
request.addHeader("Authorization", "Token " + token);
}
};
restAdapter = new RestAdapter.Builder()
.setClient(new OkClient(okHttpClient))
.setLogLevel(RestAdapter.LogLevel.FULL)
.setEndpoint(API)
.setRequestInterceptor(requestInterceptor)
.build();

followed = new boolean[list.size()];
for(int i=0; i<list.size(); i++) {
followed[i] = list.get(i).isFollowing();
}
}


Why these errors are coming and why does my app not crash completely? Where in the fragment should I write code for creating views, initializing contexts and doing network requests? Any help would be highly appreciated.

Answer

Found a way to fix this. Actually getActivity() was returning null. So added this check:

if (isAdded()) {
                    adapter = new UserStudioHiFivesNavigationAdapter(getActivity(), studioHiFives, ProfileHiFivesNavigationFragment.this);
                    listView.setAdapter(adapter);
                }
Comments