我有一个主要活动,看起来像:
public class MainActivity extends Activity {
ExpandableListAdapter listAdapter;
ExpandableListView expListView;
WeaponList listData = new WeaponList();
public static StatusCheck checkBombStatus = new StatusCheck();
String weapon = null;
Intent openGameplay;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
setRequestedOrientation(ActivityInfo.SCREEN_ORIENTATION_PORTRAIT);
// get the listview
expListView = (ExpandableListView) findViewById(R.id.expandableListView1);
// preparing list data
listData.prepareWeaponList();
listAdapter = new ExpandableListAdapter(this, listData.listDataHeader, listData.listDataChild);
// setting list adapter
expListView.setAdapter(listAdapter);
//collapse all child-items from the listview
collapseItems();
// Listview on child click listener
expListView.setOnChildClickListener(new OnChildClickListener() {
@Override
public boolean onChildClick(ExpandableListView parent, View v,
int groupPosition, int childPosition, long id) {
weapon = listData.listDataChild.get(listData.listDataHeader.get(groupPosition)).get(childPosition);
openGameplay = new Intent(MainActivity.this, GamePlay.class);
openGameplay.putExtra("weapon", weapon);
startActivity(openGameplay);
return false;
}
});
}
private void collapseItems() {
expListView.expandGroup(0);
expListView.expandGroup(1);
expListView.expandGroup(2);
expListView.expandGroup(3);
expListView.expandGroup(4);
expListView.expandGroup(5);
expListView.expandGroup(6);
expListView.expandGroup(7);
}
public void exitApplication() {
this.finish();
}
和我的第二个活动:
public class GamePlay extends Activity {
...
static MediaPlayer mediaPlayer = new MediaPlayer();
static SoundPool soundPool;
static boolean loaded = false;
private static Context mContext;
public static StatusCheck statusCheck;
public static Context getContext() {
return mContext;
}
public void setContext(Context mContext) {
GamePlay.mContext = mContext;
}
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.gameplay);
setRequestedOrientation(ActivityInfo.SCREEN_ORIENTATION_LANDSCAPE);
Bundle extras = getIntent().getExtras();
....
loadweapon(weapon = extras.getString("weapon"));
loadsound();
imageView.setOnClickListener(new OnClickListener() {
@Override
public void onClick(View v) {
if(j == 1) {
playAudioFromMediaPlayer();
}
else {
//MediaPlayerService.loadAudioToSoundPool(getApplicationContext(), 54);
}
}
});
reload.setOnClickListener(new OnClickListener() {
@Override
public void onClick(View v) {
...
}
});
}
private void loadweapon(String weapon) {
if() ....
}
private void loadsound() {
if(j == 1 && soundID != 29) {
loadAudioToMediaPlayer(soundID);
}
else {
loadAudioToSoundPool(soundID);
}
}
public static void loadAudioToMediaPlayer(int id) {
soundID = id;
mediaPlayer = MediaPlayer.create(mContext.getApplicationContext(), SoundList.soundListfire.get(id));
}
public static void loadAudioToSoundPool(int id) {
soundPool.setOnLoadCompleteListener(new OnLoadCompleteListener() {
@Override
public void onLoadComplete(SoundPool soundPool, int sampleId, int status) {
loaded = true;
}
});
soundID = soundPool.load(mContext.getApplicationContext(), SoundList.soundListfire.get(id), 1);
}
....
}
当我点击Imageview
:
03-11 12:01:20.199: E/AndroidRuntime(8929): java.lang.RuntimeException: Unable to start activity ComponentInfo{....GamePlay}: java.lang.NullPointerException: Attempt to invoke virtual method 'android.content.Context android.content.Context.getApplicationContext()' on a null object reference
03-11 12:01:20.199: E/AndroidRuntime(8929): at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2329)
03-11 12:01:20.199: E/AndroidRuntime(8929): at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2391)
03-11 12:01:20.199: E/AndroidRuntime(8929): at android.app.ActivityThread.access$900(ActivityThread.java:147)
03-11 12:01:20.199: E/AndroidRuntime(8929): at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1296)
03-11 12:01:20.199: E/AndroidRuntime(8929): at android.os.Handler.dispatchMessage(Handler.java:102)
03-11 12:01:20.199: E/AndroidRuntime(8929): at android.os.Looper.loop(Looper.java:135)
03-11 12:01:20.199: E/AndroidRuntime(8929): at android.app.ActivityThread.main(ActivityThread.java:5256)
03-11 12:01:20.199: E/AndroidRuntime(8929): at java.lang.reflect.Method.invoke(Native Method)
03-11 12:01:20.199: E/AndroidRuntime(8929): at java.lang.reflect.Method.invoke(Method.java:372)
03-11 12:01:20.199: E/AndroidRuntime(8929): at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:898)
03-11 12:01:20.199: E/AndroidRuntime(8929): at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:693)
03-11 12:01:20.199: E/AndroidRuntime(8929): Caused by: java.lang.NullPointerException: Attempt to invoke virtual method 'android.content.Context android.content.Context.getApplicationContext()' on a null object reference
03-11 12:01:20.199: E/AndroidRuntime(8929): at cs16.tacticalguns.GamePlay.loadAudioToMediaPlayer(GamePlay.java:442)
03-11 12:01:20.199: E/AndroidRuntime(8929): at cs16.tacticalguns.GamePlay.loadsound(GamePlay.java:423)
03-11 12:01:20.199: E/AndroidRuntime(8929): at cs16.tacticalguns.GamePlay.onCreate(GamePlay.java:72)
03-11 12:01:20.199: E/AndroidRuntime(8929): at android.app.Activity.performCreate(Activity.java:5938)
03-11 12:01:20.199: E/AndroidRuntime(8929): at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1105)
03-11 12:01:20.199: E/AndroidRuntime(8929): at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2282)
03-11 12:01:20.199: E/AndroidRuntime(8929): ... 10 more
我尝试将mContext.getApplicationContext()
替换为this
或Gameplay.this
,但后来我遇到了错误Cannot use this in a static context
。我做错了什么,我该如何解决?
答案 0 :(得分:2)
使用loadAudioToSoundPool方法传递context参数
private Context mContext;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.gameplay);
mContext=this;
public static void loadAudioToSoundPool(Context mContext, int id) {
soundPool.setOnLoadCompleteListener(new OnLoadCompleteListener() {
@Override
public void onLoadComplete(SoundPool soundPool, int sampleId, int status) {
loaded = true;
}
});
soundID = soundPool.load(mContext, SoundList.soundListfire.get(id), 1);
}