简体   繁体   中英

Passing data from activity to fragment nullpointer exception

I'm writing this app where a String has to be passed from activity to fragment but I get a nullpointerexception, here is my code:

Interface:

public interface FragmentCommunicator {

    public void passDataToFragment(String value);

}    

fragment code:

public class YouWinFragment extends Fragment implements FragmentCommunicator {

    TextView score;

    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container,
            Bundle savedInstanceState) {

        return inflater.inflate(R.layout.fragment_youwin, container,false);
    }
    @Override
    public void onActivityCreated(Bundle savedInstanceState) {
        // TODO Auto-generated method stub
        super.onActivityCreated(savedInstanceState);
        score = (TextView) getActivity().findViewById(R.id.score_win);
    }

    @Override
    public void passDataToFragment(String value) {
        // TODO Auto-generated method stub
        score.setText(value);
    }
}

Main activity:

public class MainActivity extends ActionBarActivity{

    Button youWin;  
    String theCounter;
    YouWinFragment youWinFrag;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        youWinFrag=new YouWinFragment();
        setContentView(R.layout.activity_main);

        youWin = (Button) findViewById(R.id.button_youwin);
        youWin.setOnClickListener(new View.OnClickListener() {

            @Override
            public void onClick(View v) {
                // TODO Auto-generated method stub
                FragmentManager fm=getFragmentManager();
                FragmentTransaction ft=fm.beginTransaction();   
                ft.add(R.id.fragment_endgame,youWinFrag );
                ft.commit();

            }
        }); 
        youWinFrag.passDataToFragment("your score is 20");
    }
}
  1. Can you post the logcat so we know where the null pointer exception occurs?

  2. You can look into using setArgument(Bundle) on your fragment.

So in your MainActivity you would have something like:

Bundle bundle = new Bundle();
bundle.putString("STRING_KEY", "your score is 20");
mYouWinFrag = new YouWinFragment();
mYouWinFrag.setArguments(bundle);

Now in your YouWinFragment, you would have:

String score = getArguments().getString("STRING_KEY");
// Optionally, you can have a default value by using getArguments().getString("STRING_KEY", "A default value if the key isn't set");

Please check out the article in the training section on developer.android.com. There is an article that discusses what you're trying to do:

http://developer.android.com/training/basics/fragments/communicating.html .

In this article is a section called:

Deliver a Message to a Fragment

This article should supply what you need.

Also, the interface you've implemented, is that part of the android API? Because I don't see it. So, I assume it's an interface that you've created. If that's the case, you should read the entire article above to understand how fragments communication with one another, the Activity, and how the Activity communicates with a fragment.

Neither do I see the method passDataToFragment in the Fragment or Activity API. So, I assume this is a method you've added. If that's the case, you don't need the @Override annotation since there is no method in the class (Activity or Fragment) to override.

I hope this helps.

Doug

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