2014-10-06 35 views
1

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

回答

1

getView返回查看你onCreateView膨脹,befo重新它返回null。在你的代碼中你有兩個選擇,第一個是使用ObjectView.findViewById來查找你需要的視圖。後者將覆蓋onViewCreated,並使用第一個參數(反過來是您在onCreateView中返回的視圖)來查找所需的視圖

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

}