NullPointer Exception while replacing fragment in android

315 views Asked by At

I want to call ShowProductDetail Fragment from home fragment but when I call FragmentTransaction I am getting NullPointerException. Here is the error log:

06-24 21:35:21.484: E/AndroidRuntime(11633): FATAL EXCEPTION: main
06-24 21:35:21.484: E/AndroidRuntime(11633): Process: org.nick.yarntrading, PID: 11633
06-24 21:35:21.484: E/AndroidRuntime(11633): java.lang.NullPointerException
06-24 21:35:21.484: E/AndroidRuntime(11633):    at org.nick.yarntrading.Home.callproductDetails(Home.java:63)
06-24 21:35:21.484: E/AndroidRuntime(11633):    at org.nick.card_view.DataListAdapter$1.onClick(DataListAdapter.java:90)
06-24 21:35:21.484: E/AndroidRuntime(11633):    at android.view.View.performClick(View.java:4463)
06-24 21:35:21.484: E/AndroidRuntime(11633):    at android.view.View$PerformClick.run(View.java:18770)
06-24 21:35:21.484: E/AndroidRuntime(11633):    at android.os.Handler.handleCallback(Handler.java:808)
06-24 21:35:21.484: E/AndroidRuntime(11633):    at android.os.Handler.dispatchMessage(Handler.java:103)
06-24 21:35:21.484: E/AndroidRuntime(11633):    at android.os.Looper.loop(Looper.java:193)
06-24 21:35:21.484: E/AndroidRuntime(11633):    at android.app.ActivityThread.main(ActivityThread.java:5292)
06-24 21:35:21.484: E/AndroidRuntime(11633):    at java.lang.reflect.Method.invokeNative(Native Method)
06-24 21:35:21.484: E/AndroidRuntime(11633):    at java.lang.reflect.Method.invoke(Method.java:515)
06-24 21:35:21.484: E/AndroidRuntime(11633):    at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:824)
06-24 21:35:21.484: E/AndroidRuntime(11633):    at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:640)
06-24 21:35:21.484: E/AndroidRuntime(11633):    at dalvik.system.NativeStart.main(Native Method)

here is the Home class from where I'm getting error

import android.annotation.SuppressLint;
import android.app.Activity;
import android.os.Bundle;
import android.support.v4.app.Fragment;
import android.support.v4.app.FragmentActivity;
import android.support.v4.app.FragmentTransaction;
 import android.support.v7.widget.PopupMenu.OnMenuItemClickListener;
import android.view.LayoutInflater;
import android.view.Menu;
import android.view.MenuInflater;
import android.view.MenuItem;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ListView;
import android.widget.RelativeLayout;
import org.nick.card_view.DataListAdapter;


public class Home extends Fragment implements OnMenuItemClickListener{
App a;
ListView card_list;
DataListAdapter adapter;
RelativeLayout prodctView;
Activity act;

@Override
public void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    a =(App)getActivity().getApplication();
    act = getActivity();
}
@SuppressLint("InflateParams") @Override
public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {

     prodctView = (RelativeLayout)inflater.inflate(R.layout.home_listview,null);
        card_list = (ListView)prodctView.findViewById(android.R.id.list);
    adapter = new DataListAdapter(a,getActivity().getSupportFragmentManager());
    card_list.setAdapter(adapter);

    return prodctView;
}
@Override
public void onCreateOptionsMenu(Menu menu, MenuInflater inflater) {
    // TODO Auto-generated method stub
    super.onCreateOptionsMenu(menu, inflater);
    inflater.inflate(R.menu.main, menu);
}
@Override
public boolean onMenuItemClick(MenuItem arg0) {
    // TODO Auto-generated method stub
    return false;
}
public void callproductDetails()
{

    Fragment d = new ShowProductDetail();
    FragmentTransaction ft =getActivity().getSupportFragmentManager().beginTransaction();
    ft.setCustomAnimations(R.anim.slide_right, android.R.anim.slide_out_right,
            android.R.anim.slide_in_left, android.R.anim.slide_out_right );
    ft.replace(R.id.frame, d);
    ft.commit();
}
@Override
 public void onResume() {
    // TODO Auto-generated method stub
    super.onResume();
    if(!((App)getActivity().getApplication()).fragmentStack.contains(this))
        ((App)getActivity().getApplication()).fragmentStack.add(this);
}
public void redirect()
{
  FragmentTransaction ft = ((FragmentActivity) act).getSupportFragmentManager().beginTransaction();
    ft.setCustomAnimations(R.anim.slide_right,android.R.anim.slide_out_right);
    ft.replace(R.id.frame, a.fragmentStack.lastElement()).commit();
}

 }

I am calling callproductDetails() method from DataListAdapter class on view.setOnclickListener method. Any idea is appreciated. Thanks in advance...

1

There are 1 answers

0
JozeRi On

Well, basically, I wouldn't recommend using

getActivity()

as your context for getSupportFragmentManager, since getActivity() can easily return a null value and throw a NullPointerException.

try giving a different context, or at least wrap it up in a

if (getActivity() != null) 

at least then you'll know for sure that your context is your problem.