在ViewPager的特定页面中更改ImageView源代码Android

问题描述:

我想要在查看传呼机中的特定页面中更改imageresource。这就是我所做的。在ViewPager的特定页面中更改ImageView源代码Android

这是活动的onCreate ...

 ((PlaceholderFragment)mSectionsPagerAdapter.getItem(0)).updateImage(R.drawable.help1); 
     ((PlaceholderFragment)mSectionsPagerAdapter.getItem(1)).updateImage(R.drawable.help2); 
     ((PlaceholderFragment)mSectionsPagerAdapter.getItem(2)).updateImage(R.drawable.help3); 
     ((PlaceholderFragment)mSectionsPagerAdapter.getItem(3)).updateImage(R.drawable.help4); 

这是片段类...

public static class PlaceholderFragment extends Fragment { 
     /** 
     * The fragment argument representing the section number for this 
     * fragment. 
     */ 
     private static final String ARG_SECTION_NUMBER = "section_number"; 

    /** 
    * Returns a new instance of this fragment for the given section 
    * number. 
    */ 
    public static PlaceholderFragment newInstance(int sectionNumber) { 
     PlaceholderFragment fragment = new PlaceholderFragment(); 
     Bundle args = new Bundle(); 
     args.putInt(ARG_SECTION_NUMBER, sectionNumber); 
     fragment.setArguments(args); 
     return fragment; 
    } 

    public PlaceholderFragment() { 
    } 

    @Override 
    public View onCreateView(LayoutInflater inflater, ViewGroup container, 
          Bundle savedInstanceState) { 
     View rootView = inflater.inflate(R.layout.fragment_help, container, false); 
     ImageView imageView = (ImageView) rootView.findViewById(R.id.imageHelp); 
     imageView.setImageResource(R.drawable.help4); 
     return rootView; 
    } 

    public void updateImage(int image){ 
     ImageView imageView = (ImageView) getView().findViewById(R.id.imageHelp); 
     imageView.setImageResource(image); 

    } 
} 

但不知何故,它仍然给我错误空指针异常。我怎么错过? 以前感谢,请帮助我:)我在这里停留了大约2个小时。

+0

显示您的logcat以及显示空指针异常的哪一行。 – duggu 2015-01-21 13:08:10

+0

引起︰java.lang.NullPointerException︰尝试调用空对象引用虚拟方法'android.view.View android.view.View.findViewById(int)' 原因由行ImageView imageView =(ImageView)rootView。 .. – user2565280 2015-01-22 00:52:42

// declare rootView as global variable 

public void updateImage(int image){ 
    ImageView imageView = (ImageView) rootView.findViewById(R.id.imageHelp); 
    imageView.setImageResource(rootView); 
} 
+0

我这样做,使rootView全局,但不知何故它仍然给我的错误引起:java.lang.NullPointerException:尝试调用虚拟方法'android.view.View android.view.View.findViewById(int)'on空对象引用 – user2565280 2015-01-22 00:54:11

+0

请提供一些更多的澄清代码 – koutuk 2015-01-22 05:35:42