2014-04-23 62 views
0

它與前置攝像頭完美配合,但不適用於後置攝像頭。背面照相機不工作 - 致命異常

在IF語句中,condition必須是(cameraId < 1)對不對?我對嗎? 找不出導致錯誤的原因。

此外,當條件改變爲camId> 0時,它給出Toast消息。很顯然,返回的值必須如預期的那樣爲0。爲什麼這會導致錯誤呢?

這裏是我的代碼和logcat的:

package com.iamtheonewhoknocks.toolkit; 

import android.app.Activity; 
import android.content.pm.PackageManager; 
import android.hardware.Camera; 
import android.hardware.Camera.CameraInfo; 
import android.os.Bundle; 
import android.util.Log; 
import android.widget.FrameLayout; 
import android.widget.Toast; 

public class MagnifyActivity extends Activity { // same as MirrorActivity with 
               // different camPreview 
    // Fields ----------------------------------------------------------------- 
    private Camera camera = null; 
    private MirrorViewmg camPreview = null; 
    private FrameLayout previewLayout = null; 
    private int cameraId = -1 ; 

    // Methods ---------------------------------------------------------------- 
    @Override 
    protected void onCreate(Bundle savedInstanceState) { 
     super.onCreate(savedInstanceState); 
     setContentView(R.layout.activity_magnify); 
     // Find out if we even have a camera 
     if (!getPackageManager() 
       .hasSystemFeature(PackageManager.FEATURE_CAMERA)) { 
      Toast.makeText(this, "No camera feature on this device", 
        Toast.LENGTH_SHORT).show(); 
     } else { 

      // Get the ID of the back facing camera 
      cameraId = findFirstBackFacingCamera(); 
      //Log.d(DEBUG_TAG,"value of cameraid=", cameraId); 
      // If we have a valid camera 
      if (cameraId < 1) { 

       // Get the preview frame and strip it of all of it's views 
       previewLayout = (FrameLayout) findViewById(R.id.camPreviewmg); 
       previewLayout.removeAllViews(); 

       // Start the camera 
       startCameraInLayout(previewLayout, cameraId); 

      } else { 
       Toast.makeText(this, "No back facing camera found", 
         Toast.LENGTH_SHORT).show(); 
      } 
     } 
    } 

    @Override 
    protected void onPause() { 
     if (camera != null) { 
      camera.release(); 
      camera = null; 
     } 
     super.onPause(); 
    } 

    @Override 
    protected void onResume() { 
     super.onResume(); 
     if (camera == null && previewLayout != null) { 
      previewLayout.removeAllViews(); 
      startCameraInLayout(previewLayout, cameraId); 
     } 
    } 

    private int findFirstBackFacingCamera() { 
     int foundId = -1; 
     int numCams = Camera.getNumberOfCameras(); 
     for (int camId = 0; camId < numCams; camId++) { 

      // Get the camera information 
      CameraInfo info = new CameraInfo(); 
      Camera.getCameraInfo(camId, info); 

      // Check to see if this is a back facing camera 
      if (info.facing == CameraInfo.CAMERA_FACING_BACK) { 
       foundId = camId; 
       break; 
      } 
     } 
     return foundId; 
    } 

    private void startCameraInLayout(FrameLayout layout, int cameraId) { 
     camera = Camera.open(cameraId); 
     if (camera != null) { 

      // Create a new MirrorView object and 
      // add the view to the FrameLayout 
      camPreview = new MirrorViewmg(this, camera); 
      layout.addView(camPreview); 

     } 
    } 

    /* 
    * @Override public boolean onCreateOptionsMenu(Menu menu) { // Inflate the 
    * menu; this adds items to the action bar if it is present. 
    * getMenuInflater().inflate(R.menu.magnify, menu); return true; } 
    */ 

} 

登錄:

04-23 21:56:36.885: D/AndroidRuntime(31421): Shutting down VM 
04-23 21:56:36.885: W/dalvikvm(31421): threadid=1: thread exiting with uncaught exception (group=0x416b0d40) 
04-23 21:56:36.889: E/AndroidRuntime(31421): FATAL EXCEPTION: main 
04-23 21:56:36.889: E/AndroidRuntime(31421): Process: com.iamtheonewhoknocks.toolkit, PID: 31421 
04-23 21:56:36.889: E/AndroidRuntime(31421): java.lang.RuntimeException: Unable to start activity ComponentInfo{com.iamtheonewhoknocks.toolkit/com.iamtheonewhoknocks.toolkit.MagnifyActivity}: java.lang.NullPointerException 
04-23 21:56:36.889: E/AndroidRuntime(31421): at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2209) 
04-23 21:56:36.889: E/AndroidRuntime(31421): at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2269) 
04-23 21:56:36.889: E/AndroidRuntime(31421): at android.app.ActivityThread.access$800(ActivityThread.java:139) 
04-23 21:56:36.889: E/AndroidRuntime(31421): at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1210) 
04-23 21:56:36.889: E/AndroidRuntime(31421): at android.os.Handler.dispatchMessage(Handler.java:102) 
04-23 21:56:36.889: E/AndroidRuntime(31421): at android.os.Looper.loop(Looper.java:136) 
04-23 21:56:36.889: E/AndroidRuntime(31421): at android.app.ActivityThread.main(ActivityThread.java:5102) 
04-23 21:56:36.889: E/AndroidRuntime(31421): at java.lang.reflect.Method.invokeNative(Native Method) 
04-23 21:56:36.889: E/AndroidRuntime(31421): at java.lang.reflect.Method.invoke(Method.java:515) 
04-23 21:56:36.889: E/AndroidRuntime(31421): at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:785) 
04-23 21:56:36.889: E/AndroidRuntime(31421): at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:601) 
04-23 21:56:36.889: E/AndroidRuntime(31421): at dalvik.system.NativeStart.main(Native Method) 
04-23 21:56:36.889: E/AndroidRuntime(31421): Caused by: java.lang.NullPointerException 
04-23 21:56:36.889: E/AndroidRuntime(31421): at com.iamtheonewhoknocks.toolkit.MirrorViewmg.<init>(MirrorViewmg.java:22) 
04-23 21:56:36.889: E/AndroidRuntime(31421): at com.iamtheonewhoknocks.toolkit.MagnifyActivity.startCameraInLayout(MagnifyActivity.java:94) 
04-23 21:56:36.889: E/AndroidRuntime(31421): at com.iamtheonewhoknocks.toolkit.MagnifyActivity.onCreate(MagnifyActivity.java:43) 
04-23 21:56:36.889: E/AndroidRuntime(31421): at android.app.Activity.performCreate(Activity.java:5248) 
04-23 21:56:36.889: E/AndroidRuntime(31421): at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1110) 
04-23 21:56:36.889: E/AndroidRuntime(31421): at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2173) 
04-23 21:56:36.889: E/AndroidRuntime(31421): ... 11 more 
04-23 21:56:38.418: I/Process(31421): Sending signal. PID: 31421 SIG: 9 

MainActivity.java

package com.iamtheonewhoknocks.toolkit; 

import android.app.Activity; 
import android.content.Intent; 
import android.os.Bundle; 
import android.view.View; 
import android.widget.ImageButton; 

public class MainActivity extends Activity { 

    @Override 
    protected void onCreate(Bundle savedInstanceState) { 
     super.onCreate(savedInstanceState); 
     setContentView(R.layout.activity_main); 

     flashlightButton(); 
     calculatorButton(); 

     mirrorButton(); 

     converterButton(); 
     mgglassButton(); 
     timerButton(); 

    } 

    private void flashlightButton() { 
     ImageButton flButton = (ImageButton) findViewById(R.id.flashButton); 
     flButton.setOnClickListener(new View.OnClickListener() { 

      @Override 
      public void onClick(View v) { 
       startActivity(new Intent(MainActivity.this, 
         FlashlightActivity.class)); 

      } 
     }); 
    } 

    private void calculatorButton() { 
     ImageButton flButton = (ImageButton) findViewById(R.id.calcButton); 
     flButton.setOnClickListener(new View.OnClickListener() { 

      @Override 
      public void onClick(View v) { 
       startActivity(new Intent(MainActivity.this, 
         CalculatorActivity.class)); 

      } 
     }); 
    } 

    private void mirrorButton() { 
     ImageButton mrButton = (ImageButton) findViewById(R.id.mirrorButton); 
     mrButton.setOnClickListener(new View.OnClickListener() { 

      @Override 
      public void onClick(View v) { 
       startActivity(new Intent(MainActivity.this, 
         MirrorActivity.class)); 

      } 
     }); 
    } 

    private void converterButton() { 
     ImageButton flButton = (ImageButton) findViewById(R.id.converterButton); 
     flButton.setOnClickListener(new View.OnClickListener() { 

      @Override 
      public void onClick(View v) { 
       startActivity(new Intent(MainActivity.this, 
         ConverterActivity.class)); 

      } 
     }); 
    } 

    private void mgglassButton() { 
     ImageButton mrButton = (ImageButton) findViewById(R.id.mglassButton); 
     mrButton.setOnClickListener(new View.OnClickListener() { 

      @Override 
      public void onClick(View v) { 
       startActivity(new Intent(MainActivity.this, 
         MagnifyActivity.class)); 

      } 
     }); 
    } 

    private void timerButton() { 
     ImageButton mrButton = (ImageButton) findViewById(R.id.timerButton); 
     mrButton.setOnClickListener(new View.OnClickListener() { 

      @Override 
      public void onClick(View v) { 
       startActivity(new Intent(MainActivity.this, TimerActivity.class)); 

      } 
     }); 
    } 

    /* 
    * @Override public boolean onCreateOptionsMenu(Menu menu) { // Inflate the 
    * menu; this adds items to the action bar if it is present. 
    * getMenuInflater().inflate(R.menu.main, menu); return true; } 
    */ 
} 

回答

0

條件應該是cameraid >= 0

+0

nope。不起作用。 我不明白它..camId返回一個值),對..因爲CAMERA_FACING_BACK有一個值0? 爲什麼這不起作用? – nandu

+0

此外,當條件更改爲camId> 0時,它會發送Toast消息。很顯然,返回的值必須如預期的那樣爲0。爲什麼這會導致錯誤呢? – nandu

+0

這是Camera API常見的誤解。 cameraId對攝像機進行計數(0,1,...,numberOfCameras-1),但CAMERA_FACING_BACK的值與此無關,只是*發生*爲0.反正,findFirstBackFacingCamera()的結果是無效的如果它是-1,意味着沒有找到後視鏡頭。不過,我不認爲這是導致'NullPointerException'的原因。 「MirrorViewmg.java」文件的第22行是什麼? –