为什么我无法从onCreateView方法获取Bundle数据

时间:2014-09-25 16:01:59

标签: android

在我的一个活动中,我设置了一个Bundle,我把一些数据传递给一个片段。

infos = new Bundle();

infos.putString("Id",jObj.getString("ID"));
infos.putString("Name",jObj.getString("display_name"));

HeaderFragement ndf = new HeaderFragement();

//Charger les infos dans le fragement.
ndf.setArguments(infos);

但是在片段onCreateView方法中,我无法获取数据。错误日志表示bundle是NULL。

09-25 15:50:18.394    2245-2245/com.example.user.unchained E/AndroidRuntime﹕ FATAL EXCEPTION: main
Process: com.example.user.unchained, PID: 2245
java.lang.NullPointerException
        at com.example.user.unchained.HeaderFragement.onCreateView(HeaderFragement.java:108)

所以任何人都有想法解决这个问题

这是onCreateView函数

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

    Bundle i = this.getArguments();

     View v = inflater.inflate(R.layout.fragment_header_fragement, container, false);

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

    name.setText(i.getString("Name"));
    img.setImageBitmap(getBitmapFromURL(i.getString("ImgUrl")));


     return v;

}

fragment_header_fragment.xml

<FrameLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:id="@+id/fragment"
tools:context="com.example.user.unchained.HeaderFragement">


<ImageView
    android:layout_width="40dp"
    android:layout_height="40dp"
    android:id="@+id/imageViewHeader"
    android:layout_marginLeft="10dp"
    android:layout_marginTop="12dp"
    android:layout_gravity="left|top"

     />

<TextView
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:textAppearance="?android:attr/textAppearanceMedium"
    android:text=""
    android:layout_marginLeft="60dp"
    android:layout_marginTop="10dp"
    android:id="@+id/nameH" />

<TextView
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:textAppearance="?android:attr/textAppearanceSmall"
    android:text="Currently @"
    android:layout_marginLeft="60dp"
    android:layout_marginTop="35dp"
    android:id="@+id/locationText"
    />

   </FrameLayout>

getBitmapFromURL功能

 public static Bitmap getBitmapFromURL(String src) {
    try {
        Log.e("src",src);
        URL url = new URL(src);
        HttpURLConnection connection = (HttpURLConnection) url.openConnection();
        connection.setDoInput(true);
        connection.connect();
        InputStream input = connection.getInputStream();
        Bitmap myBitmap = BitmapFactory.decodeStream(input);
        Log.e("Bitmap","returned");
        return myBitmap;
    } catch (IOException e) {
        e.printStackTrace();
        Log.e("Exception",e.getMessage());
        return null;
    }
}

0 个答案:

没有答案