Android向けに開発しているEclipse Javaでは...
Context c = getBaseContext(); // returns null
Context c = this.getBaseContext(); // throws an exception.
Context c = getApplicationContext(); // throws an exception
Context c = this.getApplicationContext(); // throws an exception.
File f = getFilesDir(); // throws an exception
File f = this.getFilesDir(); // throws an exception
ご覧のとおり、アプリケーションまたは基本コンテキストをまったく取得できません。それらなしでファイルディレクトリを取得しようとしても機能しません。どうすれば自分のファイルディレクトリにアクセスできますか?
public class SoundHandler extends Activity {
private Button mButtonPlay;
private Button mButtonDone;
// private LinearLayout mOverallView;
private PeekActivity mHome;
private MyButtonListener myButtonListener;
private MediaPlayer mPlayer;
private File audioFilePath;
public SoundHandler(PeekActivity home) {
mHome = home;
myButtonListener = new MyButtonListener();
}
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
}
public void open() {
mHome.setContentView(R.layout.sounds);
mButtonDone = (Button) mHome.findViewById(R.id.soundDone);
mButtonDone.setOnClickListener(myButtonListener);
mButtonPlay = (Button) mHome.findViewById(R.id.playSound);
mButtonPlay.setOnClickListener(myButtonListener);
mPlayer = null;
// This is what I thought would work, but it does not
audioFilePath = this.getBaseContext().getFilesDir().getAbsolutePath();
// These are my attempts to see what, if anything works and is not null
// but I've tried all the combinations and permutations above.
SoundHandler a = this;
Context b = getBaseContext();
Context c = getApplicationContext();
File d = this.getFilesDir();
// I'm really just trying to get access to an audio file that is included
// in my build in file /res/raw/my_audio_file.mp3
// mPlayer = MediaPlayer.create(this, R.raw.my_audio_file); doesn't work either
}