gpt4 book ai didi

java - 布局充气器 nullPointerException

转载 作者:行者123 更新时间:2023-12-01 22:15:05 25 4
gpt4 key购买 nike

我遇到运行时错误。收到此错误消息:

java.lang.NullPointerException: Attempt to invoke virtual method 'android.view.View android.view.LayoutInflater.inflate(int, android.view.ViewGroup, boolean)' on a null object reference

工作室指向这一行:

View view= lI.inflate(R.layout.fragment_frag3,container,false);

public class Frag3 extends Fragment {

ViewPager viewPager;
CardAdapter2 adapter2;
List<String> cardWords;
LayoutInflater lI;

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

View view= lI.inflate(R.layout.fragment_frag3,container,false);
viewPager= (ViewPager)view.findViewById(R.id.vp);
viewPager.setAdapter(adapter2);
viewPager.addOnPageChangeListener(new ViewPager.OnPageChangeListener() {
@Override
public void onPageScrolled(int position, float positionOffset, int positionOffsetPixels) {

}

@Override
public void onPageSelected(int position) {

}

@Override
public void onPageScrollStateChanged(int state) {

}
});
cardWords= new ArrayList<>();
cardWords.add("x");
cardWords.add("y");
cardWords.add("z");

adapter2= new CardAdapter2(getActivity(),cardWords);

// Inflate the layout for this fragment
return view;
}


}

适配器:

public class CardAdapter2 extends PagerAdapter {
private Context mContext;
private List<String> cardWords;
private LayoutInflater layoutInflater;

public CardAdapter2(Context mContext, List<String> cardWords) {
this.mContext = mContext;
this.cardWords = cardWords;
}

@Override
public int getCount() {
return cardWords.size();
}

@NonNull
@Override
public Object instantiateItem(@NonNull ViewGroup container, int position) {
layoutInflater= LayoutInflater.from(mContext);
View view= layoutInflater.inflate(R.layout.card_design2,container,false);

TextView title= view.findViewById(R.id.textView2);
title.setText(cardWords.get(position));
container.addView(view,0);

return super.instantiateItem(container, position);
}

@Override
public void destroyItem(@NonNull ViewGroup container, int position, @NonNull Object object) {
container.removeView((View)object);
}

@Override
public boolean isViewFromObject(@NonNull View view, @NonNull Object object) {
return view.equals(object);
}


}

最佳答案

你定义一个类变量LayoutInflater

LayoutInflater lI;

在 Frag3 类的顶部。然后在 onCreateView 方法中,对未初始化的充气器调用充气。您已经传入了布局充气器,因此您需要进行的更改是

View view= inflater.inflate(R.layout.fragment_frag3,container,false);

只需对传递给您的 LI 调用 inflate

关于java - 布局充气器 nullPointerException,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/58633645/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com