【问题标题】:How do you inflate dynamically created objects in a fragment?你如何在片段中膨胀动态创建的对象?
【发布时间】:2014-10-06 16:11:56
【问题描述】:

我查看了大多数其他线程,但我无法弄清楚导致我的应用程序崩溃的原因。我不断收到“尝试在空引用对象上调用虚拟方法 Android.view.View android.view.View.findViewById(int)。此错误发生在第 25 行或

"View ObjectView = inflater.inflate(R.layout.fragment_layout_biking,container,false); "

如何在 Android 中为新创建的对象扩展视图? 谢谢。

    public class Fragment1 extends Fragment {

        @Override
        public View onCreateView(LayoutInflater inflater,
                @Nullable ViewGroup container, @Nullable Bundle savedInstanceState) {
            // TODO Auto-generated method stub
            View ObjectView = inflater.inflate(R.layout.fragment_layout_biking,   container, false);
            final EditText input1 = (EditText)getView().findViewById(R.id.input);
            final ScrollView scroll = (ScrollView)getView().findViewById(R.id.scrolling);
            Button addbox = (Button)getView().findViewById(R.id.CheckBoxAdd);
            addbox.setOnClickListener(new OnClickListener(){

                @Override
                public void onClick(View arg0) {
                    for (int box = 0; box <25; box++){
                    CheckBox newcheckbox = (CheckBox)getView().findViewById(R.id.NewCheckBox);
                    newcheckbox.setText(input1.toString());
                    scroll.addView(newcheckbox);
                    }
                }
            });



            return ObjectView;
        }
}

这里是xml

 <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:id="@+id/linearlayout"
        android:orientation="vertical" >
       <EditText
        android:id="@+id/input"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content">
       </EditText>
       <Button
        android:id="@+id/CheckBoxAdd"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="Add Checkbox" />
       <ScrollView 
        android:id="@+id/scrolling"    
        android:layout_width="match_parent"
        android:layout_height="match_parent" >
       <LinearLayout
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:id="@+id/linearlayout2"
        android:orientation="vertical"> 
        </LinearLayout>

       </ScrollView>

    </LinearLayout>

【问题讨论】:

    标签: android android-fragments view dynamically-generated


    【解决方案1】:

    getView 在返回 null 之前返回您在 onCreateView 中膨胀的视图。在您的代码中,您有两个选项,第一个是使用ObjectView.findViewById,来查找您需要的视图。后者是覆盖onViewCreated,使用第一个参数,也就是你在onCreateView返回的视图,来寻找你需要的视图

    【讨论】:

      【解决方案2】:

      这是产生所需结果的最终代码。

      package fragments_for_app;
      import android.app.adventureprototype.R;
      import android.content.Context;
      import android.os.Bundle;
      import android.support.annotation.Nullable;
      import android.support.v4.app.Fragment;
      import android.text.Layout;
      import android.view.LayoutInflater;
      import android.view.View;
      import android.view.View.OnClickListener;
      import android.view.ViewGroup;
      import android.view.ViewGroup.LayoutParams;
      import android.widget.Button;
      import android.widget.CheckBox;
      import android.widget.EditText;
      import android.widget.LinearLayout;
      import android.widget.ScrollView;
      public class Fragment1 extends Fragment {
      
          LinearLayout layout;
          CheckBox newcheckbox;
      //18
          @Override
          public View onCreateView(LayoutInflater inflater,
                  @Nullable ViewGroup container, @Nullable Bundle savedInstanceState) {
      
              final View ObjectView = inflater.inflate(R.layout.fragment_layout_biking, container, false);
              final EditText input1 = (EditText)ObjectView.findViewById(R.id.input);
              layout = (LinearLayout)ObjectView.findViewById(R.id.linearlayout2);
                //final ScrollView scroll = (ScrollView)ObjectView.findViewById(R.id.scrolling);
              final Button addbox = (Button)ObjectView.findViewById(R.id.CheckBoxAdd);
              layout.removeAllViews();
              addbox.setOnClickListener(new OnClickListener(){
      
                  @Override
                  public void onClick(View arg0) {
      
      
                          newcheckbox = new CheckBox(ObjectView.getContext());
                          newcheckbox.setText(input1.getText().toString());
                          layout.addView(newcheckbox);    
      
      
                          //work on this thursday
      
      
                      }
              });
      
                return ObjectView; 
          }
      
      }
      

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 2016-01-08
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        相关资源
        最近更新 更多