2017-01-03 6 views
1

Hallo, ich bin ein Novize zu Android Studio, aber ich kann nicht scheinen, diesen Fehler zu lösen. Habt ihr etwas dagegen?empt zum Aufruf der virtuellen Methode 'void android.widget.button.setonclicklistener

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) 

-Code

@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 

Das Problem scheint die

scan_btn = button 

ich ändern es im Hinblick auf GetView und Wurzel versucht, aber es immer noch nicht. I versuchte auch Lösungen aus stackoverflow aber stürzt trotzdem ab. Hier ist die XML-Datei:

<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> 
+0

Mögliches Duplikat von [Was ist eine NullPointerException, und wie behebe ich sie?] (Http://stackoverflow.com/questions/218384/what-is-a-nullpointerexception-and-how-do-i-fix -it) –

+0

Entfernen Sie 'super.onCreate (savedInstanceState);' –

+0

scan_btn = (Schaltfläche) view.findViewById (R.id.scan_button); das wird dein Problem lösen –

Antwort

0

sein sollte

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

statt scan_btn = (Button) view.findViewById(scan_button);

und entfernen Sie diese Zeile super.onCreate(savedInstanceState);. Ich glaube nicht, dass es nötig ist.

Verwandte Themen