-3

I'm trying to set EditText value when activity is starting, here is what I'm doing:

      @Override
        public void onCreate(Bundle savedInstanceState) {
            super.onCreate(savedInstanceState);
            SharedPreferences mPrefs = this.getActivity().getSharedPreferences("userinfoafterlogin", MODE_PRIVATE); //add key
            String response = mPrefs.getString("userinfo", null);

            try{
                JSONObject json = new JSONObject(response);
                json = (JSONObject)json.get("nameValuePairs");
                json = (JSONObject)json.get("userData");
                json = (JSONObject)json.get("nameValuePairs");
                String fullname = (String)json.get("fullname");
                String username = (String)json.get("username");

                EditText editText = (EditText)getActivity().findViewById(R.id.fullnametext);
                editText.setText(fullname);

                Log.i("data", " retrieve --> " + fullname);
            }catch (JSONException e) {
                Log.e("MYAPP", "unexpected JSON exception", e);
            }
  }

But, when I run it, I get this error

12-29 14:29:48.321 3273-3273/com.example.boby.srsly E/AndroidRuntime: FATAL EXCEPTION: main Process: com.example.boby.srsly, PID: 3273 java.lang.NullPointerException at com.example.boby.srsly.ProfilFragment.onCreate(ProfilFragment.java:71) at android.app.Fragment.performCreate(Fragment.java:1678) at android.app.FragmentManagerImpl.moveToState(FragmentManager.java:859) at android.app.FragmentManagerImpl.moveToState(FragmentManager.java:1062) at android.app.BackStackRecord.run(BackStackRecord.java:684) at android.app.FragmentManagerImpl.execPendingActions(FragmentManager.java:1447) at android.app.FragmentManagerImpl$1.run(FragmentManager.java:443) at android.os.Handler.handleCallback(Handler.java:733) at android.os.Handler.dispatchMessage(Handler.java:95) at android.os.Looper.loop(Looper.java:136) at android.app.ActivityThread.main(ActivityThread.java:5019) at java.lang.reflect.Method.invokeNative(Native Method) at java.lang.reflect.Method.invoke(Method.java:515) at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:779) at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:595) at dalvik.system.NativeStart.main(Native Method)

How can i fix it ?

I moved it inside onCreateView

public View onCreateView(LayoutInflater inflater, ViewGroup container,
                         Bundle savedInstanceState) {
    // Inflate the layout for this fragment
    View v = inflater.inflate(R.layout.fragment_profil, container, false);
    SharedPreferences mPrefs = this.getActivity().getSharedPreferences("userinfoafterlogin", MODE_PRIVATE); //add key
    String response = mPrefs.getString("userinfo", null);

    try{
        JSONObject json = new JSONObject(response);
        json = (JSONObject)json.get("nameValuePairs");
        json = (JSONObject)json.get("userData");
        json = (JSONObject)json.get("nameValuePairs");
        String fullname = (String)json.get("fullname");
        String username = (String)json.get("username");

        EditText editText = (EditText)v.findViewById(R.id.fullnametext);
        editText.setText(fullname);
        Log.i("data", " retrieve --> " + fullname);
        return v;
    }catch (JSONException e) {
        Log.e("MYAPP", "unexpected JSON exception", e);
        return v;
    }

}

Now I got this error

12-29 14:35:09.801 3383-3383/com.example.boby.srsly E/AndroidRuntime: FATAL EXCEPTION: main Process: com.example.boby.srsly, PID: 3383 java.lang.ClassCastException: android.support.v7.widget.AppCompatTextView cannot be cast to android.widget.EditText at com.example.boby.srsly.ProfilFragment.onCreateView(ProfilFragment.java:78) at android.app.Fragment.performCreateView(Fragment.java:1700) at android.app.FragmentManagerImpl.moveToState(FragmentManager.java:890) at android.app.FragmentManagerImpl.moveToState(FragmentManager.java:1062) at android.app.BackStackRecord.run(BackStackRecord.java:684) at android.app.FragmentManagerImpl.execPendingActions(FragmentManager.java:1447) at android.app.FragmentManagerImpl$1.run(FragmentManager.java:443) at android.os.Handler.handleCallback(Handler.java:733) at android.os.Handler.dispatchMessage(Handler.java:95) at android.os.Looper.loop(Looper.java:136) at android.app.ActivityThread.main(ActivityThread.java:5019) at java.lang.reflect.Method.invokeNative(Native Method) at java.lang.reflect.Method.invoke(Method.java:515) at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:779) at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:595) at dalvik.system.NativeStart.main(Native Method)

JimHawkins
  • 4,843
  • 8
  • 35
  • 55
Bossby
  • 49
  • 1
  • 9

2 Answers2

0

First you need to override onActivityCreated()method in your fragment and initialize the EditText inside on activitycreated() method.

EditText editText ;

like this

@Override
public void onActivityCreated(@Nullable Bundle savedInstanceState) {
    super.onActivityCreated(savedInstanceState);
    editText = (EditText)getActivity().findViewById(R.id.fullnametext);
            editText.setText(fullname);
}
ZIA ANSARI
  • 131
  • 1
  • 11
umesh shakya
  • 237
  • 2
  • 12
0

First you need to learn about the fragment life cycle Fragments. In onCreate of fragment the view is not created. The view is created in onCreateView method of fragment

 @Override
public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
 View v = inflater.inflate(R.layout.yourView, container, false);
 editText = (EditText)v.findViewById(R.id.fullnametext);
        editText.setText(fullname);
}

So you need to move your layout widgets in this method.

And about your second error you are probably casting the wrong widget:

EXCEPTION: main Process: com.example.boby.srsly, PID: 3383 java.lang.ClassCastException: android.support.v7.widget.AppCompatTextView cannot be cast to android.widget.EditText

So you need to do it like this:

AppCompatTextView textview;
textview= (AppCompatTextView )v.findViewById(R.id.fullnametext);
            textview.setText(fullname);

Or if it's the edittext you want then you need to change your AppCompatTextView to EditText

Umair
  • 6,366
  • 15
  • 42
  • 50