在Android中的片段中设置图像时出现NullPointerException

时间:2017-02-20 13:59:02

标签: android android-fragments nullpointerexception

我试图在片段中的ImageView内设置一个drawable图像。上面的代码会在行

处抛出NullPointerException
  

imageView.setImageResource(R.drawable.featured_one);

public class FeaturedTwo extends Fragment {

    ImageView imageView;

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

        View rootView = inflater.inflate(R.layout.featured_one, container, false);

        imageView = (ImageView) rootView.findViewById(R.id.featured_two_image);

        imageView.setImageResource(R.drawable.featured_one);

        return rootView;

    }
}

请帮我找一个解决方案。

java.lang.NullPointerException: Attempt to invoke virtual method 'void android.widget.ImageView.setImageResource(int)' on a null object reference
                                                                                   at com.example.anandzoom.appytizertest.FeaturedTwo.onCreateView(FeaturedTwo.java:28)
                                                                                   at android.support.v4.app.Fragment.performCreateView(Fragment.java:2189)
                                                                                   at android.support.v4.app.FragmentManagerImpl.moveToState(FragmentManager.java:1299)
                                                                                   at android.support.v4.app.FragmentManagerImpl.moveFragmentToExpectedState(FragmentManager.java:1528)
                                                                                   at android.support.v4.app.FragmentManagerImpl.moveToState(FragmentManager.java:1595)

featured_one的XML,

<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="match_parent"
    android:layout_height="match_parent">

    <ImageView
        android:id="@+id/featured_one_image"
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:scaleType="centerCrop" />

</LinearLayout>

5 个答案:

答案 0 :(得分:2)

使用rootView.findViewById代替getActivity,因为您的布局位于片段内而不是活动内。

你的id也错了,你的xml被命名为“featured_one_image”,但你得到的是“featured_two_image”。

所以改变你的代码 imageView = (ImageView) rootView.findViewById(R.id.featured_two_image);imageView = (ImageView) rootView.findViewById(R.id.featured_one_image);

。{

答案 1 :(得分:2)

嘿,您需要在片段内找到您的视图,而不是在父活动中。

只需替换

imageView = (ImageView) getActivity().findViewById(R.id.featured_one_image);

imageView = (ImageView) rootView.findViewById(R.id.featured_two_image);

答案 2 :(得分:1)

在Fragment中使用视图的上下文而不是getActivity()

编辑还要更正您的imageview ID使用featured_one_image而不是featured_one_image。

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

    View rootView = inflater.inflate(R.layout.featured_one, container, false);
    imageView = (ImageView) rootView.findViewById(R.id.featured_one_image);

    // set drawable image like it
    imageView.setImageDrawable(ContextCompat.getDrawable(getActivity(), R.drawable.featured_one));


    return rootView;

}

答案 3 :(得分:0)

您需要从rootView中找到ImageView。

呼叫

imageView = (ImageView) rootView.findViewById(R.id.featured_one_image);

编辑:rootView中的ImageView是 featured_one_image ,而不是 featured_two_image

答案 4 :(得分:0)

而不是:

imageView = (ImageView) getActivity().findViewById(R.id.featured_two_image);

使用它:

imageView = (ImageView) rootView.findViewById(R.id.featured_two_image);