액티비티에서 뷰페이저로 값 전달 할일 있으신분들
java.lang.NullPointerException: Attempt to invoke virtual method
'java.lang.String android.os.BaseBundle.getString(java.lang.String)' on a null object reference
메시지와 함께 bundle에서 null 오류날 수도 있습니다.
1
2
3
4
|
Bundle bundle = new Bundle();
bundle.putString("goodsName", goodsName);
yourFragment.setArguments(bundle);
|
cs |
Activity 쪽에서 저렇게 번들에 put하고 viewpager의 프래그먼트로setArguments(bundle) 하고
1
2
3
4
|
Bundle bundle = getArgument();
String goodsName = bundle.getString("goodsName");
http://colorscripter.com/info#e" target="_blank" style="color:#4f4f4ftext-decoration:none">Colored by Color Scripter
|
cs |
Fragment쪽에서 이렇게 받는 형태의 예제가 자주 보여서 적용했더니 에러 났어요.
하지만 아래 링크의 도움을 받아서 해결 했어요.
https://stackoverflow.com/questions/12739909/send-data-from-activity-to-fragment-in-android
Send data from activity to fragment in Android
I have two classes. First is activity, second is a fragment where I have some EditText. In activity I have a subclass with async-task and in method doInBackground I get some result, which I save to
stackoverflow.com
에러를 잡게된 포인트는
기존의 방식에서
1
2
3
4
|
MotherActivity activity = (MotherActivity)getActivity();
Bundle results = activity.getMyData();
String value1 = results.getString("val1");
|
cs |
getActivity()를 활용하고나서 Bundle 에러가 안납니다.
만세~