使用 ViewPager 的 Layout Inflater 抛出 NullPointerException

Posted

技术标签:

【中文标题】使用 ViewPager 的 Layout Inflater 抛出 NullPointerException【英文标题】:Layout Inflater using ViewPager throws NullPointerException 【发布时间】:2016-06-13 11:53:18 【问题描述】:

我正在开发一个基于测验的应用程序。每个问题有 4 个选项。问题是一个文本视图,而对于选项我使用的是 viewpager,以便用户可以滑动将从服务器动态加载的 4 个选项。 我收到了 NullPointerException 的 Layout inflater。 这是我得到空值的行:

LayoutInflater inflater = (LayoutInflater)  
context.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
View itemView = inflater.inflate(R.layout.viewpager_item, container, false);
class MyAdapter extends PagerAdapter  
    int NumberOfPages = 4;
    Context context;
    ArrayList<String> option1List; .....

我们非常感谢您的帮助和指导。

Error :

E/androidRuntime: FATAL EXCEPTION: main java.lang.NullPointerException 在 quizapp.android.mohit.com.quizapp.MyAdapter.instantiateItem(Questions.java:485)

这是我的 Mainactivity 代码:

  public class Questions extends ActionBarActivity 
  protected void onCreate(Bundle savedInstanceState) 
  super.onCreate(savedInstanceState);
  setContentView(R.layout.activity_questions);
  dbHelper = new DataBaseHelper(getApplicationContext());

viewPager = (ViewPager) findViewById(R.id.viewPager);
adapter = new MyAdapter(option1List, option2List, option3List,option4List);
viewPager.setAdapter(adapter);

class MyAdapter extends PagerAdapter 
    int NumberOfPages = 4;
    Context context;
    LayoutInflater inflater;
    View itemView;
    ArrayList<String> option1List;
    ArrayList<String> option2List;
    ArrayList<String> option3List;
    ArrayList<String> option4List;

    public MyAdapter(ArrayList<String> option1List, ArrayList<String> option2List, ArrayList<String> option3List, ArrayList<String> option4List) 
        this.option1List = option1List;
        this.option2List = option2List;
        this.option3List = option3List;
        this.option4List = option4List;
    
    public int getCount()
        return NumberOfPages;
    

    public boolean isViewFromObject(View container, Object obj) 
        return container == obj;
        // return container == (View) obj;
    

    public Object instantiateItem(ViewGroup container, int position) 
        LayoutInflater inflater = (LayoutInflater) context.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
        View itemView = inflater.inflate(R.layout.viewpager_item, container, false);
        // Declare Variables
        TextView option1, option2, option3, option4;
        // Locate the TextViews in viewpager_item.xml
        option1 = (TextView) itemView.findViewById(R.id.tv_option1);
        option2 = (TextView) itemView.findViewById(R.id.tv_option2);
        option3 = (TextView) itemView.findViewById(R.id.tv_option3);
        option4 = (TextView) itemView.findViewById(R.id.tv_option4);
        // Capture position and set to the TextViews
        option1.setText(option1List.get(0));
        option2.setText(option2List.get(0));
        option3.setText(option3List.get(0));
        option4.setText(option4List.get(0));
        // Add viewpager_item.xml to ViewPager
        container.addView(itemView);
        return itemView;
    

    public void destroyItem(ViewGroup container, int position, Object obj) 
       container.removeView((LinearLayout)obj);
    

【问题讨论】:

***.com/a/20412475/1527458 错误发生在哪一行,你在哪里声明字段“上下文”? @barq :这是我得到空值的行:LayoutInflater inflater = (LayoutInflater) context.getSystemService(Context.LAYOUT_INFLATER_SERVICE);查看 itemView = inflater.inflate(R.layout.viewpager_item, container, false);类 MyAdapter 扩展 PagerAdapter int NumberOfPages = 4;上下文上下文; ArrayList option1List; ..... 您需要将 getSupportFragmentManager() 传递给您的视图寻呼机适配器 @Mina Fawzy :我没有使用片段。我正在使用 PagerAdapter 实现 Viewpager,而不是使用片段。 【参考方案1】:

您应该尝试以另一种方式膨胀视图,例如使用 LayoutInflater。您还应该停止使用应用程序上下文,而应该使用您的活动作为上下文。

代码:-

View v = LayoutInflater.from(context).inflate(R.layout.viewpager_item, container, false);

【讨论】:

【参考方案2】:

您创建 Adapter 对象并在其中传递四个 ArrayList 但在您的适配器中您没有定义您的构造函数.....请定义它。参见下面的代码

试试这个.....

public class MyAdapter extends PagerAdapter 
    int NumberOfPages = 4;
    LayoutInflater inflater;

public MyAdapter(ArrayList<type you define> option1List, ArrayList<type you define>  option2List, ArrayList<type you define>  option3List,ArrayList<type you define> option4List) 


    public int getCount()
        return NumberOfPages;
    

    public boolean isViewFromObject(View container, Object obj) 
        return container == obj;
        // return container == (View) obj;
    

    public Object instantiateItem(ViewGroup container, int position) 
        inflater = (LayoutInflater) context.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
        View itemView = inflater.inflate(R.layout.viewpager_item, container, false);
        // Declare Variables
        TextView option1, option2, option3, option4;
        // Locate the TextViews in viewpager_item.xml
        option1 = (TextView) itemView.findViewById(R.id.tv_option1);
        option2 = (TextView) itemView.findViewById(R.id.tv_option2);
        option3 = (TextView) itemView.findViewById(R.id.tv_option3);
        option4 = (TextView) itemView.findViewById(R.id.tv_option4);
        // Capture position and set to the TextViews
        option1.setText(option1List.get(0));
        option2.setText(option2List.get(0));
        option3.setText(option3List.get(0));
        option4.setText(option4List.get(0));
        // Add viewpager_item.xml to ViewPager
        container.addView(itemView);
        return itemView;
    

    public void destroyItem(ViewGroup container, int position, Object obj) 
       container.removeView((LinearLayout)obj);
    

【讨论】:

【参考方案3】:

创建一个MyAdapter 构造函数,其中Context 作为其参数之一,例如

private Context context;
private ArrayList<String>arrayList;
public MyAdapter(ArrayList<String>, Context context)
this.context = context;
this.arrayList = arrayList;

在你的活动中,只需通过传入数组列表和上下文来初始化适配器

myAdapter = new MyAdapter(myList, getApplicationContext());

【讨论】:

类 MyAdapter 扩展 PagerAdapter int NumberOfPages = 4;上下文上下文; ArrayList option1List; ArrayList option2List; ArrayList option3List; ArrayList option4List;公共 MyAdapter(上下文上下文,ArrayList option1List,ArrayList option2List,ArrayList option3List,ArrayList option4List) this.context = context; this.option1List = option1List; this.option2List = option2List;等等

以上是关于使用 ViewPager 的 Layout Inflater 抛出 NullPointerException的主要内容,如果未能解决你的问题,请参考以下文章

Android - ViewPager

ViewPager 中的膨胀布局

滚动条 viewPager

Android:使用ViewPager实现左右滑动切换图片加点点

TabHost+ViewPager+Fragment实现页面的滑动,android开发工具deve

ViewPager实现页面切换