2017-02-17 4 views
0

Ich habe einen Fehler in meinem Android-Projekt. ich kann nicht View.findviewbyid() von meinem Fragment verwenden, ist die ID eine andere XML-Hexe sind es ich nennen möchte, aber ich finde, einen Fehler und meine Taste return nullSchaltfläche zurück null

  Attempt to invoke virtual method 'android.view.View android.view.View.findViewById(int)' on a null object reference 

Hexe i eine Include bin mit Das ist mein Haupt-XML.

 <FrameLayout xmlns:android="http://schemas.android.com/apk/res/android" 
     xmlns:tools="http://schemas.android.com/tools" 
     android:layout_width="match_parent" 
     android:layout_height="match_parent" 
    tools:context="user.ens.project.controleurarduino.ui.fragments.UsbLed"> 
     <include 
     layout="@layout/ui_frag_usb_led1" 
     android:id="@+id/usbled1" 
     /> 
     <include layout="@layout/ui_frag_usb_led2"/> 
     <include layout="@layout/ui_frag_usb_led3"/> 

     </FrameLayout> 

und die ui_frag_usb_led1.xml ist:

  <LinearLayout 
     android:layout_width="match_parent" 
     android:layout_height="wrap_content" 
     android:background="#80CBC4" 
     android:orientation="vertical" 
     android:layout_marginBottom="20dp"> 

     <TextView 
      android:layout_width="match_parent" 
      android:layout_height="match_parent" 
      android:text="@string/cab" 
      android:textSize="24sp" 
      tools:textStyle="bold" 
      android:textColor="@android:color/background_light" 
      android:layout_marginLeft="90dp" 
      android:layout_marginRight="90dp" 
      android:textAlignment="center" 
      android:textStyle="normal|bold" /> 


     <ImageView 
      android:layout_height="wrap_content" 
      android:layout_width="wrap_content" 
      android:src="@drawable/musb" 
      android:layout_weight="0.01" /> 

     <TextView 
      android:layout_width="match_parent" 
      android:layout_height="match_parent" 
      android:text="Numéro de broche" 
      android:textSize="15sp" 
      android:textColor="#FFD3EFEC" 
      android:layout_marginLeft="50dp" 
      android:layout_marginRight="50dp" 
      android:textAlignment="center" 
      android:layout_marginBottom="10dp" /> 

     <EditText 
      android:layout_width="80dp" 
      android:layout_height="wrap_content" 
      android:inputType="numberSigned" 
      android:ems="10" 
      android:id="@+id/pin1" 
      android:textAlignment="center" 
      android:layout_marginTop="5dp" 
      android:layout_marginBottom="10dp" 
      android:layout_marginLeft="20dp" 
      android:layout_marginRight="20dp" 
      android:textColor="?attr/colorAccent" /> 
     </LinearLayout> 

     <Button 
     android:layout_width="wrap_content" 
     android:layout_height="wrap_content" 
     android:id="@+id/btn1" 
     android:text="Accepter" 
     android:textAlignment="center" 
     android:layout_marginLeft="150dp" 
     android:layout_marginRight="70dp" /> 

und mein fragmant Java ist:

public class UsbLed extends Fragment { 

    public Button btn1; 
     public EditText pin1; 
    Intent intentA; 
     int val; 
     int pos=0; 
     int total; 
     @Override 
     public View onCreateView(LayoutInflater inflater, ViewGroup container, 
         Bundle savedInstanceState) { 

     View rootView; 
     pos=FirstView.spiPosition(); 
     switch (pos) { 
     case 1: { 
      rootView = inflater.inflate(R.layout.ui_frag_usb_led1,container, false); 
      break; 

     } 
     case 2: { 
      rootView = inflater.inflate(R.layout.ui_frag_usb_led2, container, false); 
      break; 

     } 



     default: 
      rootView = inflater.inflate(R.layout.ui_frag_usb_led1, container, false); 
    } 


    View usbView1 = rootView.findViewById(R.id.usbled1); 

     btn1 = (Button) getView().findViewById(R.id.btn1); 


     return rootView; 
     } 
     } 

Antwort

1

Du getView() in onCreateView() verwendet, die null zurück, als die Ansicht nicht gewesen sein erstellt noch.

ändern

btn1 = (Button) getView().findViewById(R.id.btn1); 

zu

btn1 = (Button) rootView.findViewById(R.id.btn1); 
+0

Behälter Sie, arbeitet es. – ihsoma