简体   繁体   English

无法解决或不在现场

[英]cannot be resolved or not in a field

OK I'm new to android dev's and Java so I'm having problems with on click method here's my code Ii know I've gotta be close thanks in advance all I want my button to do is when its clicked on the phone play a sound. 确定,我是Android开发人员和Java的新手,因此on方法有问题,这是我的代码。我知道我要关闭了,在此先感谢,我希望我的按钮要做的就是单击手机时声音。 But I get this error, pistol1 cannot be resolved or not in a field, it's a sound file that I want to play when I click on button. 但我收到此错误,pistol1无法解析或不在字段中,这是我单击按钮时要播放的声音文件。

import android.os.Bundle;
import android.app.Activity;
import android.media.MediaPlayer;
import android.view.Menu;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;

public class MainActivity extends Activity {
    @Override
    protected void onCreate(Bundle savedInstanceState) {        
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);

        Button one = (Button)this.findViewById(R.id.imageButton1);
        final MediaPlayer mp = MediaPlayer.create(this, R.raw.pistol1);
        one.setOnClickListener(new OnClickListener(){

            public void onClick(View v) {
                mp.start();
            }
        });     
    }
}

Are you sure your file is inside a folder called raw inside res folder ? 您确定文件位于名为raw inside res文件夹的文件夹中吗?

If so, did you made the good import ? 如果是这样,您取得了很好的成绩吗?

import my.project.name.R 导入my.project.name.R

or 要么

import my.project.name.R.raw 导入my.project.name.R.raw

As documentation is saying: Android doc. 正如文档所说: Android文档。

Here is an example of how to play audio that's available as a local raw resource (saved in your application's res/raw/ directory): 这是如何播放作为本地原始资源(保存在应用程序的res / raw /目录中)的音频的示例:

MediaPlayer mediaPlayer = MediaPlayer.create(context, R.raw.sound_file_1);
mediaPlayer.start(); // no need to call prepare(); create() does that for you

In this case, a "raw" resource is a file that the system does not try to parse in any particular way. 在这种情况下,“原始”资源是系统不会尝试以任何特定方式解析的文件。 However, the content of this resource should not be raw audio. 但是,此资源的内容不应为原始音频。 It should be a properly encoded and formatted media file in one of the supported formats. 它应该是受支持格式之一的正确编码和格式化的媒体文件。

Also you should look over the: Supported Media Formats 您还应该查看以下内容: 支持的媒体格式

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM