简体   繁体   中英

Null object reference when passing variable from activity to fragment

I am getting a null object reference when passing a variable from an activity to a fragment and I can't find out what the problem is. The variable I am trying to pass is currentPollName . Any help would be appreciated.

Here is the GroupActivity:

public class GroupActivity extends AppCompatActivity {

    private ViewPager myViewPager;
    private TabLayout myTabLayout;
    private GroupTabsAccessorAdapter myTabsAccessorAdapter;
    private String currentPollName;

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

        currentPollName = getIntent().getExtras().get("pollName").toString();
        Toast.makeText(GroupActivity.this, currentPollName, Toast.LENGTH_SHORT).show();

        Bundle bundle = new Bundle();
        bundle.putString("pollName", currentPollName);
        PollFragment fragInfo = new PollFragment();
        fragInfo.setArguments(bundle);

        Toolbar toolbar = (Toolbar) findViewById(R.id.group_page_toolbar);
        setSupportActionBar(toolbar);
        getSupportActionBar().setTitle(currentPollName);

        myViewPager = (ViewPager) findViewById(R.id.group_tabs_pager);
        myTabsAccessorAdapter = new GroupTabsAccessorAdapter(getSupportFragmentManager());
        myViewPager.setAdapter(myTabsAccessorAdapter);

        myTabLayout = (TabLayout) findViewById(R.id.group_tabs);
        myTabLayout.setupWithViewPager(myViewPager);
    }

    public String getGroupName() {
        return currentPollName;
    }
}

Here is the PollFragment OnCreateView

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

        currentPollName = getArguments().getString("pollName");


        mAuth = FirebaseAuth.getInstance();
        currentUserID = mAuth.getCurrentUser().getUid();
        UsersRef = FirebaseDatabase.getInstance().getReference().child("Users");
        GroupNameRef = FirebaseDatabase.getInstance().getReference().child("Groups").child(currentPollName);

        InitializeFields();

        RetrieveDisplayPollOptions();

        newOption.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                RequestNewPollOption();
            }
        });

        submitOption.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                SavePollOption();
            }
        });

        return pollView;

    }

Here is the Itent that starts the GroupActivity

list_view.setOnItemClickListener(new AdapterView.OnItemClickListener() {
            @Override
            public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
                String currentPollName = list_view.getItemAtPosition(position).toString();

                Intent pollIntent = new Intent(getContext(), GroupActivity.class);
                pollIntent.putExtra("pollName", currentPollName);
                startActivity(pollIntent);
            }
        });

I found the following things in the code

  1. fragInfo Fragment is not passed to GroupTabsAccessorAdapter . So add the fragment to the adapter and then write myViewPager.setAdapter(myTabsAccessorAdapter);
  2. In PollFragment move currentPollName = getArguments().getString("pollName"); in onViewCreated as below.
public void onViewCreated(View view, @Nullable Bundle savedInstanceState)
        {
            super.onViewCreated(view, savedInstanceState);
            currentPollName = getArguments().getString("pollName");
        }`

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM