予期せず停止のエラー

974 views
Skip to first unread message

yocto

unread,
Jan 12, 2012, 9:01:33 PM1/12/12
to 日本Androidの会
はじめまして。

今まではJavaやC言語など基本的なプログラムを勉強していて、
Androidは最近勉強し始めました。

Google Android アプリ開発ガイド第2版 クロノス・クラウンさんと柳井政和さんの本で勉強しています。

記載されているサンプルアプリを作成時に下記のエラーが出ました。

【エラー内容】
エミュレータを起動後、「予期せず停止しました。再起動してください。」
というエラーメッセージが出ます。

【これまでの対処法】
検索して、XMLの修正や不要アプリなどを消したのですが、それでもエラーメッセージが出ます。
XMLは本通り記述しましたし、エラーメッセージは出ないので、どこが間違えているかわかりません。

何が原因でしょうか?

開発環境 Win7、Eclipse、エミュレータ

------------------------------------------------------------------------------------------------------------------------------------------
【Tanbarin.java】

package com.crocro.android.sample;

import android.app.Activity;
import android.media.MediaPlayer;
import android.os.Bundle;
import android.view.MotionEvent;
import android.view.View;

public class Tanbarin extends Activity {
/** Called when the activity is first created. */

//効果音
private final int[] SE_TYPE = {
R.raw.tambourine0,
R.raw.tambourine1,
R.raw.tambourine2,
R.raw.tambourine3,
R.raw.tambourine4,
R.raw.tambourine5
};

private final int SE_TYPE_MAX = 6;

private MediaPlayer[][] mSePlayer;
private final int SE_STACK_MAX = 5;

//Activity作成
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);

//タッチ・リスナーの登録

findViewById(R.id.tambourine_linear).setOnTouchListener(touchListener);

//効果音の初期化
try{
mSePlayer = new MediaPlayer[SE_TYPE_MAX][SE_STACK_MAX];
for(int i=0;i<SE_TYPE_MAX;i++){
for(int j=0;j<SE_STACK_MAX;j++){
mSePlayer[i][j]=MediaPlayer.create(this, SE_TYPE[i]);
mSePlayer[i][j].setLooping(false);
}
}
}catch (Exception e) {
// TODO: handle exception
}
}

//タッチ・リスナー
View.OnTouchListener touchListener = new View.OnTouchListener() {
public boolean onTouch(View v, MotionEvent event) {
// TODO 自動生成されたメソッド・スタブ
//タッチ以外は無視
if(event.getAction() != MotionEvent.ACTION_DOWN) return false;

//変数の初期化
int centerX, centerY;
int distPer, rangePer;

centerX = v.getWidth() / 2;
centerY = v.getHeight() / 2 ;

distPer = dist(centerX, centerY, (int)event.getX(),
(int)event.getY()) * 100 / dist(centerX, centerY, 0,0);
rangePer = 100 / SE_TYPE_MAX;

//タッチ時(中央からの距離によって音を変える)
for(int i = 0; i<SE_TYPE_MAX;i++){
if(i + rangePer <= distPer && distPer < (i+1) * rangePer){
playSe(i);
}
}
return true;
}
};

//距離計算
private int dist(int x0, int y0, int x1, int y1){
return (int)Math.sqrt((x0-x1)*(x0-x1) + (y0-y1) * (y0-y1));
}

//効果音再生
public void playSe(int type){
if(mSePlayer == null) return;
try{
for(int i=0;i<SE_STACK_MAX;i++){
if(mSePlayer[type][i] == null)continue;
if(mSePlayer[type][i].isPlaying()) continue;
mSePlayer[type][i].seekTo(0);
mSePlayer[type][i].start();
break;
}
}catch(Exception e){

}
}
}

------------------------------------------------------------------------------------------------------------------------------------------
【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"
>
<TextView
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:text="@string/hello"
/>
</LinearLayout>

------------------------------------------------------------------------------------------------------------------------------------------

【chapter6_tambourine.xml】

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/
android"
android:id="@+id/tambourine_linear"
android:orientation="vertical"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:gravity="center">

<ImageView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:src="@drawable/tambourine"/>

</LinearLayout>

------------------------------------------------------------------------------------------------------------------------------------------

【AndroidManifest.xml】

<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
package="com.crocro.android.sample"
android:versionCode="1"
android:versionName="1.0" >

<uses-sdk android:minSdkVersion="8" />

<application
android:icon="@drawable/ic_launcher"
android:label="@string/app_name" >
<activity
android:label="@string/app_name"
android:name=".Tanbarin" >
<intent-filter >
<action android:name="android.intent.action.MAIN" />

<category
android:name="android.intent.category.LAUNCHER" />
</intent-filter>
</activity>
</application>

</manifest>

------------------------------------------------------------------------------------------------------------------------------------------


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

goki

unread,
Jan 13, 2012, 2:25:12 AM1/13/12
to 日本Androidの会
こんにちは、gokiです。

ソースを読んではいないのですが、自分がよくする原因の探し方を書きます。

Log.d( String tag, String msg);

を各メソッドの先頭や怪しいと思う箇所に配置して、
ログを見ます。
エラーログが吐き出される直前のメッセージを見て、どこまではきているか確認したら
エラー箇所がわかると思います。

参考にしていただければ幸いです。
以上です。

田代透

unread,
Jan 13, 2012, 3:17:08 AM1/13/12
to android-g...@googlegroups.com
LogCatを見れば、何が原因で落ちているのかわかりますし、エラーが発生した位置を直接もしくは
間接的に知ることができます。

オリジナルのサンプルースコードの一部を抜き出したようですが、そのためバグが発生しているようです。


2012年1月13日16:25 goki <goki...@gmail.com>:

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

--
--
Toru Tashiro
toru...@gmail.com

yocto

unread,
Jan 14, 2012, 2:56:12 AM1/14/12
to 日本Androidの会
こんにちは。

回答ありがとうございます。
Log,dで配置して、ログを見て、エラー箇所を探せるのですね。
勉強になります。

早速、試させて頂きます。

ありがとうございました。
> > よろしくご教示、お願い致します。- 引用テキストを表示しない -
>
> - 引用テキストを表示 -

yocto

unread,
Jan 14, 2012, 3:18:08 AM1/14/12
to 日本Androidの会
回答ありがとうございます。
LogCatをあまり見ていませんでした。
LogCatでエラー位置などの原因がわかるのですね。
まだまだ勉強不足を痛感しています。


> オリジナルのサンプルースコードの一部を抜き出したようですが、そのためバグが発生しているようです。
たしかに一部抜き出しました。
その部分がバグになっているのですね。
その部分を再度見直して修正したいと思います。

ありがとうございました。

On 1月13日, 午後5:17, 田代透 <toru2...@gmail.com> wrote:
> LogCatを見れば、何が原因で落ちているのかわかりますし、エラーが発生した位置を直接もしくは
> 間接的に知ることができます。
>
> オリジナルのサンプルースコードの一部を抜き出したようですが、そのためバグが発生しているようです。
>
> 2012年1月13日16:25 goki <gokit...@gmail.com>:
> toru2...@gmail.com- 引用テキストを表示しない -
>
> - 引用テキストを表示 -
Reply all
Reply to author
Forward
0 new messages