我正在使用具有MyAdapter的Fragment Activity进行Android项目:
public static class MyAdapter extends FragmentPagerAdapter {
public MyAdapter(FragmentManager fragmentManager) {
super(fragmentManager);
}
@Override
public int getCount() {
return ITEMS;
}
@Override
public Fragment getItem(int position) {
switch (position) {
case 0: // Fragment # 0 - Pokazuje listę
return ImageFragment.init(position);
//return TruitonListFragment.init(position);
case 1: // Fragment # 1 - Pokazuje wybór
return ImageFragment.init(position);
default:
return ImageFragment.init(position);
}
}
}
现在,我有了扩展Fragment的ImageFragment类。我正在尝试将ImageFragment内的addView()添加到布局中的元素上,并且出现了NullPointerException。它可以在Activity中使用,但在Fragment中不能使用。可以解决该问题,这到底是怎么回事?
public class ImageFragment extends Fragment implements OnClickListener {
int fragVal;
public static ImageFragment init(int val) {
ImageFragment truitonFrag = new ImageFragment();
// Supply val input as an argument.
Bundle args = new Bundle();
args.putInt("val", val);
truitonFrag.setArguments(args);
return truitonFrag;
}
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
InitializeDatabase(); // Wczytanie zasobów
fragVal = getArguments() != null ? getArguments().getInt("val") : 1;
//
AddonsList = new ArrayList<myAddons>();
//
Parent = new ArrayList<Long>();
Parent.add((long) 0);
GenerateNodeTree((long) 0); // Ustawienie fragmentu na początek
}
@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
View layoutView = inflater.inflate(R.layout.fragment_pager_list,
container, false);
linearLayout = new LinearLayout(getActivity());
linearLayout = (LinearLayout) layoutView.findViewById(R.id.Groups);
linearLayout = new LinearLayout(getActivity());
GoTop = (Button) layoutView.findViewById(R.id.Button02);
GoHome = (Button) layoutView.findViewById(R.id.Button01);
return layoutView;
}
private void GenerateNodeTree(Long TreeNode) {
try {
currentProductList = new ArrayList<Products>();
ClearView();
for(ProductGroups grupa : myGroupList) {
Log.i("mm",grupa.getNazwa());
LinearLayout row = new LinearLayout(getActivity());
row.setLayoutParams(new LayoutParams(LayoutParams.MATCH_PARENT, LayoutParams.WRAP_CONTENT));
if(grupa.getPARENTID() == TreeNode & grupa.getPOSPRODID() == 0) { // Przyciski kategorii
ImageButton imgTag = new ImageButton(getActivity());
Button btnTag = new Button(getActivity());
int k = R.drawable.icon_empt;
imgTag.setImageResource(k);
btnTag.setLayoutParams(new LayoutParams(LayoutParams.MATCH_PARENT, LayoutParams.MATCH_PARENT));
btnTag.setText(grupa.getNazwa());
btnTag.setId((int) grupa.getPOSPRODGRPID());
btnTag.setOnClickListener(this);
row.addView(imgTag);
row.addView(btnTag);
linearLayout.addView(row); // HERE NULL POINTER APPEARS
}
}
}
catch(Exception exc)
{
//Print.StandardToast(getActivity(), exc.getStackTrace().toString());
}
// Koniec generowania widoku
}
}
最佳答案
您的片段类中似乎没有一个linearLayout
字段(是否省略了代码?)。无论如何,您似乎在linearLayout
回调中初始化了onCreateView()
字段。问题是您试图在onCreate()
方法中(通过GenerateNodeTree()
调用)使用它,该方法在onCreateView()
之前被调用(因此linearLayout
那时不会被初始化)。因此,将GenerateNodeTree((long) 0);
调用移至onCreateView()
或onActivityCreated()
之后调用的onCreateView()
中。
关于java - fragment 内的addView()抛出NullPointerException,我们在Stack Overflow上找到一个类似的问题:https://stackoverflow.com/questions/17169077/