如何在片段中为动态创建的对象充气?

时间:2014-10-06 16:11:56

标签: android android-fragments view dynamically-generated

我查看了大多数其他线程,但我无法弄清楚导致我的应用程序崩溃的原因。我一直试图“尝试在空引用对象上调用虚拟方法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>

2 个答案:

答案 0 :(得分:1)

getView返回在onCreateView中充气的View,然后返回null。在您的代码中,您有两个选项,第一个是使用ObjectView.findViewById来查找所需的视图。后者将覆盖onViewCreated,并使用第一个参数来查找您需要的视图,而第一个参数又是您在onCreateView中返回的视图

答案 1 :(得分:0)

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

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; 
    }

}