2017-08-04 22 views
1

この部分ではZXingscannerビューを追加していますが、コードにエラーは一切表示されませんが、アプリを実行するとクラッシュして停止します。それぞれのタブとタブの断片はこのスキャナですが、何らかの理由でこのビューが破棄されます。ここに私のフラグメント・クラスのコードがあります:実行時にアプリケーションがクラッシュするアンドロイドスタジオの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> 
関連する問題