2016-09-02 2 views
-1

フラグメントに描画可能なフォルダからの画像を表示する際に問題があります。 これを表示しようとすると、java.lang.NullPointerExceptionが表示されます。 ご協力いただければ幸いです。 Androidのフラグメントで画像を表示する

この

は、あなたがXMLで画像にid属性を追加していないので、それは私のコード、

public class Fragment4 extends Fragment { 
    public Fragment4() { 
    } 

    public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) { 
     View rootView = inflater.inflate (R.layout.fragment4, container, false); 
     ImageView image = (ImageView)rootView.findViewById(R.id.image); 
     image.setImageResource(R.drawable.image123); 
     return rootView; 
    } 
} 

XMLファイル

<?xml version="1.0" encoding="utf-8"?> 
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" 
android:orientation="vertical" android:layout_width="match_parent" 
android:layout_height="match_parent" > 

<ImageView 
    android:layout_width="wrap_content" 
    android:layout_height="wrap_content" /> 

</LinearLayout> 
+0

さらに、レイアウトxmlファイルを追加できますか? –

+0

参照されるNullPointerExceptionの行番号は何ですか? (そしてその番号が参照する行はどれですか) –

+0

NullPointerExceptionはこのコード行 "image.setImageResource(R.drawable.image123);"を参照しています。 – Andy

答えて

3

ImageViewにはidがありません。次のように設定します。

<ImageView 
    android:id="@+id/image" 
    android:layout_width="wrap_content" 
    android:layout_height="wrap_content" /> 
2

ある これを試してみてください:

<ImageView 
android:id="@+id/image" 
android:layout_width="wrap_content" 
android:layout_height="wrap_content" /> 
関連する問題