2014-09-22 71 views
-2

我試圖將一些數據放入一個捆綁包並將其傳輸到一個片段。但是日誌顯示了片段中的NullPointerException的錯誤,我不知道兩者之間的關係是什麼。Android碎片上的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

佈局取代從的onCreate到onActivityCreated您的代碼塊()方法 – santalu 2014-09-22 14:31:12

+0

我認爲現在是很好的,但仍然有一個錯誤: 'java.lang.IllegalArgumentException:沒有找到id爲0x7f070075(com.example.user.unchained:id/frgmnt)的片段HeaderFragement {53c330a8#0 id = 0x7f070075} ' – 2014-09-22 14:35:33

+0

你能分享你的xml文件請 – santalu 2014-09-22 14:36:58

回答

0

你應該在你的主佈局中添加你的片段容器。那麼你會使用這個佈局

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

變化做更換與此

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

但它會替換主佈局中的片段線性佈局,但不會替換傳輸數據中的獨立佈局?! – 2014-09-22 14:51:57

+0

多數民衆贊成如何片段工作 – santalu 2014-09-22 14:52:44

+0

我想要顯示在一個新的片段,而不是主要佈局的數據我的整個問題:/ 謝謝任何​​方式 – 2014-09-22 14:54:34

2

onCreate()是爲時過早getView(),它會返回null。推遲片段生命週期中需要觸及片段視圖層次的代碼爲onCreateView()或更高版本。

+0

我已經做到了,但仍然是一個新的問題'java.lang.IllegalArgumentException:未發現id爲0x7f070075(com.example.user.unchained:id/frgmnt)用於片段HeaderFragement {53c330a8#0 id = 0x7f070075}' – 2014-09-22 14:43:43

+0

您的活動佈局缺少您嘗試將片段放入片段事務中的容器。 – laalto 2014-09-22 14:45:58

+0

如何將其顯示在獨立片段中而不是主 – 2014-09-22 14:55:45