我看過大部分其他線程,但我無法弄清楚是什麼導致我的應用程序崩潰。我不斷收到「試圖將一個空引用對象上調用虛方法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>