使用接口将数据数组从Activity传递到Fragment。我收到错误

时间:2019-09-04 09:18:25

标签: android-studio

我正在将值从活动传递到片段。在此我收到一个错误,说尝试在null对象引用上调用接口方法

E/AndroidRuntime: FATAL EXCEPTION: main
    Process: com.wristcode.daasasamsara, PID: 17540
    java.lang.NullPointerException: Attempt to invoke interface method 'void com.wristcode.daasasamsara.BhajanaTabActivity$FragmentInterfaceSabhe.sendSabheMethod(java.util.ArrayList, java.util.ArrayList)' on a null object reference
        at com.wristcode.daasasamsara.BhajanaTabActivity$2.onResponse(BhajanaTabActivity.java:271)
        at com.wristcode.daasasamsara.BhajanaTabActivity$2.onResponse(BhajanaTabActivity.java:225)
        at com.android.volley.toolbox.StringRequest.deliverResponse(StringRequest.java:78)
        at com.android.volley.toolbox.StringRequest.deliverResponse(StringRequest.java:30)
        at com.android.volley.ExecutorDelivery$ResponseDeliveryRunnable.run(ExecutorDelivery.java:106)
        at android.os.Handler.handleCallback(Handler.java:739)
        at android.os.Handler.dispatchMessage(Handler.java:95)
        at android.os.Looper.loop(Looper.java:148)
        at android.app.ActivityThread.main(ActivityThread.java:7325)
        at java.lang.reflect.Method.invoke(Native Method)
        at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:1230)
        at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:1120)

这是一个错误

BhajanaTabActivity.java

 public class BhajanaTabActivity extends AppCompatActivity implements 
 SwipeRefreshLayout.OnRefreshListener{

 private FragmentInterfaceSabhe fragmentInterfaceListener2;

ArrayList<String> SabetitleList = new ArrayList<String>();
ArrayList<String> SabedescList = new ArrayList<String>();

@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_bhajanatab);

    setupViewPager(viewPager);

    tabLayout = (TabLayout) findViewById(R.id.tabs);
    tabLayout.setupWithViewPager(viewPager);

    SabetitleList.add("Sabhe1);
    SabedescList.add("desc1");
    SabetitleList.add("Sabhe2);
    SabedescList.add("desc2");
    SabetitleList.add("Sabhe3);
    SabedescList.add("desc3");


    fragmentInterfaceListener2.sendSabheMethod(SabetitleList, 
    SabedescList);
    //For above line im getting error

}


public interface FragmentInterfaceSabhe {
    void sendSabheMethod(ArrayList<String> Name, ArrayList<String> Designation);
}
public void setOnDataListener1(FragmentInterfaceSabhe fragmentInterfaceSabhe){
    fragmentInterfaceListener2 = fragmentInterfaceSabhe;
}


private void setupViewPager(ViewPager viewPager) {
    ViewPagerAdapter adapter = new ViewPagerAdapter(getSupportFragmentManager());
    adapter.addFragment(new SanghaFragment(), "ಸಂಘದ ಬಗ್ಗೆ");
    adapter.addFragment(new SabheFragment(), "ಸಭೆ");
    viewPager.setAdapter(adapter);
}


class ViewPagerAdapter extends FragmentPagerAdapter {
    private final List<Fragment> mFragmentList = new ArrayList<>();
    private final List<String> mFragmentTitleList = new ArrayList<>();

    public ViewPagerAdapter(FragmentManager manager) {
        super(manager);
    }

    @Override
    public Fragment getItem(int position) {
        return mFragmentList.get(position);
    }

    @Override
    public int getCount() {
        return mFragmentList.size();
    }

    public void addFragment(Fragment fragment, String title) {
        mFragmentList.add(fragment);
        mFragmentTitleList.add(title);
    }

    @Override
    public CharSequence getPageTitle(int position) {
        return mFragmentTitleList.get(position);
    }
}
}

SabheFragment.java

 public class SabheFragment extends Fragment implements 
 SwipeRefreshLayout.OnRefreshListener, BhajanaTabActivity.FragmentInterfaceSabhe {

public SabheFragment() {
    // Required empty public constructor
}

@Override
public void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    ((BhajanaTabActivity)getActivity()).setOnDataListener1(this);
}

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

    return v;
}



@Override
public void sendSabheMethod(ArrayList<String> Name, ArrayList<String> Designation) {

}
}

0 个答案:

没有答案