empt调用虚方法' void android.widget.button.setonclicklistener

时间:2017-01-03 05:28:22

标签: android android-fragments invoke

嗨,我是Android工作室的新手,但我似乎无法解决这个错误。你们介意帮忙吗?

Process: com.sp.ez_mart_xy, PID: 24157
java.lang.NullPointerException: Attempt to invoke virtual method 'android.view.View android.view.View.findViewById(int)' on a null object reference at com.sp.ez_mart_xy.OneFragment.onCreateView(OneFragment.java:39)

代码

 @Override
        public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle   savedInstanceState) {
            View view = inflater.inflate(R.layout.fragment_two, container, false);
            super.onCreate(savedInstanceState);
            scan_btn = (Button) view.findViewById(scan_button);
            final Activity activity = getActivity();
            scan_btn.setOnClickListener(new View.OnClickListener() {
                @Override
                public void onClick(View view) {
                    IntentIntegrator integrator = new IntentIntegrator(activity);
                    integrator.setDesiredBarcodeFormats(IntentIntegrator.QR_CODE_TYPES);
                    integrator.setPrompt("Scan");
                    integrator.setCameraId(0);
                    integrator.setBeepEnabled(false);
                    integrator.setBarcodeImageEnabled(false);
                    integrator.initiateScan();
                }
            });
            return view

问题似乎是

scan_btn = button

我尝试将其更改为getview和root视图但它仍然失败。一世 还尝试了stackoverflow的解决方案,但仍然崩溃。 这是xml文件:

<LinearLayout 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"
    android:weightSum="1"
    android:orientation="vertical"
    android:background="@color/zxing_custom_possible_result_points">

    <LinearLayout
        android:orientation="vertical"
        android:layout_width="match_parent"
        android:layout_height="24dp"></LinearLayout>

    <TextView
        android:text="Current Location :"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:id="@+id/textView"
        android:textAppearance="@style/TextAppearance.AppCompat.Body1"
        android:fontFamily="cursive"
        android:textSize="30sp"
        android:textAlignment="viewStart"
        android:typeface="serif"
        android:textStyle="normal|bold"
        android:textAllCaps="false" />

    <EditText
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:inputType="textPersonName"
        android:ems="10"
        android:id="@+id/Edit_current"
        android:selectAllOnFocus="true"
        tools:backgroundTint="@color/common_action_bar_splitter"
        android:background="@color/common_action_bar_splitter"
        android:layout_weight="0.21"
        android:hint="Scan/Type Location"
        android:textStyle="normal|italic"
        tools:background="@color/zxing_possible_result_points" />

    <LinearLayout
        android:orientation="horizontal"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:layout_weight="0.14"></LinearLayout>

    <LinearLayout
        android:orientation="vertical"
        android:layout_width="match_parent"
        android:layout_height="115dp">

        <Button
            android:text="Scan Nearest Qr Code"
            android:layout_width="match_parent"
            android:layout_height="match_parent"
            android:id="@+id/scan_btn"
            android:background="@color/zxing_possible_result_points"
            android:elevation="0dp"
            android:typeface="monospace"
            android:textSize="18sp"
            android:visibility="visible" />

    </LinearLayout>

    <LinearLayout
        android:orientation="vertical"
        android:layout_width="match_parent"
        android:layout_height="224dp"
        android:elevation="1dp"
        android:weightSum="1">

        <LinearLayout
            android:orientation="vertical"
            android:layout_width="match_parent"
            android:layout_height="35dp"></LinearLayout>

        <TextView
            android:text="Destination :"
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:id="@+id/Destination"
            android:textAppearance="@style/TextAppearance.AppCompat.Body1"
            android:fontFamily="cursive"
            android:textSize="30sp"
            android:textStyle="normal|bold" />

        <EditText
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:inputType="textPersonName"
            android:ems="10"
            android:id="@+id/edit_destination"
            android:hint="Example:Fish"
            android:singleLine="true"
            android:backgroundTint="@color/common_action_bar_splitter"
            android:background="@color/zxing_possible_result_points"
            android:layout_weight="0.25"
            style="@style/Widget.AppCompat.AutoCompleteTextView"
            android:selectAllOnFocus="false"
            android:textStyle="normal|italic"
            android:textAllCaps="false" />

    </LinearLayout>

</LinearLayout>

1 个答案:

答案 0 :(得分:0)

应该是

 scan_btn = (Button) view.findViewById(R.id.scan_button);

而不是scan_btn = (Button) view.findViewById(scan_button);

并删除此行super.onCreate(savedInstanceState);。我不认为这是必要的。