0

Before you start answering i tried sharing data using sharedpreferences ... i tried bundle thing but still having null values and application crushes, my application is a draw menu when you click on listview item it brings a fragment in front where i have several edittext textviews , when i click on a edittext to change text it works perfect , the problem is that the edittext is initialy blank and it must load data from the main activity and this where i got NULL

[MainActivity ] onCreate method

EditText     editus_ername = (EditText) findViewById(R.id.editUserName);
            editus_ername.setText("Username");

[fragment class]

EditText editus_ername;

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

        View rootView = inflater.inflate(R.layout.fragment_whats_hot, container, false);

            editus_ername = (EditText)getActivity().findViewById(R.id.editUserName);


        return rootView;


    }




12-14 12:32:48.699: E/AndroidRuntime(1517): FATAL EXCEPTION: main
12-14 12:32:48.699: E/AndroidRuntime(1517): java.lang.NullPointerException
12-14 12:32:48.699: E/AndroidRuntime(1517):     at info.androidhive.slidingmenu.WhatsHotFragment.onCreateView(WhatsHotFragment.java:40)
12-14 12:32:48.699: E/AndroidRuntime(1517):     at android.app.Fragment.performCreateView(Fragment.java:1695)
12-14 12:32:48.699: E/AndroidRuntime(1517):     at android.app.FragmentManagerImpl.moveToState(FragmentManager.java:885)
12-14 12:32:48.699: E/AndroidRuntime(1517):     at android.app.FragmentManagerImpl.moveToState(FragmentManager.java:1057)
12-14 12:32:48.699: E/AndroidRuntime(1517):     at android.app.BackStackRecord.run(BackStackRecord.java:682)
12-14 12:32:48.699: E/AndroidRuntime(1517):     at android.app.FragmentManagerImpl.execPendingActions(FragmentManager.java:1435)
12-14 12:32:48.699: E/AndroidRuntime(1517):     at android.app.FragmentManagerImpl$1.run(FragmentManager.java:441)
12-14 12:32:48.699: E/AndroidRuntime(1517):     at android.os.Handler.handleCallback(Handler.java:725)
12-14 12:32:48.699: E/AndroidRuntime(1517):     at android.os.Handler.dispatchMessage(Handler.java:92)
12-14 12:32:48.699: E/AndroidRuntime(1517):     at android.os.Looper.loop(Looper.java:137)
12-14 12:32:48.699: E/AndroidRuntime(1517):     at android.app.ActivityThread.main(ActivityThread.java:5041)
12-14 12:32:48.699: E/AndroidRuntime(1517):     at java.lang.reflect.Method.invokeNative(Native Method)
12-14 12:32:48.699: E/AndroidRuntime(1517):     at java.lang.reflect.Method.invoke(Method.java:511)
12-14 12:32:48.699: E/AndroidRuntime(1517):     at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:793)
12-14 12:32:48.699: E/AndroidRuntime(1517):     at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:560)
12-14 12:32:48.699: E/AndroidRuntime(1517):     at dalvik.system.NativeStart.main(Native Method)
user1642036
  • 195
  • 2
  • 11

3 Answers3

1

you are calling the edittext using getactivity, change this like;

 editus_ername = (EditText)getActivity().findViewById(R.id.editUserName);

with;

 editus_ername = (EditText)rootView.findViewById(R.id.editUserName);

and if you want to pass values from your activity to the fragment, in Activity.java use;

Bundle bundle = new Bundle();
bundle.putString("value", value);
fragment.setArguments(bundle);

and in fragment.java use;

value= getArguments().getString("value");
0

Inside your fragment class, if the EditText is located in your R.layout.fragment_whats_hot layout, so do this instead :

editus_ername = (EditText) rootView.findViewById(R.id.editUserName);

And if you want to pass data to your fragment class, from your main activity:

  1. You can use arguments
  2. Or create a method inside your fragment :

    public void test(int value) { // do stuff }

And call this method inside main activity.

Here is a link from the doc

S.Thiongane
  • 6,883
  • 3
  • 37
  • 52
  • i tried the bundle method i always get null values when declare editus_ername = (EditText) rootView.findViewById(R.id.editUserName); – user1642036 Dec 14 '14 at 12:40
  • that means that your edit text is not defined inside this layout. Paste your layout's content so ! – S.Thiongane Dec 14 '14 at 12:46
-2

try send your String by fragment constructor or a new public method.

for example by constructor:

Fragment Class:

String _userName;
EditText editus_ername;

public FragmentClassName(String userName){
_userName = userName;
}



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

        View rootView = inflater.inflate(R.layout.fragment_whats_hot, container, false);

            editus_ername = (EditText)getActivity().findViewById(R.id.editUserName);

            editus_ername.setText(_userName);

        return rootView;


    }

MainActivity:

new FragmentClassName("Username");
Hamidreza Samadi
  • 637
  • 1
  • 7
  • 24