[android-group-japan: 4216] カメラ起動時エラーが発生

2,936 views
Skip to first unread message

beginner

unread,
May 20, 2010, 1:09:46 AM5/20/10
to 日本Androidの会
Android開発をはじめて1週間が経過しようとしています。

Google Android アプリケーション開発入門 木南英夫さんの本を参考に勉強をしています。

本に記載されていたカメラサンプルを使って開発を行ったところ、以下のエラーが発生しました。

【エラー内容】
HelloCamera(android.HelloCamera)が予期せず停止しました。やり直してください。

何が原因でしょうか?

開発環境 WinXP、Eclipse、Xperia

------------------------------------------------------------------------------------------------------------------------------------------
【HelloCamera.java】
package android.HelloCamera;

import android.app.Activity;
import android.graphics.Bitmap;
import android.graphics.BitmapFactory;
import android.hardware.Camera;
import android.os.Bundle;
import android.util.Log;
import android.view.SurfaceHolder;
import android.view.SurfaceView;
import android.view.View;
import android.view.Window;
import android.widget.ImageButton;
import android.widget.ImageView;

public class HelloCamera extends Activity {
//Log用のTAG
private static final String TAG = "HelloCamera";
//イメージビューに設定する画像の間引きサイズ。1/8の大きさで処理する。
private static final int IN_SAMPLE_SIZE = 8;

//カメラ制御用
private Camera mCamera;

//撮影画像表示用イメージビュー
private ImageView mImage;

//処理中フラグ
private boolean mInProgress;

//カメラのプレビューを表示するSurfaceViewのリスナー
private SurfaceHolder.Callback mSurfaceListener = new
SurfaceHolder.Callback() {
public void surfaceCreated(SurfaceHolder holder) {
//SurfaceViewが生成されたらカメラをオープンする
mCamera = Camera.open();
Log.i(TAG, "Camera opened");

//SDK1.5からsetPreviewDisplayはIOExceptionをthrowするが、
//1.1では定義されていないので、Exceptionを使用する
try{
mCamera.setPreviewDisplay(holder);
}catch(Exception e){
e.printStackTrace();
}
}

public void surfaceDestroyed(SurfaceHolder holder) {
//SurfaceView破棄のタイミングでカメラを解放する
mCamera.release();
mCamera = null;
Log.i(TAG, "Camera released");
}

public void surfaceChanged(SurfaceHolder holder, int format, int
width, int height) {
//プレビューの大きさを設定する
Camera.Parameters parameters = mCamera.getParameters();
parameters.setPreviewSize(width, height);
mCamera.setParameters(parameters);
mCamera.startPreview();
Log.i(TAG, "Camera preview started");
}
};

private Camera.ShutterCallback mShutterListener = new
Camera.ShutterCallback(){
//イメージの処理前に呼び出される
public void onShutter(){
//通常はシャッター音などを鳴らす
Log.i(TAG, "onShutter");
}
};

//ボタンが押された時のリスナー
private View.OnClickListener mButtonListener = new
View.OnClickListener() {
public void onClick(View v) {
if(mCamera != null && mInProgress == false){
//イメージの取得を開始する。リスナーを設定する
mCamera.takePicture(mShutterListener, //シャッター後
null, //Rawイメージ生成後
mPicutureListener); //JPEGイメージ生成後
mInProgress = true;
}
}
};

//JPEG画像が生成された後に呼び出される
private Camera.PictureCallback mPicutureListener = new
Camera.PictureCallback(){
public void onPictureTaken(byte[] data, Camera camera){
Log.i(TAG, "Picture Taken");
if(data != null){
Log.i(TAG, "JPEG Picture Taken");

//処理する画像のサイズを縮小する
BitmapFactory.Options options = new BitmapFactory.Options();
options.inSampleSize = IN_SAMPLE_SIZE;
Bitmap bitmap = BitmapFactory.decodeByteArray(data, 0,
data.length, options);

//イメージビューに画像を設定する
mImage.setImageBitmap(bitmap);

//停止しているプレビューを再開する
camera.startPreview();

//処理中フラグを落とす
mInProgress = false;
}
}
};

public void onCreate(Bundle savedInstanceState){
super.onCreate(savedInstanceState);
setContentView(R.layout.main);

mImage = (ImageView) findViewById(R.id.image_view);

SurfaceView surface = (SurfaceView) findViewById(R.id.surface_view);
SurfaceHolder holder = surface.getHolder();

//SurfaceViewにリスナーを登録する
holder.addCallback(mSurfaceListener);

//外部のバッファを使用するように設定する
holder.setType(SurfaceHolder.SURFACE_TYPE_PUSH_BUFFERS);

//シャッター用のボタンにリスナーを登録する
ImageButton button = (ImageButton) findViewById(R.id.shutter);
button.setOnClickListener(mButtonListener);
}
}
------------------------------------------------------------------------------------------------------------------------------------------
【main.xml】
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/
android"
android:orientation="vertical"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
>

<!-- プレビューを表示するビュー -->
<surfaceView
android:id="@+id/surface_view"
android:layout_gravity="center"
android:layout_height="100px"
android:layout_width="160px"
/>

<!-- シャッター用のボタン -->
<ImageButton
android:layout_width="wrap_content"
android:layout_height="fill_parent"
android:id="@+id/shutter"
android:src="@drawable/arrow"
/>

<!-- 取得したイメージを表示する -->
<ImageView
android:layout_height="fill_parent"
android:layout_width="fill_parent"
android:layout_weight="1"
android:id="@+id/image_view"
/>
</LinearLayout>

------------------------------------------------------------------------------------------------------------------------------------------
【string.xml】

<?xml version="1.0" encoding="utf-8"?>
<resources>
<string name="hello">Hello World, HelloCamera!</string>
<string name="app_name">HelloCamera</string>
</resources>
------------------------------------------------------------------------------------------------------------------------------------------

【AndroidManifest.xml】
<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
package="android.HelloCamera"
android:versionCode="1"
android:versionName="1.0">
<application android:icon="@drawable/icon" android:label="@string/
app_name">
<activity android:name=".HelloCamera"
android:label="@string/app_name"
android:screenOrientation="landscape">
<intent-filter>
<action android:name="android.intent.action.MAIN" />
<category
android:name="android.intent.category.LAUNCHER" />
</intent-filter>
</activity>
</application>
<uses-permission android:name="android.permission.CAMERA" />
<uses-sdk android:minSdkVersion="3" />
</manifest>
------------------------------------------------------------------------------------------------------------------------------------------


AndroidManifest.xmlに <uses-permission
android:name="android.permission.CAMERA" /> は
入れてあります。

よろしくご教示、お願い致します。

--
このメールは Google グループのグループ「日本Androidの会」の登録者に送られています。
このグループに投稿するには、android-g...@googlegroups.com にメールを送信してください。
このグループから退会するには、android-group-j...@googlegroups.com にメールを送信してください。
詳細については、http://groups.google.com/group/android-group-japan?hl=ja からこのグループにアクセスしてください。

Hideo Kinami

unread,
May 20, 2010, 7:47:25 AM5/20/10
to android-g...@googlegroups.com
木南です。

> Android開発をはじめて1週間が経過しようとしています。
Androidの世界にようこそ!

【main.xml】の8行目のSufaceViewのスペルが、小文字のsで始まる「surfaceView」になっているために、エラーにが発生しているようですね。

このように、アプリケーションが終了してしまう場合には、EclipseのWindow > Open Perspetiveメニューから、DDMS
画面を開いて、下の方の、「Log Cat」という画面を確認してみてください。
この画面には、実行中のアプリケーションから出力されるいろいろなメッセージが表示されています。

今回の場合、以下のようなエラーが出ていました。

05-20 20:26:47.383: ERROR/AndroidRuntime(345):
java.lang.RuntimeException: Unable to start activity
ComponentInfo{android.HelloCamera/android.HelloCamera.HelloCamera}:
android.view.InflateException: Binary XML file line #9: Error
inflating class surfaceView

このメッセージから、 XML fileの9行目に書かれている「surfaceView」が展開できないよといっていることがわかります。

# ちなみに、僕のサンプルもちょっとレイアウトがちょっと崩れているようです。
いろいろと試行錯誤しながら、デバックを楽しんでみてください。
では!
--
Hideo



2010年5月20日14:09 beginner <android_...@yahoo.co.jp>:
--
木南 英夫 (http://d.hatena.ne.jp/hkinami/)

櫻井覚司

unread,
May 21, 2010, 1:30:53 AM5/21/10
to android-g...@googlegroups.com
はじめまして。さとです
 
public class HelloCamera extends Activity
 
の後に何か抜けているものがないでしょうか!?
参考書を見るとその部分が抜けてると思いますが・・・
 
でも、そこがないとコンパイルも通らないと思うのですが。。。
 
もし、勘違いだったらすいません。

2010年5月20日14:09 beginner <android_...@yahoo.co.jp>:

beginner

unread,
May 21, 2010, 3:05:44 AM5/21/10
to 日本Androidの会
木南さん

ご回答、ありがとうございました。
main.xmlの8行目を直したところ、問題なく動作することができました。
ビルドでエラー扱いにはならないのですね。

著者の木南さんからご返信いただけて光栄です。
木南さんの本は非常に読みやすく分かりやすく記載されているので、勉強になります。

今後ともよろしくお願い申し上げます。

beginner

unread,
May 21, 2010, 3:08:27 AM5/21/10
to 日本Androidの会
さとさん

書き込みありがとうございます。

木南さんのご指摘のとおり、main.xmlの8行目を修正したところ、問題なく動作しました。

ご指摘ありがとうございました。
Message has been deleted

beginner

unread,
May 21, 2010, 3:41:36 AM5/21/10
to 日本Androidの会
main.htmlを以下のように変更しました。
SurfaceViewとImageViewを

android:layout_height="200px"
android:layout_width="150px"

と同じ大きさにしたのですが、アプリを起動すると、ImageViewのほうが小さく表示されてしまいます。

SurfaceViewとImageViewを同じ大きさにする方法がありましたら、ご教示いただけないでしょうか。

よろしくお願い致します。


以下、ソースです。
-------------------------------------------------------------------------
<!-- プレビューを表示するビュー -->
<SurfaceView
android:id="@+id/surface_view"
android:layout_gravity="center"
android:layout_height="200px"
android:layout_width="150px"
/>

<!-- シャッター用のボタン -->
<ImageButton
android:id="@+id/shutter"
android:src="@drawable/star"
android:layout_gravity="center"
android:layout_height="50px"
android:layout_width="50px"
/>

<!-- 取得したイメージを表示する -->
<ImageView
android:id="@+id/image_view"
android:layout_gravity="center"
android:layout_height="200px"
android:layout_width="150px"
/>

beginner

unread,
May 21, 2010, 6:17:50 AM5/21/10
to 日本Androidの会
追加で質問があります。m(__)m

カメラで撮影し、撮影場所の緯度・経度値を別ウィンドウで表示するところまで作成しましたが、
Xperiaを縦に見た場合、上からプレビュー画像、撮影ボタン、取得したイメージ画像という順番で表示され、
ここまではいいのですが、メニューボタン押した時に、「現在地座標表示」ボタンとそのボタンを押したあとの別ウィンドウに表示される緯度・経度値が
携帯を横に倒した中央に表示されてしまいます。縦に見た場合、左端にメニューボタンが表示されます。

どこがいけないのでしょうか?

以下、ソースです。
--------------------------------------------------------
【map2.java】
package android.map2;
import android.app.AlertDialog;
import android.content.Context;
import android.content.DialogInterface;
import android.content.Intent;
import android.graphics.Bitmap;
import android.graphics.BitmapFactory;
import android.hardware.Camera;
import android.location.Location;
import android.location.LocationListener;
import android.location.LocationManager;
import android.os.Bundle;
import android.util.Log;
import android.view.Menu;
import android.view.MenuItem;
import android.view.SurfaceHolder;
import android.view.SurfaceView;
import android.view.View;
import android.widget.ImageView;
import android.widget.ImageButton;

import com.google.android.maps.GeoPoint;
import com.google.android.maps.MapActivity;
import com.google.android.maps.MapView;
import com.google.android.maps.MapController;


//位置情報の取得
public class map2 extends MapActivity
implements LocationListener {
private final static String API_KEY =
"************************************************:";

//Log用のTAG
private static final String TAG = "HelloCamera";

//イメージビューに設定する画像の間引きサイズ。1/8の大きさで処理する。
private static final int IN_SAMPLE_SIZE = 8;

private Camera mCamera; //カメラ制御用
private ImageView mImage; //撮影画像表示用イメージビュー
private boolean mInProgress; //処理中フラグ
private MapView mapView; //Mapビュー
private MapController mapCtrl; //Mapコントローラ
private LocationManager lm; //ロケーションマネージャ

private float latitude = 0f; //緯度
private float longitude = 0f; //経度

//メニューID
private final int MENU_ID1 = Menu.FIRST;

//カメラのプレビューを表示するSurfaceViewのリスナー
private SurfaceHolder.Callback mSurfaceListener = new
SurfaceHolder.Callback() {
public void surfaceCreated(SurfaceHolder holder) {

mCamera = Camera.open();
Log.i(TAG, "Camera opened");

//ロケーションマネージャの設定

lm=(LocationManager)getSystemService(Context.LOCATION_SERVICE);
lm.requestLocationUpdates(LocationManager.GPS_PROVIDER,
0,0,this);

//Mapビューの生成
mapView=new MapView(this,API_KEY);
// setContentView(mapView);
mapCtrl=mapView.getController();
mapCtrl.setZoom(16);

mImage = (ImageView) findViewById(R.id.image_view);

SurfaceView surface = (SurfaceView) findViewById(R.id.surface_view);
SurfaceHolder holder = surface.getHolder();

//SurfaceViewにリスナーを登録する
holder.addCallback(mSurfaceListener);

//外部のバッファを使用するように設定する
holder.setType(SurfaceHolder.SURFACE_TYPE_PUSH_BUFFERS);

//シャッター用のボタンにリスナーを登録する
ImageButton button = (ImageButton) findViewById(R.id.shutter);
button.setOnClickListener(mButtonListener);
}

//ルート表示するかどうか
protected boolean isRouteDisplayed() {
return false;
}

//メニューを作成する
public boolean onCreateOptionsMenu(Menu menu) {
super.onCreateOptionsMenu(menu);

menu.add(0, MENU_ID1, 0,

getString(R.string.menu_Rashinban)).setIcon(android.R.drawable.ic_menu_compass);
return true;
}

//メニューボタン押下時の処理
public boolean onPrepareOptionsMenu(Menu menu) {
super.onPrepareOptionsMenu(menu);
return true;
}

//位置情報変更のイベント処理
public void onLocationChanged(Location location) {
//緯度と経度の取得
GeoPoint pos=new GeoPoint(
(int)(location.getLatitude()*1E6),
(int)(location.getLongitude()*1E6));
mapCtrl.setCenter(pos);
}

//メニューを選択時の処理
public boolean onOptionsItemSelected(MenuItem item) {
switch (item.getItemId()) {
case MENU_ID1:

GeoPoint gPoint = mapView.getMapCenter();

latitude =
Float.parseFloat(String.valueOf(gPoint.getLatitudeE6() / 1E6)); //緯度
longitude =
Float.parseFloat(String.valueOf(gPoint.getLongitudeE6() / 1E6)); //経度

//ダイアログ設定
AlertDialog.Builder aDialogBuilder = new
AlertDialog.Builder(this);

aDialogBuilder.setTitle(getString(R.string.msg_confirmation));

aDialogBuilder.setMessage(
getString(R.string.msg_Latitude) + latitude + "\n"
+
getString(R.string.msg_Longitude) + longitude +
"\n" +
getString(R.string.msg_rashinban_start));

aDialogBuilder.setPositiveButton(getString(R.string.msg_yes),
new DialogInterface.OnClickListener(){
public void onClick(DialogInterface dialog,int
whichButton) {
Intent intent = new Intent();
intent.putExtra("lat", latitude); //緯度
intent.putExtra("lon", longitude); //経度
}
});


aDialogBuilder.setNegativeButton(getString(R.string.msg_cancel),
new DialogInterface.OnClickListener(){
public void onClick(DialogInterface dialog,int
whichButton) {
}
});

aDialogBuilder.create(); //ダイアログ作成
aDialogBuilder.show(); //ダイアログ表示

return true;
default:
break;
}
return super.onOptionsItemSelected(item);
}

//位置情報取得有効化のイベントの処理
public void onProviderEnabled(String provider) {
}

//位置情報取得無効化のイベント処理
public void onProviderDisabled(String provider) {
}

//位置情報状態変更のイベント処理
public void onStatusChanged(String provider,int status,Bundle
extras) {
}
}
--------------------------------------------------------


--------------------------------------------------------
【main.html】
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/
android"
android:orientation="horizontal"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
>
<!-- プレビューを表示するビュー -->
<SurfaceView
android:id="@+id/surface_view"
android:layout_gravity="center"
android:layout_height="200px"
android:layout_width="150px"
/>

<!-- シャッター用のボタン -->
<ImageButton
android:id="@+id/shutter"
android:src="@drawable/star"
android:layout_gravity="center"
android:layout_height="50px"
android:layout_width="50px"
/>

<!-- 取得したイメージを表示する -->
<ImageView
android:id="@+id/image_view"
android:layout_gravity="center"
android:layout_height="200px"
android:layout_width="150px"
/>
</LinearLayout>
--------------------------------------------------------

--------------------------------------------------------
【AndroidManifest.xml】
<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
package="android.map2"
android:versionCode="1"
android:versionName="1.0">
<application android:icon="@drawable/icon" android:label="@string/
app_name" >
<activity android:name=".map2"
android:label="@string/app_name"
android:screenOrientation="landscape">
<intent-filter>
<action android:name="android.intent.action.MAIN" />
<category
android:name="android.intent.category.LAUNCHER" />
</intent-filter>
</activity>
<uses-library android:name="com.google.android.maps"></uses-
library>
</application>
<uses-permission
android:name="android.permission.ACCESS_FINE_LOCATION"></uses-
permission>
<uses-permission
android:name="android.permission.ACCESS_MOCK_LOCATION"></uses-
permission>
<uses-permission android:name="android.permission.INTERNET"></uses-
permission>
<uses-permission
android:name="android.permission.ACCESS_COARSE_LOCATION"></uses-
permission>
<uses-permission android:name="android.permission.CAMERA" />
</manifest>

--------------------------------------------------------
Reply all
Reply to author
Forward
0 new messages