2017-02-17 131 views
0

我在我的Android項目中遇到錯誤。 我無法從我的片段使用View.findviewbyid(),id爲包括另一個XML巫婆我想打電話,但我發現一個錯誤,我的按鈕返回NULL按鈕返回null

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

女巫我使用的是包括這是我的主要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> 

和ui_frag_usb_led1.xml是:

  <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" /> 

和我fragmant Java是:

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

回答

1

您在使用onCreateView()其中getView()將返回null作爲視圖尚未尚未創建。

變化

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

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

坦克你,它的工作。 – ihsoma