【发布时间】: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