2017-08-04 95 views
1

在這個片段中,我添加了ZXingscanner視圖,並且代碼完全沒有顯示任何錯誤,但是當我運行應用程序時,它崩潰並且每次都停止。此應用程序有3個選項卡,並且其中一個選項卡的片段是此掃描器,但由於某種原因,此視圖會將應用程序廢棄。這是我的片段類的代碼:Android工作室中的LogCat錯誤,在運行時崩潰的應用程序

import android.app.AlertDialog; 
import android.content.DialogInterface; 
import android.os.Bundle; 
import android.os.Vibrator; 
import android.support.annotation.NonNull; 
import android.support.v4.app.Fragment; 
import android.util.Log; 
import android.view.LayoutInflater; 
import android.view.View; 
import android.view.ViewGroup; 
import android.widget.LinearLayout; 

import com.google.zxing.BarcodeFormat; 
import com.google.zxing.Result; 
import com.hist_area.imeda.histarea.R; 

import java.util.ArrayList; 
import java.util.List; 

import me.dm7.barcodescanner.zxing.ZXingScannerView; 


public class EmptyFragment extends Fragment implements ZXingScannerView.ResultHandler { 
    public static EmptyFragment create() { 
     return new EmptyFragment(); 
    } 
    Vibrator vibrator; 


    @Override 
    public void onRequestPermissionsResult(int requestCode, @NonNull String[] permissions, @NonNull int[] grantResults) { 
     super.onRequestPermissionsResult(requestCode, permissions, grantResults); 
     } 

     private ZXingScannerView mScannerView; 
     private LinearLayout qrCameraLayout; 

     @Override 
     public View onCreateView (LayoutInflater inflater, ViewGroup container, Bundle 
     savedInstanceState) 
     { 
      View v = inflater.inflate(R.layout.fragment_camera, container, false); 

      qrCameraLayout = (LinearLayout) v.findViewById(R.id.camera_preview); 
      mScannerView = new ZXingScannerView(getActivity().getApplicationContext()); 
      mScannerView.setLayoutParams(new LinearLayout.LayoutParams(
        LinearLayout.LayoutParams.MATCH_PARENT, 
        LinearLayout.LayoutParams.MATCH_PARENT)); 
      qrCameraLayout.addView(mScannerView); 

      List<BarcodeFormat> formats = new ArrayList<>(); 
      formats.add(BarcodeFormat.QR_CODE); 
      mScannerView.setFormats(formats); 

      return v; 
     } 

     @Override 
     public void onResume() 
     { 
      super.onResume(); 
      mScannerView.setResultHandler(this); // Register ourselves as a handler for scan results. 
      mScannerView.startCamera();   // Start camera on resume 
     } 

     @Override 
     public void onPause() 
     { 
      super.onPause(); 
      mScannerView.stopCamera(); 
     } 

     @Override 
     public void handleResult (final Result result) 
     { 
      vibrator.vibrate(369); 
      Log.e("handleresult", result.getText()); 
      //Hold result 
      AlertDialog.Builder builder = new AlertDialog.Builder(getActivity()); 
      builder.setTitle("    Scan Result"); 
      builder.setPositiveButton("Scan Again", new DialogInterface.OnClickListener() { 
       @Override 
       public void onClick(DialogInterface dialog, int which) { 
        mScannerView.resumeCameraPreview(EmptyFragment.this); 
       } 
      }); 
      builder.setNeutralButton("Main Page", new DialogInterface.OnClickListener() { 
       @Override 
       public void onClick(DialogInterface dialog, int which) { 
        mScannerView.stopCamera(); 
        MainPageFragment.create(); 
       } 


      }); 

      builder.setMessage(result.getText()); 
      AlertDialog alertDialog = builder.create(); 
      alertDialog.show(); 
     } 


} 

而這裏的代碼,XML的LinearLayout其中有這樣的觀點:

<?xml version="1.0" encoding="utf-8"?> 
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" 
    android:orientation="vertical" android:layout_width="match_parent" 
    android:layout_height="match_parent"> 


    <View 
     android:id="@+id/camera_scanner" 
     android:layout_width="match_parent" 
     android:layout_height="match_parent" /> 

</LinearLayout> 

這裏的logcat的錯誤:

java.lang.NullPointerException: Attempt to invoke virtual method 'void android.widget.LinearLayout.addView(android.view.View)' on a null object reference 
                       at com.hist_area.imeda.histarea.fragment.EmptyFragment.onCreateView(EmptyFragment.java:52) 
                       at android.support.v4.app.Fragment.performCreateView(Fragment.java:2239) 
                       at android.support.v4.app.FragmentManagerImpl.moveToState(FragmentManager.java:1332) 
                       at android.support.v4.app.FragmentManagerImpl.moveFragmentToExpectedState(FragmentManager.java:1574) 
                       at android.support.v4.app.FragmentManagerImpl.moveToState(FragmentManager.java:1641) 
                       at android.support.v4.app.BackStackRecord.executeOps(BackStackRecord.java:794) 
                       at android.support.v4.app.FragmentManagerImpl.executeOps(FragmentManager.java:2415) 
                       at android.support.v4.app.FragmentManagerImpl.executeOpsTogether(FragmentManager.java:2200) 
                       at android.support.v4.app.FragmentManagerImpl.optimizeAndExecuteOps(FragmentManager.java:2153) 
                       at android.support.v4.app.FragmentManagerImpl.execSingleAction(FragmentManager.java:2034) 
                       at android.support.v4.app.BackStackRecord.commitNowAllowingStateLoss(BackStackRecord.java:651) 
                       at android.support.v4.app.FragmentPagerAdapter.finishUpdate(FragmentPagerAdapter.java:143) 
                       at android.support.v4.view.ViewPager.populate(ViewPager.java:1239) 
                       at android.support.v4.view.ViewPager.populate(ViewPager.java:1087) 
                       at android.support.v4.view.ViewPager.onMeasure(ViewPager.java:1613) 
                       at android.view.View.measure(View.java:18788) 
                       at android.widget.RelativeLayout.measureChildHorizontal(RelativeLayout.java:715) 
                       at android.widget.RelativeLayout.onMeasure(RelativeLayout.java:461) 
                       at android.view.View.measure(View.java:18788) 
                       at android.view.ViewGroup.measureChildWithMargins(ViewGroup.java:5951) 
                       at android.widget.FrameLayout.onMeasure(FrameLayout.java:194) 
                       at android.support.v7.widget.ContentFrameLayout.onMeasure(ContentFrameLayout.java:139) 
                       at android.view.View.measure(View.java:18788) 
                       at android.view.ViewGroup.measureChildWithMargins(ViewGroup.java:5951) 
                       at android.widget.LinearLayout.measureChildBeforeLayout(LinearLayout.java:1465) 
                       at android.widget.LinearLayout.measureVertical(LinearLayout.java:748) 
                       at android.widget.LinearLayout.onMeasure(LinearLayout.java:630) 
                       at android.view.View.measure(View.java:18788) 
                       at android.view.ViewGroup.measureChildWithMargins(ViewGroup.java:5951) 
                       at android.widget.FrameLayout.onMeasure(FrameLayout.java:194) 
                       at android.view.View.measure(View.java:18788) 
                       at android.view.ViewGroup.measureChildWithMargins(ViewGroup.java:5951) 
                       at android.widget.LinearLayout.measureChildBeforeLayout(LinearLayout.java:1465) 
                       at android.widget.LinearLayout.measureVertical(LinearLayout.java:748) 
                       at android.widget.LinearLayout.onMeasure(LinearLayout.java:630) 
                       at android.view.View.measure(View.java:18788) 
                       at android.view.ViewGroup.measureChildWithMargins(ViewGroup.java:5951) 
                       at android.widget.FrameLayout.onMeasure(FrameLayout.java:194) 
                       at com.android.internal.policy.PhoneWindow$DecorView.onMeasure(PhoneWindow.java:2643) 
                       at android.view.View.measure(View.java:18788) 
                       at android.view.ViewRootImpl.performMeasure(ViewRootImpl.java:2100) 
                       at android.view.ViewRootImpl.measureHierarchy(ViewRootImpl.java:1216) 
                       at android.view.ViewRootImpl.performTraversals(ViewRootImpl.java:1452) 
                       at android.view.ViewRootImpl.doTraversal(ViewRootImpl.java:1107) 
                       at android.view.ViewRootImpl$TraversalRunnable.run(ViewRootImpl.java:6013) 
                       at android.view.Choreographer$CallbackRecord.run(Choreographer.java:858) 
                       at android.view.Choreographer.doCallbacks(Choreographer.java:670) 
                       at android.view.Choreographer.doFrame(Choreographer.java:606) 
                       at android.view.Choreographer$FrameDisplayEventReceiver.run(Choreographer.java:844) 
                       at android.os.Handler.handleCallback(Handler.java:739) 
                       at android.os.Handler.dispatchMessage(Handler.java:95) 
                       at android.os.Looper.loop(Looper.java:148) 
                       at android.app.ActivityThread.main(ActivityThread.java:5417) 
                       at java.lang.reflect.Method.invoke(Native Method) 
                       at co 
08-04 17:29:54.075 4819-4819/com.hist_area.imeda.histarea I/Process: Sending signal. PID: 4819 SIG: 9 

回答

0

你有沒有在佈局文件中爲此元素提供了一個id。這樣做:

<?xml version="1.0" encoding="utf-8"?> 
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" 
android:orientation="vertical" android:layout_width="match_parent" 
android:layout_height="match_parent" 
android:id="@+id/camera_preview" <!-- This was missing --> 
> 
<View 
    android:id="@+id/camera_scanner" 
    android:layout_width="match_parent" 
    android:layout_height="match_parent" /> 
</LinearLayout> 
相關問題