【问题标题】:Button return null按钮返回空
【发布时间】:2017-02-17 18:11:19
【问题描述】:

我的 Android 项目出现错误。 我不能从我的片段中使用 View.findviewbyid(),id 是包含另一个我想调用它的 xml 女巫,但我发现一个错误并且我的按钮返回 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" />

而我的碎片化 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;
        }
        }

【问题讨论】:

    标签: android android-fragments nullpointerexception


    【解决方案1】:

    您在onCreateView() 中使用getView() 将返回null,因为尚未创建视图。

    改变

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

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

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 2011-12-08
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2018-01-18
      • 2016-09-08
      相关资源
      最近更新 更多