-2

I’m trying to put some data into a bundle and transfer it to a fragment. But the log show me an error of NullPointerException in the fragment and I don't know what is the relation between the two.

The error code :

09-22 14:19:03.216    1918-1918/com.example.user.unchained E/AndroidRuntime﹕ FATAL EXCEPTION: main
Process: com.example.user.unchained, PID: 1918
java.lang.NullPointerException
        at com.example.user.unchained.HeaderFragement.onCreate(HeaderFragement.java:85)
        at android.support.v4.app.Fragment.performCreate(Fragment.java:1481)
        at android.support.v4.app.FragmentManagerImpl.moveToState(FragmentManager.java:908)
        at android.support.v4.app.FragmentManagerImpl.moveToState(FragmentManager.java:1121)
        at android.support.v4.app.BackStackRecord.run(BackStackRecord.java:682)
        at android.support.v4.app.FragmentManagerImpl.execPendingActions(FragmentManager.java:1484)
        at android.support.v4.app.FragmentManagerImpl$1.run(FragmentManager.java:450)
        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:5017)
        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)

The Fragment code :

@Override
public void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    if (getArguments() != null) {
        mParam1 = getArguments().getString(ARG_PARAM1);
        mParam2 = getArguments().getString(ARG_PARAM2);
    }

    name = (TextView) getView().findViewById(R.id.nameH);
    img = (ImageView) getView().findViewById(R.id.imageViewHeader);

    Bundle infos = getArguments();
    name.setText(infos.getString("Name"));
    img.setImageBitmap(getBitmapFromURL(infos.getString("ImgUrl")));

    //Name.setText(Name.getText()+" "+infos.getString("Name"));

}

The Activity in which i put data in the bundle.

protected void onPostExecute(final String res) {
        //LoginTask = null;
        //showProgress(false);

        try {
             jObj = new JSONObject(res);
        } catch (JSONException e) {
            e.printStackTrace();
        }
        try {
            if (jObj.getString("code").equals("1")) {

                infos = new Bundle();

                infos.putString("ID",jObj.getString("ID"));
                infos.putString("Name",jObj.getString("display_name"));
                infos.putString("ImgUrl","http://unchained-network.com/uploads/profilpics/53f5c570b6ac2.png");

                android.support.v4.app.FragmentTransaction tr = getSupportFragmentManager().beginTransaction();
                HeaderFragement hf = new HeaderFragement();

                //Charger les infos dans l'activité
                hf.setArguments(infos);
                tr.replace(R.id.frgmnt, hf);
                tr.commit();

                Intent myIntent = new Intent(getActivity(), HomesActivity.class);
                //Lançer l'activité
                startActivityForResult(myIntent, 0);

            } else {
                //password.setError(getString(R.string.error_incorrect_password));
                //password.requestFocus();
                AlertDialog.Builder builder = new AlertDialog.Builder(getActivity());
                builder.setTitle("Error !");
                builder.setMessage("The information entered is incorrect.\nPlease try again!")
                        .setCancelable(false)
                        .setPositiveButton("OK", new DialogInterface.OnClickListener() {
                            public void onClick(DialogInterface dialog, int id) {
                                dialog.cancel();
                            }
                        });
                AlertDialog alert = builder.create();
                alert.show();

            }
        } catch (JSONException e) {
            e.printStackTrace();
        }

    }

The layout

<?xml version="1.0" encoding="utf-8"?>

<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:background="#000"
android:orientation="vertical" >

<!-- Login progress -->
<ProgressBar
    android:id="@+id/login_progress"
    style="?android:attr/progressBarStyleLarge"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:layout_marginTop="50dp"
    android:visibility="gone"/>

<ScrollView
    android:layout_width="match_parent"
    android:layout_height="0dp"
    android:layout_weight="1" >

    <LinearLayout
        android:id="@+id/login_form"
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:orientation="vertical"
        android:paddingBottom="10dp"
        android:paddingLeft="15dp"
        android:paddingRight="15dp"
        android:paddingTop="5dp" >

        <ImageView
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:id="@+id/logo"
            android:focusableInTouchMode="true"
            android:src="@drawable/unchained_beta_logo"
            android:layout_marginTop="20dp"
            android:gravity="center"
            android:contentDescription="Unchained-Network Logo"
            android:layout_alignParentTop="true"
            android:layout_alignParentRight="true"
            android:layout_alignParentEnd="true" />

        <EditText
            style="@style/SignInEditText"
            android:layout_width="match_parent"
            android:layout_height="match_parent"
            android:completionThreshold="1"
            android:inputType="textEmailAddress"
            android:layout_marginTop="64dp"
            android:layout_below="@+id/logo"
            android:layout_alignParentRight="true"
            android:layout_alignParentEnd="true"
            android:id="@+id/email"
            android:hint="Email" />

        <EditText
            style="@style/SignInEditText"
            android:layout_width="match_parent"
            android:layout_height="match_parent"
            android:completionThreshold="1"
            android:inputType="textPassword"
            android:layout_marginTop="20dp"
            android:layout_below="@+id/email"
            android:layout_alignParentRight="true"
            android:layout_alignParentEnd="true"
            android:id="@+id/password"
            android:hint="Password" />

        <Button
            android:id="@+id/connexion"
            style="@style/ShadowText"
            android:layout_width="match_parent"
            android:layout_height="match_parent"
            android:layout_marginTop="50dp"
            android:layout_below="@+id/password"
            android:background="@color/green_android"
            android:gravity="center"
            android:padding="10dp"
            android:textColor="#fff"
            android:textSize="20sp"
            android:textStyle="bold"
            android:text="Go Ride !"

            />


        <TextView

            android:layout_width="200dp"
            android:id="@+id/passf"
            style="@style/ShadowText"
            android:layout_height="30dp"
            android:layout_marginLeft="70dp"
            android:text="Forgotten your password?"
            android:layout_marginTop="20dp"
            android:gravity="center"
            android:textSize="10sp"
            android:textStyle="bold"
            android:textColor="#feed01" />

        <TextView

            android:layout_width="200dp"
            style="@style/ShadowText"
            android:layout_height="30dp"
            android:layout_marginLeft="70dp"
            android:text="No account yet ? Get one "
            android:layout_marginTop="5dp"
            android:gravity="center"
            android:id="@+id/newaccount"
            android:textSize="14sp"
            android:textStyle="bold"
            android:textColor="#fff" />

    </LinearLayout>
</ScrollView>

</LinearLayout>
K.Ly
  • 75
  • 3
  • 11

2 Answers2

2

onCreate() is too early to call getView() and it will return null. Postpone the code that needs to touch the fragment's view hierarchy to onCreateView() or later in the fragment lifecycle.

laalto
  • 150,114
  • 66
  • 286
  • 303
  • i've do it but still a new probleme of `java.lang.IllegalArgumentException: No view found for id 0x7f070075 (com.example.user.unchained:id/frgmnt) for fragment HeaderFragement{53c330a8 #0 id=0x7f070075}` – K.Ly Sep 22 '14 at 14:43
  • Your activity layout is missing the container you're trying to put the fragment in with a fragment transaction. – laalto Sep 22 '14 at 14:45
  • How to display it in an independent fragment and not the main – K.Ly Sep 22 '14 at 14:55
0

you should add your fragment container inside your main layout. then you will do that replacement using this layout

 <LinearLayout
        android:id="@+id/frgmnt"
        android:layout_width="fill_parent"
        android:layout_height="wrap_content"
        android:orientation="vertical" />

change your layout with this

<?xml version="1.0" encoding="utf-8"?>

<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:background="#000"
android:orientation="vertical" >

<!-- Login progress -->
<ProgressBar
    android:id="@+id/login_progress"
    style="?android:attr/progressBarStyleLarge"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:layout_marginTop="50dp"
    android:visibility="gone"/>

<ScrollView
    android:layout_width="match_parent"
    android:layout_height="0dp"
    android:layout_weight="1" >

    <LinearLayout
        android:id="@+id/login_form"
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:orientation="vertical"
        android:paddingBottom="10dp"
        android:paddingLeft="15dp"
        android:paddingRight="15dp"
        android:paddingTop="5dp" >

        <ImageView
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:id="@+id/logo"
            android:focusableInTouchMode="true"
            android:src="@drawable/unchained_beta_logo"
            android:layout_marginTop="20dp"
            android:gravity="center"
            android:contentDescription="Unchained-Network Logo"
            android:layout_alignParentTop="true"
            android:layout_alignParentRight="true"
            android:layout_alignParentEnd="true" />

        <EditText
            style="@style/SignInEditText"
            android:layout_width="match_parent"
            android:layout_height="match_parent"
            android:completionThreshold="1"
            android:inputType="textEmailAddress"
            android:layout_marginTop="64dp"
            android:layout_below="@+id/logo"
            android:layout_alignParentRight="true"
            android:layout_alignParentEnd="true"
            android:id="@+id/email"
            android:hint="Email" />

        <EditText
            style="@style/SignInEditText"
            android:layout_width="match_parent"
            android:layout_height="match_parent"
            android:completionThreshold="1"
            android:inputType="textPassword"
            android:layout_marginTop="20dp"
            android:layout_below="@+id/email"
            android:layout_alignParentRight="true"
            android:layout_alignParentEnd="true"
            android:id="@+id/password"
            android:hint="Password" />

        <Button
            android:id="@+id/connexion"
            style="@style/ShadowText"
            android:layout_width="match_parent"
            android:layout_height="match_parent"
            android:layout_marginTop="50dp"
            android:layout_below="@+id/password"
            android:background="@color/green_android"
            android:gravity="center"
            android:padding="10dp"
            android:textColor="#fff"
            android:textSize="20sp"
            android:textStyle="bold"
            android:text="Go Ride !"

            />


        <TextView

            android:layout_width="200dp"
            android:id="@+id/passf"
            style="@style/ShadowText"
            android:layout_height="30dp"
            android:layout_marginLeft="70dp"
            android:text="Forgotten your password?"
            android:layout_marginTop="20dp"
            android:gravity="center"
            android:textSize="10sp"
            android:textStyle="bold"
            android:textColor="#feed01" />

        <TextView

            android:layout_width="200dp"
            style="@style/ShadowText"
            android:layout_height="30dp"
            android:layout_marginLeft="70dp"
            android:text="No account yet ? Get one "
            android:layout_marginTop="5dp"
            android:gravity="center"
            android:id="@+id/newaccount"
            android:textSize="14sp"
            android:textStyle="bold"
            android:textColor="#fff" />

    </LinearLayout>
</ScrollView>

<LinearLayout
            android:id="@+id/frgmnt"
            android:layout_width="fill_parent"
            android:layout_height="wrap_content"
            android:orientation="vertical" />

</LinearLayout>
Fatih Santalu
  • 4,641
  • 2
  • 17
  • 34
  • But it will replace the fragment linear layout in the main layout but not the independent layout which in i transfer the data ?! – K.Ly Sep 22 '14 at 14:51
  • My whole problem that I want to show the data in a new fragment and not the main layout: / Thanks any way – K.Ly Sep 22 '14 at 14:54
  • i suggest you to learn fragments clearly. if you want to use independent screen then just use activity. – Fatih Santalu Sep 22 '14 at 14:57
  • http://stackoverflow.com/questions/8597769/when-should-i-use-fragments-in-android-applications-why-to-use-fragments check this out – Fatih Santalu Sep 22 '14 at 15:00
  • These is the point i want to show some data from an activity to an fragment that will placed on an activity with others fragments so I’m more or less forced to do like that thanks any way ! – K.Ly Sep 22 '14 at 15:06