2014-09-22 2 views
-2

일부 데이터를 묶음에 넣고 조각으로 전송하려고합니다. 그러나 로그는 조각에서 NullPointerException 오류를 보여 주며 둘 사이의 관계가 무엇인지 알지 못합니다.android Fragment에서 NullPointerException

오류 코드 :

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) 

프래그먼트 번호 :

@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")); 

} 

내가 묶음 데이터를 입력하는 작업.

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(); 
     } 

    } 

레이아웃

<?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> 
+0

와 레이아웃이 onActivityCreated에에서 onCreate에서 코드 블록을 교체 레이아웃을

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

변화를 사용하여 해당 교체 할 것() 메소드 – santalu

+0

내가 생각하는 지금은 잘하지만 여전히 오류가 있습니다 : 'java.lang.IllegalArgumentException : 조각에 대한 0x7f070075 (com.example.user.unchained : id/frgmnt)의 뷰가 발견되지 않았습니다. HeaderFragement {53c330a8 # 0 id = 0x7f070075} –

+0

xml 파일을 공유 할 수 있습니까? – santalu

답변

0

기본 컨테이너 내에 조각 컨테이너를 추가해야합니다. 당신이

<?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> 
+0

하지만 주 레이아웃의 조각 선형 레이아웃을 대체하지만 데이터를 전송하는 독립 레이아웃은 대체하지 않습니다. –

+0

조각이 작동하는 방식입니다. – santalu

+0

데이터를 새로운 조각으로 표시하고 주 레이아웃이 아닌 데이터를 표시하고 싶습니다./ 아무튼 고맙습니다. –

2

onCreate()getView()를 호출 너무 일찍이며 null를 돌려줍니다. 프래그먼트 수명주기에서 프래그먼트의보기 계층을 onCreateView() 이상으로 이동해야하는 코드를 연기하십시오.

+0

나는 그것을했지만 여전히 java.lang.IllegalArgumentException의 새로운 probleme : 0x7f070075 (com.example.user.unchained : id/frgmnt) 조각에 대해 HeaderFragement {53c330a8 # 0 id = 0x7f070075} ' –

+0

조각 트랜잭션을 사용하여 조각을 넣으려는 컨테이너의 활동 레이아웃이 누락되었습니다. – laalto

+0

기본 조각이 아닌 독립적 인 조각으로 표시하는 방법 –

관련 문제