在为 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
}