- html - 出于某种原因,IE8 对我的 Sass 文件中继承的 html5 CSS 不友好?
- JMeter 在响应断言中使用 span 标签的问题
- html - 在 :hover and :active? 上具有不同效果的 CSS 动画
- html - 相对于居中的 html 内容固定的 CSS 重复背景?
可能有两个问题,一个是关于Handler
,另一个是关于声音池。
这是错误日志:
09-09 06:36:24.743: W/dalvikvm(541): threadid=1: thread exiting with uncaught exception (group=0x409c01f8)
09-09 06:36:24.801: E/AndroidRuntime(541): FATAL EXCEPTION: main
09-09 06:36:24.801: E/AndroidRuntime(541): java.lang.NullPointerException
09-09 06:36:24.801: E/AndroidRuntime(541): at com.example.helloandroid.WelcomeView.playSound(WelcomeView.java:78)
09-09 06:36:24.801: E/AndroidRuntime(541): at com.example.helloandroid.TestHelloAndroidActivity$1.handleMessage(TestHelloAndroidActivity.java:67)
09-09 06:36:24.801: E/AndroidRuntime(541): at android.os.Handler.dispatchMessage(Handler.java:99)
09-09 06:36:24.801: E/AndroidRuntime(541): at android.os.Looper.loop(Looper.java:137)
09-09 06:36:24.801: E/AndroidRuntime(541): at android.app.ActivityThread.main(ActivityThread.java:4424)
09-09 06:36:24.801: E/AndroidRuntime(541): at java.lang.reflect.Method.invokeNative(Native Method)
09-09 06:36:24.801: E/AndroidRuntime(541): at java.lang.reflect.Method.invoke(Method.java:511)
09-09 06:36:24.801: E/AndroidRuntime(541): at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:784)
09-09 06:36:24.801: E/AndroidRuntime(541): at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:551)
09-09 06:36:24.801: E/AndroidRuntime(541): at dalvik.system.NativeStart.main(Native Method)
package com.example.helloandroid;
import android.app.Activity;
import android.os.Bundle;
import android.os.Handler;
import android.view.View;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.os.Looper;
import android.os.Message;
import android.view.KeyEvent;
import android.view.MotionEvent;
import android.view.Window;
import android.view.WindowManager;
public class TestHelloAndroidActivity extends Activity {
/** Called when the activity is first created. */ <br/>
View CurrentView;
WelcomeView wv;
ProgressView pv;
GameView gv;
boolean wantSound=true; //是否开启声音,默认不开
Handler myHandler = new Handler(){ //创建自定义的Handler
public void handleMessage(Message msg) { //重写处理消息的方法
switch(msg.what){ //判断Message对象的类型
case 0: //切换到开始游戏前的进度条界面
pv = new ProgressView(TestHelloAndroidActivity.this, 4); //target为0,进度条走完后去Gameview
setContentView(pv); //切换屏幕到ProgressView
CurrentView = pv; //记录当前View
wv.soundPool.release(); //释放SoundPool
wv.soundPool = null; //声明SoundPool对象为null
wv = null; //声明WelcomeView对象为null
new Thread(){ //创建并启动一个新线程
public void run(){
Looper.prepare();
BitmapManager.loadGamePublic(getResources()); //加载游戏界面图片资源
updateProgress(40); //更新进度条
gv = new GameView(null); //创建GameView对象
updateProgress(100); //更新进度条
}
}.start();
break;
case 4: //开始游戏
setContentView(gv); //切换屏幕到GameView
CurrentView = gv; //记录当前View
pv = null; //声明pv指向的ProgressView的
break;
/*case 1: //显示帮助
hv = new HelpView(RunActivity.this); //创建HelpView对象
setContentView(hv); //切换屏幕到HelpView
currentView = hv; //记录当前View
wv.resetMenu(); //重置菜单按钮
break; */
case 2: //退出游戏
wv.stopSound(1); //停止声音的播放
wv.soundPool.release(); //释放WelcomeView的SoundPool对象
android.os.Process.killProcess(android.os.Process.myPid());
System.exit(0); //退出程序
break;
case 3: //显示欢迎界面
setContentView(wv); //设置当前屏幕为WelcomeView对象
CurrentView = wv; //记录当前View
if(wantSound){ //如有需要,播放声音
wv.playSound(1, -1);
}
pv = null; //将pv指向的ProgressView声明为垃圾
break;
}
}
};
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
//设置全屏
getWindow().requestFeature(Window.FEATURE_NO_TITLE);
getWindow().setFlags(WindowManager.LayoutParams.FLAG_FULLSCREEN, WindowManager.LayoutParams.FLAG_FULLSCREEN);
BitmapManager.loadSystemPublic(getResources());
pv = new ProgressView(this, 3); //创建一个ProgressView对象,目标为3,即走满后去WelcomeView
setContentView(pv);
CurrentView = pv;
new Thread(){
public void run(){
Looper.prepare();
BitmapManager.loadWelcomePublic(getResources()); //加载欢迎界面的图片资源
updateProgress(60);
wv = new WelcomeView(TestHelloAndroidActivity.this);//初始化WelcomeView
updateProgress(100);
}
}.start();
}
@Override
public boolean onTouchEvent(MotionEvent event) {
if(event.getAction() == MotionEvent.ACTION_UP){
int x = (int)event.getX(); //获取屏幕点击处的X坐标
int y = (int)event.getY(); //获取屏幕点击处的Y坐标
if(CurrentView == wv){ //如果当前View为WelcomeView
return wv.myTouchEvent(x, y); //调用WelcomeView的相关事件处理方法
}
/*else if(CurrentView == gv){ //如果当前View为GameView
return gv.myTouchEvent(x, y); //调用GameView的相关事件处理方法
} */
}
return true;
}
//方法:更新进度条 <br/>
public void updateProgress(int progress){
if(pv != null){
pv.setProgress(progress);
}
}
}
package com.example.helloandroid;
import java.util.HashMap;
import android.content.Context;
import android.graphics.Canvas;
import android.graphics.Paint;
import android.graphics.Rect;
import android.graphics.Typeface;
import android.media.AudioManager;
import android.media.SoundPool;
import android.view.SurfaceHolder;
import android.view.SurfaceView;
public class WelcomeView extends SurfaceView implements SurfaceHolder.Callback {
TestHelloAndroidActivity father;
WelcomeDrawThread wdt;
int status = -1; //记录当前状态,0:背景渐显,1:美女移入,2:显示菜单,3:待命,4:有按钮点下
int selectedIndex=-1;//选择的菜单选项,如0为开始游戏/1为帮助/2退出
int mm_x = 129; //mm图片宽度
int mm_y = 289; //mm图片高度
int mmX; //终点X坐标0
int mmY; //终点Y左边191
int btnIndex = 0; //按钮帧索引
int btnSpan = 10; //按钮间隔
int btnHight = 25; //按钮高度
int alpha = 0;
Rect rectMenuStart = new Rect(105,334,317,359);//开始按钮矩形框
Rect rectMenuHelp = new Rect(105,369,317,394);//帮助按钮矩形框
Rect rectMenuQuit = new Rect(105,404,317,429);//退出按钮矩形框
Rect rectSoundMenu = new Rect(140,440,172,472);//声音按钮
SoundPool soundPool; //SoundPool对象引用
HashMap<Integer,Integer> SoundMap; //存放声音资源的Map
public WelcomeView(TestHelloAndroidActivity father) {
super(father);
this.father = father;
getHolder().addCallback(this);
wdt = new WelcomeDrawThread(this, getHolder());
}
public void doDraw(Canvas canvas){
Paint paint = new Paint(); //创建画笔
paint.setAlpha(alpha);
BitmapManager.drawWelcomePublic(0, canvas, 0, 0,paint);//画背景
switch(status){
case 1://状态:美女渐入
BitmapManager.drawWelcomePublic(1, canvas, mmX, mmY, paint);
case 4://状态:有按钮点下
case 3://状态:待命状态,等待按钮按下
BitmapManager.drawWelcomePublic((father.wantSound?6:5), canvas, 140, 440, paint);//画声音菜单,5表示开启那帧,6表示关闭那帧
case 2://状态:显示菜单按钮, 光画框还没写字呢09.09
paint.setAlpha(alpha); //设置画笔透明度
for(int i=0; i<3; i++){
BitmapManager.drawWelcomePublic((i==selectedIndex?btnIndex+2:2), canvas, rectMenuStart.left, rectMenuStart.top+i*(btnHight + btnSpan), paint);
}
break;
}
}
//方法:初始化声音
public void initSound(TestHelloAndroidActivity father){
soundPool = new SoundPool(1,AudioManager.STREAM_MUSIC,100);
int id = soundPool.load(father, R.raw.welcom_background, 1);
SoundMap.put(1, id); //加载背景音乐
}
//方法:播放声音
public void playSound(int sound,int loop){
AudioManager am = (AudioManager)getContext().getSystemService(Context.AUDIO_SERVICE);
float streamVolumeCurrent = am.getStreamVolume(AudioManager.STREAM_MUSIC);
float streamVolumeMax = am.getStreamMaxVolume(AudioManager.STREAM_MUSIC);
float volume = streamVolumeCurrent / streamVolumeMax;
soundPool.play(SoundMap.get(sound), volume, volume, 1, loop, 1);
}
//方法:停止声音
public void stopSound(int sound){
soundPool.stop(SoundMap.get(sound));
}
//方法:处理用户点击屏幕事件
public boolean myTouchEvent(int x,int y){
if(rectMenuStart.contains(x, y)){//点下开始按钮
selectedIndex = 0;//设置按下的索引
status = 4;//设置状态为4,即按下列按钮
}
else if(rectMenuHelp.contains(x, y)){//点下帮助按钮
selectedIndex = 1;//设置按下的索引
status = 4;//设置状态为4,即按下列按钮
}
else if(rectMenuQuit.contains(x, y)){//点下退出按钮
selectedIndex = 2;//设置按下的索引
status = 4;//设置状态为4,即按下列按钮
}
else if(rectSoundMenu.contains(x, y)){//点下了声音菜单按钮
if(father.wantSound){//按下之前是有声音的
stopSound(1);//停止声音的播放
}
else{//按下之前没声音
playSound(1, -1);
}
father.wantSound = !father.wantSound;//置反
}
return true;
}
public void surfaceChanged(SurfaceHolder holder, int format, int width,
int height) {
// TODO Auto-generated method stub
}
public void surfaceCreated(SurfaceHolder holder) {
// TODO Auto-generated method stub
}
public void surfaceDestroyed(SurfaceHolder holder) {
// TODO Auto-generated method stub
}
}
最佳答案
您不会在 Activity 中的WelcomeView中调用initSound
,因此执行soundPool
时null
仍然是playSound
。
关于android - Android Audio Project中未处理的异常,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/12337154/
我最近在/ drawable中添加了一些.gifs,以便可以将它们与按钮一起使用。这个工作正常(没有错误)。现在,当我重建/运行我的应用程序时,出现以下错误: Error: Gradle: Execu
Android 中有返回内部存储数据路径的方法吗? 我有 2 部 Android 智能手机(Samsung s2 和 s7 edge),我在其中安装了一个应用程序。我想使用位于这条路径中的 sqlit
这个问题在这里已经有了答案: What's the difference between "?android:" and "@android:" in an android layout xml f
我只想知道 android 开发手机、android 普通手机和 android root 手机之间的实际区别。 我们不能从实体店或除 android marketplace 以外的其他地方购买开发手
自Gradle更新以来,我正在努力使这个项目达到标准。这是一个团队项目,它使用的是android-apt插件。我已经进行了必要的语法更改(编译->实现和apt->注释处理器),但是编译器仍在告诉我存在
我是android和kotlin的新手,所以请原谅要解决的一个非常简单的问题! 我已经使用导航体系结构组件创建了一个基本应用程序,使用了底部的导航栏和三个导航选项。每个导航选项都指向一个专用片段,该片
我目前正在使用 Facebook official SDK for Android . 我现在正在使用高级示例应用程序,但我不知道如何让它获取应用程序墙/流/状态而不是登录的用户。 这可能吗?在那种情
我在下载文件时遇到问题, 我可以在模拟器中下载文件,但无法在手机上使用。我已经定义了上网和写入 SD 卡的权限。 我在服务器上有一个 doc 文件,如果用户单击下载。它下载文件。这在模拟器中工作正常但
这个问题在这里已经有了答案: What is the difference between gravity and layout_gravity in Android? (22 个答案) 关闭 9
任何人都可以告诉我什么是 android 缓存和应用程序缓存,因为当我们谈论缓存清理应用程序时,它的作用是,缓存清理概念是清理应用程序缓存还是像内存管理一样主存储、RAM、缓存是不同的并且据我所知,缓
假设应用程序 Foo 和 Eggs 在同一台 Android 设备上。任一应用程序都可以获取设备上所有应用程序的列表。一个应用程序是否有可能知道另一个应用程序是否已经运行以及运行了多长时间? 最佳答案
我有点困惑,我只看到了从 android 到 pc 或者从 android 到 pc 的例子。我需要制作一个从两部手机 (android) 连接的 android 应用程序进行视频聊天。我在想,我知道
用于使用 Android 以编程方式锁定屏幕。我从 Stackoverflow 之前关于此的问题中得到了一些好主意,并且我做得很好,但是当我运行该代码时,没有异常和错误。而且,屏幕没有锁定。请在这段代
文档说: android:layout_alignParentStart If true, makes the start edge of this view match the start edge
我不知道这两个属性和高度之间的区别。 以一个TextView为例,如果我将它的layout_width设置为wrap_content,并将它的width设置为50 dip,会发生什么情况? 最佳答案
这两个属性有什么关系?如果我有 android:noHistory="true",那么有 android:finishOnTaskLaunch="true" 有什么意义吗? 最佳答案 假设您的应用中有
我是新手,正在尝试理解以下 XML 代码: 查看 developer.android.com 上的文档,它说“starStyle”是 R.attr 中的常量, public static final
在下面的代码中,为什么当我设置时单选按钮的外观会发生变化 android:layout_width="fill_parent" 和 android:width="fill_parent" 我说的是
很难说出这里要问什么。这个问题模棱两可、含糊不清、不完整、过于宽泛或夸夸其谈,无法以目前的形式得到合理的回答。如需帮助澄清此问题以便重新打开,visit the help center . 关闭 9
假设我有一个函数 fun myFunction(name:String, email:String){},当我调用这个函数时 myFunction('Ali', 'ali@test.com ') 如何
我是一名优秀的程序员,十分优秀!