在尝试将listview从片段C填充到片段B时获取Null指针异常

时间:2015-11-17 07:20:19

标签: android android-fragments android-listview

我是android开发的新手。我需要从片段C中的用户在微调器中选择的值填充片段B中的ListView。

到目前为止,我尝试的是bundle方法,但它抛出空指针异常。

我真的很困惑为什么会这样。

这是我的代码:

How to pass spinner value from one fragment to another?

如果有人帮助我,我会很高兴在同一活动的片段之间进行沟通的程序是什么

这是我的from_fragment(片段c)

package com.example.first.servicefirst;
import android.app.Fragment;
import android.content.Context;
import android.os.Bundle;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ArrayAdapter;
import android.widget.Button;
import android.widget.Spinner;

public class NewRequirements extends Fragment {

//public static NewRequirements newInstance(Bundle bundle) {

//     Add myFragment = new Add();
//    myFragment.setArguments(bundle);
//}
    FragmentMigration framgnetmigration;
// }

@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
                         Bundle savedInstanceState) {
    // TODO Auto-generated method stub
    View view = inflater.inflate(R.layout.fragment_dialog_claim, container,
            false);
    Button btnupdate;
    btnupdate=(Button)view.findViewById(R.id.update);
    final Spinner sbu=(Spinner)view.findViewById(R.id.sbuu);
    ArrayAdapter<CharSequence>adaptersbu=ArrayAdapter.createFromResource(
            getActivity().getBaseContext(),R.array.newrequirements,  R.layout.spinnerlayout);
    adaptersbu.setDropDownViewResource(android.R.layout.simple_spinner_dropdown_item);
    sbu.setAdapter(adaptersbu);
    final Spinner bu=(Spinner)view.findViewById(R.id.bu);

    adapterbu.setDropDownViewResource(android.R.layout.simple_spinner_dropdown_item);
    bu.setAdapter(adapterbu);
    final Spinner sbuu=(Spinner)view.findViewById(R.id.sbu);

    adaptersbuu.setDropDownViewResource(android.R.layout.simple_spinner_dropdown_item);
    sbuu.setAdapter(adaptersbuu);
    final Spinner sc=(Spinner)view.findViewById(R.id.sc);
    ;
    adaptersc.setDropDownViewResource(android.R.layout.simple_spinner_dropdown_item);
    sc.setAdapter(adaptersc);
    final Spinner ssc=(Spinner)view.findViewById(R.id.ssc);
    ArrayAdapter<CharSequence>adapterssc=ArrayAdapter.createFromResource
            (    getActivity().getBaseContext(), R.array.newrequirements, R.layout.spinnerlayout);
    adapterssc.setDropDownViewResource(android.R.layout.simple_spinner_dropdown_item);
    ssc.setAdapter(adapterssc);
    final String str=sbu.getSelectedItem().toString();

    btnupdate.setOnClickListener(new View.OnClickListener() {
        @Override
        public void onClick(View v) {
            Bundle bundle = new Bundle();
                    FragmentManager fm=getFragmentManager();
                    Add add = new Add();

                    bundle.putString("yes", str);
                //    Log.i("Bundle", bundle.toString());
                    Log.v("Add", str);
                    add.setArguments(bundle);
                    FragmentTransaction ft=fm.beginTransaction();
                    ft.setTransition(FragmentTransaction.TRANSIT_FRAGMENT_FADE);
                    ft.replace(R.id.content_frame, add,"hi");
                    ft.addToBackStack(null);
                    ft.commit();

         }
    });
    return view;
 }





}


}

这是我的to_fragment(片段B):

package com.example.first.servicefirst;

import android.app.Fragment;
import android.app.FragmentTransaction;
import android.os.Bundle;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ArrayAdapter;
import android.widget.Button;
 import android.widget.ListView;
import android.widget.Spinner;
import android.widget.TextView;

import java.util.ArrayList;


public  class Add extends Fragment implements View.OnClickListener
{
public static Add()
public View onCreateView(LayoutInflater inflater, ViewGroup container,
                         Bundle savedInstanceState) {

    View rootView = inflater.inflate(R.layout.activity_btn_add, container, false);
    Spinner ldsource=(Spinner)rootView.findViewById(R.id.lead_source);
    ArrayAdapter<CharSequence> adapter = ArrayAdapter.createFromResource(
            getActivity().getBaseContext(), R.array.dropbox1, R.layout.spinnerlayout);


    adapter.setDropDownViewResource(android.R.layout.simple_spinner_dropdown_item);
    ldsource.setAdapter(adapter);
 //   EditText editText=(EditText)rootView.findViewById(R.id.title);

    Spinner ldtype=(Spinner)rootView.findViewById(R.id.ldtype);
    ArrayAdapter<CharSequence> adapter1 = ArrayAdapter.createFromResource(
            getActivity().getBaseContext(), R.array.dropbox2,R.layout.spinnerlayout);

    adapter1.setDropDownViewResource(android.R.layout.simple_spinner_dropdown_item);
    ldtype.setAdapter(adapter1);
    Spinner ldstatus=(Spinner)rootView.findViewById(R.id.ldstatus);
    TextView txt=(TextView)rootView.findViewById(R.id.spinnerTarget);
    ArrayAdapter<CharSequence> adapter2 = ArrayAdapter.createFromResource(
            getActivity().getBaseContext(), R.array.dropbox3, R.layout.spinnerlayout);

    adapter2.setDropDownViewResource(android.R.layout.simple_selectable_list_item);
    ldstatus.setAdapter(adapter2);
  //  Bundle bundle=getArguments();
  //  String good=bundle.getString("sbu");
    ArrayList<LdNewsItem> listContact = GetlistContact();
    final   ListView lv = (ListView)rootView.findViewById(R.id.ldrequirements);
    lv.setAdapter(new customListAdapterldrequirements(getActivity(), listContact));
//    lv.setOnTouchListener(new View.OnTouchListener() {
  //      @Override
    //    public boolean onTouch(View v, MotionEvent event) {
    //        return false;
       // }

        // Setting on Touch Listener for handling the touch inside ScrollView


    //});
     return rootView;


}




private ArrayList<LdNewsItem> GetlistContact()
{

    ArrayList<LdNewsItem> contactlist = new ArrayList<>();
    LdNewsItem contact = new LdNewsItem();
    String yog=getArguments().getString("yes");

    for(int i=1;i<=10;i++)
    {
       // contact = new LdNewsItem( );
        contact.setSbu(""+yog);
       // contact.setBu(""+str);
       // contact.setSbuu("Yogeswaran" + str);
        contact.setSc("Sales" + i);
        contact.setSsc("term" + i);
        contact.setReq("business"+i);
        contactlist.add(contact);
    }

    return contactlist;
}


@Override
public void onActivityCreated(Bundle savedInstanceState) {
    super.onActivityCreated(savedInstanceState);

    Fragment fragment=null;

    Button btnrequirements=(Button)getActivity().findViewById(R.id.btnrequirements);
    btnrequirements.setOnClickListener(this);

}
@Override

public void onClick(View v) {
    switch (v.getId()){
        case R.id.btnrequirements:
            Fragment newFragment = new NewRequirements();
            // consider using Java coding conventions (upper first char class names!!!)
            FragmentTransaction transaction = getFragmentManager().beginTransaction();
            FragmentTransaction ft = getActivity()
                    .getFragmentManager().beginTransaction();

            // Replace whatever is in the fragment_container view with this fragment,
            // and add the transaction to the back stack
            transaction.replace(R.id.content_frame, newFragment);
            transaction.addToBackStack(null);

            // Commit the transaction
            transaction.commit();

    }
}



}

1 个答案:

答案 0 :(得分:0)

在您查找参数args.putString("yog",god);时传递的引用字符串应该是相同的yog=getArguments().getString("yog");