我是一名试图启动的新编码员。我有一个奇怪的问题。我一直在努力的所有应用程序(包括hello world / myfirstapp演示)强制关闭。每次我将它们加载到真实设备上或从模拟器运行它们。当我尝试执行某些功能时,它们会强制关闭。没有突出显示的错误。更何况,eclipse不会在任何现实世界的设备上安装我的应用程序(我手动完成)。我的一个应用程序播放来自应用内资源的音频文件;在这个应用程序中我认为它强制关闭start()播放音频的调用。 (这个应用程序不完整,但仍然没有错误,应该有效)。另一个只是让手机静音一段时间;在这里,我认为它在设置警报的呼叫后关闭。这是我的代码,请帮帮我。如果您发现任何错误,请告诉我。 (也是的,我做了我的研究,无法找到任何东西。)我认为它可能是eclipse的一个问题,或者一个简单的语法/ java练习我还没有完成,我不知道。我是新人,愿意向有经验的人学习。
所以这是我的媒体应用程序的主要活动。
package com.STI.stockhollywoodsoundeffects;
import android.app.Activity;
import android.media.MediaPlayer;
import android.media.MediaPlayer.OnCompletionListener;
import android.os.Bundle;
import android.view.View;
public class MainActivity extends Activity {
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
}
public void testsound (View view){
MediaPlayer mediaPlayer = MediaPlayer.create(this, R.raw.tooclose);
mediaPlayer.start();
mediaPlayer.setOnCompletionListener(new MediaPlayer.OnCompletionListener() {
public void onCompletion(MediaPlayer mediaPlayer) {
mediaPlayer.release();
mediaPlayer = null;
}
});
}
}
如果有人想看到任何其他文件,请告诉我,我会发布。
这是"定时铃声消音器的主要活动"应用
package com.STI.timedringtonesilencer;
import android.os.Bundle;
import android.view.View;
import android.app.Activity;
import android.app.DialogFragment;
public class MainActivity extends Activity {
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
}
public void startpicker(View v) {
DialogFragment newFragment = new Fragment1();
newFragment.show(getFragmentManager(), "timePicker");
}
}
最后我的这个课程的片段(可能问题在这里,但我再也不知道了)
package com.STI.timedringtonesilencer;
import java.util.Calendar;
import android.annotation.SuppressLint;
import android.app.AlarmManager;
import android.app.Dialog;
import android.app.DialogFragment;
import android.app.PendingIntent;
import android.app.TimePickerDialog;
import android.app.TimePickerDialog.OnTimeSetListener;
import android.content.Context;
import android.content.Intent;
import android.media.AudioManager;
import android.os.Bundle;
import android.os.SystemClock;
import android.text.format.DateFormat;
import android.widget.TimePicker;
@SuppressLint("NewApi")
public class Fragment1 extends DialogFragment
implements TimePickerDialog.OnTimeSetListener {
public static final int REQUEST_CODE = 0;
@Override
public Dialog onCreateDialog(Bundle savedInstanceState) {
final Calendar C = Calendar.getInstance();
int hourofday2 = C.get(Calendar.HOUR_OF_DAY);
int minute2 = C.get(Calendar.MINUTE);
return new TimePickerDialog(getActivity(), (OnTimeSetListener) this, hourofday2, minute2,
DateFormat.is24HourFormat(getActivity()));
}
public void onTimeSet(TimePicker view, int hourofday2, int minute2 ) {
final Calendar C = Calendar.getInstance();
int hourofday1 = C.get(Calendar.HOUR_OF_DAY);
int minute1 = C.get(Calendar.MINUTE);
int numominutes = minute2 - minute1;
int numohours = hourofday2 - hourofday1;
int numohours1 = numohours * 3600;
int numominutes1 = numominutes * 60;
int seconds = numohours1 + numominutes1;
int miliseconds = seconds * 1000;
int alarmType = AlarmManager.ELAPSED_REALTIME_WAKEUP;
Intent intent = new Intent(getActivity(), RingVolumeActivity.class);
intent.setAction(Intent.ACTION_MAIN);
intent.setFlags(Intent.FLAG_ACTIVITY_EXCLUDE_FROM_RECENTS);
PendingIntent pendingIntent = PendingIntent.getActivity(getActivity(), REQUEST_CODE,
intent, 0);
AudioManager audiomanager = (AudioManager)
getActivity().getSystemService(Context.AUDIO_SERVICE);
audiomanager.setRingerMode(AudioManager.RINGER_MODE_SILENT);
AlarmManager alarmManager = (AlarmManager)
getActivity().getSystemService(Context.ALARM_SERVICE);
alarmManager.setExact(alarmType, SystemClock.elapsedRealtime() + miliseconds, pendingIntent);
}
}
stockhollywoddsoundeffects的logcat数据
04-20 20:47:03.780: D/dalvikvm(1257): GC_FOR_ALLOC freed 54K, 10% free 3418K/3768K, paused 229ms, total 232ms
04-20 20:47:04.670: D/dalvikvm(1257): GC_FOR_ALLOC freed 3K, 9% free 3636K/3992K, paused 71ms, total 72ms
04-20 20:47:04.700: I/dalvikvm-heap(1257): Grow heap (frag case) to 6.249MB for 2536936-byte allocation
04-20 20:47:04.770: D/dalvikvm(1257): GC_FOR_ALLOC freed <1K, 6% free 6113K/6472K, paused 61ms, total 61ms
04-20 20:47:05.690: D/(1257): HostConnection::get() New Host Connection established 0xb7ff7a48, tid 1257
04-20 20:47:05.810: W/EGL_emulation(1257): eglSurfaceAttrib not implemented
04-20 20:47:05.820: D/OpenGLRenderer(1257): Enabling debug mode 0
04-20 20:47:25.890: E/MediaPlayer(1257): Unable to create media player
04-20 20:47:25.920: D/MediaPlayer(1257): create failed:
04-20 20:47:25.920: D/MediaPlayer(1257): java.io.IOException: setDataSourceFD failed.: status=0x80000000
04-20 20:47:25.920: D/MediaPlayer(1257): at android.media.MediaPlayer._setDataSource(Native Method)
04-20 20:47:25.920: D/MediaPlayer(1257): at android.media.MediaPlayer.setDataSource(MediaPlayer.java:1025)
04-20 20:47:25.920: D/MediaPlayer(1257): at android.media.MediaPlayer.create(MediaPlayer.java:848)
04-20 20:47:25.920: D/MediaPlayer(1257): at com.STI.stockhollywoodsoundeffects.MainActivity.testsound(MainActivity.java:20)
04-20 20:47:25.920: D/MediaPlayer(1257): at java.lang.reflect.Method.invokeNative(Native Method)
04-20 20:47:25.920: D/MediaPlayer(1257): at java.lang.reflect.Method.invoke(Method.java:515)
04-20 20:47:25.920: D/MediaPlayer(1257): at android.view.View$1.onClick(View.java:3818)
04-20 20:47:25.920: D/MediaPlayer(1257): at android.view.View.performClick(View.java:4438)
04-20 20:47:25.920: D/MediaPlayer(1257): at android.view.View$PerformClick.run(View.java:18422)
04-20 20:47:25.920: D/MediaPlayer(1257): at android.os.Handler.handleCallback(Handler.java:733)
04-20 20:47:25.920: D/MediaPlayer(1257): at android.os.Handler.dispatchMessage(Handler.java:95)
04-20 20:47:25.920: D/MediaPlayer(1257): at android.os.Looper.loop(Looper.java:136)
04-20 20:47:25.920: D/MediaPlayer(1257): at android.app.ActivityThread.main(ActivityThread.java:5017)
04-20 20:47:25.920: D/MediaPlayer(1257): at java.lang.reflect.Method.invokeNative(Native Method)
04-20 20:47:25.920: D/MediaPlayer(1257): at java.lang.reflect.Method.invoke(Method.java:515)
04-20 20:47:25.920: D/MediaPlayer(1257): at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:779)
04-20 20:47:25.920: D/MediaPlayer(1257): at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:595)
04-20 20:47:25.920: D/MediaPlayer(1257): at dalvik.system.NativeStart.main(Native Method)
04-20 20:47:25.920: D/AndroidRuntime(1257): Shutting down VM
04-20 20:47:25.950: W/dalvikvm(1257): threadid=1: thread exiting with uncaught exception (group=0xb1a8eba8)
04-20 20:47:25.980: E/AndroidRuntime(1257): FATAL EXCEPTION: main
04-20 20:47:25.980: E/AndroidRuntime(1257): Process: com.STI.stockhollywoodsoundeffects, PID: 1257
04-20 20:47:25.980: E/AndroidRuntime(1257): java.lang.IllegalStateException: Could not execute method of the activity
04-20 20:47:25.980: E/AndroidRuntime(1257): at android.view.View$1.onClick(View.java:3823)
04-20 20:47:25.980: E/AndroidRuntime(1257): at android.view.View.performClick(View.java:4438)
04-20 20:47:25.980: E/AndroidRuntime(1257): at android.view.View$PerformClick.run(View.java:18422)
04-20 20:47:25.980: E/AndroidRuntime(1257): at android.os.Handler.handleCallback(Handler.java:733)
04-20 20:47:25.980: E/AndroidRuntime(1257): at android.os.Handler.dispatchMessage(Handler.java:95)
04-20 20:47:25.980: E/AndroidRuntime(1257): at android.os.Looper.loop(Looper.java:136)