无法向ViewGroup添加视图

时间:2017-09-14 10:02:34

标签: java android android-fragments

我在活动

中将片段添加到groupview时遇到问题
09-14 09:49:43.364 5118-5118/com.example.dhajczuk_adm.randomchat E/AndroidRuntime: FATAL EXCEPTION: main
   Process: com.example.dhajczuk_adm.randomchat, PID: 5118
   java.lang.IllegalArgumentException: Cannot add a null child view to a ViewGroup
       at android.view.ViewGroup.addView(ViewGroup.java:4647)
       at android.view.ViewGroup.addView(ViewGroup.java:4629)
       at com.example.dhajczuk_adm.randomchat.MainActivity$1.instantiateItem(MainActivity.java:156)
       at android.support.v4.view.ViewPager.addNewItem(ViewPager.java:1005)
       at android.support.v4.view.ViewPager.populate(ViewPager.java:1153)
       at android.support.v4.view.ViewPager.populate(ViewPager.java:1087)
       at android.support.v4.view.ViewPager.onMeasure(ViewPager.java:1613)
       at android.view.View.measure(View.java:21783)
       at android.view.ViewGroup.measureChildWithMargins(ViewGroup.java:6549)
       at android.widget.LinearLayout.measureChildBeforeLayout(LinearLayout.java:1514)
       at android.widget.LinearLayout.measureHorizontal(LinearLayout.java:1167)
       at android.widget.LinearLayout.onMeasure(LinearLayout.java:687)
       at android.view.View.measure(View.java:21783)
       at android.support.constraint.ConstraintLayout.internalMeasureChildren(ConstraintLayout.java:934)
       at android.support.constraint.ConstraintLayout.onMeasure(ConstraintLayout.java:973)
       at android.view.View.measure(View.java:21783)
       at android.view.ViewGroup.measureChildWithMargins(ViewGroup.java:6549)
       at android.widget.FrameLayout.onMeasure(FrameLayout.java:185)
       at android.support.v7.widget.ContentFrameLayout.onMeasure(ContentFrameLayout.java:139)
       at android.view.View.measure(View.java:21783)
       at android.view.ViewGroup.measureChildWithMargins(ViewGroup.java:6549)
       at android.widget.LinearLayout.measureChildBeforeLayout(LinearLayout.java:1514)
       at android.widget.LinearLayout.measureVertical(LinearLayout.java:806)
       at android.widget.LinearLayout.onMeasure(LinearLayout.java:685)
       at android.view.View.measure(View.java:21783)
       at android.view.ViewGroup.measureChildWithMargins(ViewGroup.java:6549)
       at android.widget.FrameLayout.onMeasure(FrameLayout.java:185)
       at android.view.View.measure(View.java:21783)
       at android.view.ViewGroup.measureChildWithMargins(ViewGroup.java:6549)
       at android.widget.LinearLayout.measureChildBeforeLayout(LinearLayout.java:1514)
       at android.widget.LinearLayout.measureVertical(LinearLayout.java:806)
       at android.widget.LinearLayout.onMeasure(LinearLayout.java:685)
       at android.view.View.measure(View.java:21783)
       at android.view.ViewGroup.measureChildWithMargins(ViewGroup.java:6549)
       at android.widget.FrameLayout.onMeasure(FrameLayout.java:185)
       at com.android.internal.policy.DecorView.onMeasure(DecorView.java:699)
       at android.view.View.measure(View.java:21783)
       at android.view.ViewRootImpl.performMeasure(ViewRootImpl.java:2409)
       at android.view.ViewRootImpl.measureHierarchy(ViewRootImpl.java:1497)
       at android.view.ViewRootImpl.performTraversals(ViewRootImpl.java:1750)
       at android.view.ViewRootImpl.doTraversal(ViewRootImpl.java:1385)
       at android.view.ViewRootImpl$TraversalRunnable.run(ViewRootImpl.java:6722)
       at android.view.Choreographer$CallbackRecord.run(Choreographer.java:886)
       at android.view.Choreographer.doCallbacks(Choreographer.java:698)
       at android.view.Choreographer.doFrame(Choreographer.java:633)
       at android.view.Choreographer$FrameDisplayEventReceiver.run(Choreographer.java:872)
       at android.os.Handler.handleCallback(Handler.java:769)
       at android.os.Handler.dispatchMessage(Handler.java:98)
       at android.os.Looper.loop(Looper.java:164)
       at android.app.ActivityThread.main(ActivityThread.java:6540)
       at java.lang.reflect.Method.invoke(Native Method)
       at com.android.internal.os.Zygote$MethodAndArgsCaller.run(Zygote.java:240)
       at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:767)

有问题的部分代码

public class UsersFragment extends Fragment {
public View onCreateView(LayoutInflater inflater, ViewGroup container,
                         Bundle savedInstanceState) {
    View view = LayoutInflater.from(getContext()).inflate(R.layout.item_vp, null, false);
    return view;

public class MainActivity extends AppCompatActivity {

private FirebaseAuth mAuth;
private FirebaseUser mCurrentUser;
private DatabaseReference mUserDatabase;
private Button mLogOut;
private int GALLERY_PICK;
private CircleImageView mDisplayImage;
private TextView mName;
private TextView mStatus;
private RecyclerView mUsersList;
private DatabaseReference mUsersDatabase;


@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    mAuth = FirebaseAuth.getInstance();
    setContentView(R.layout.activity_main);
    init();
}
@Override
public void onStart() {
    super.onStart();
    mCurrentUser = mAuth.getCurrentUser();
    if(mCurrentUser ==null){
        Intent startIntent = new Intent(MainActivity.this, StartActivity.class);
        startActivity(startIntent);
        finish();
    }
}

public void init(){

    final ViewPager viewPager = (ViewPager) findViewById(R.id.vp_vertical_ntb);
    viewPager.setAdapter(new PagerAdapter() {
        @Override
        public int getCount() {
            return 8;
        }

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

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

        @Override
        public Object instantiateItem(final ViewGroup container, final int position) {

            else if(position==3){
                final View view = LayoutInflater.from(getBaseContext()).inflate(R.layout.item_vp, null, false);
                return view;
            }

            else if(position==4){
                final View view = new UsersFragment().getView();
                container.addView(view);
                return view;}}
    });

对于位置3(导航栏),一切都很好。当我想用新的fragment类和new object.getView做同样的事情时,我有一个View子组错误的null子视图。有什么问题?

2 个答案:

答案 0 :(得分:0)

您只是创建了一个fragment对象,因此,它的getView()方法返回null,导致异常。

if(position==4){
  final View view = new UsersFragment().getView();
  container.addView(view);
  return view;
}

new UsersFragment() - 它只是创建一个UserFragment对象。它不会使视图层次结构膨胀,因此其getView()方法返回null。

答案 1 :(得分:0)

 final View view = new UsersFragment().getView();
            container.addView(view);
            return view;}}

getView()始终为null,因为在此处调用new UsersFragment()时未显示视图。 使用view = Layoutinflater ....而不是。

对于位置3(导航栏),一切都很好。当我想用新的fragment类和new object.getView做同样的事情时,我有一个View子组错误的null子视图。怎么了? =>这里不使用getBaseContext()。请删除它。