为什么 MediaPlayer.create 在 class 的开头初始化时抛出 NullPointer 异常,但在 OnCreate 方法中初始化时不抛出 NullPointer 异常?
Why does MediaPlayer.create throw NullPointer exception when initialised in the beginning of the class but not when initialised in OnCreate method?
在 class 开头使用上下文和资源初始化 MediaPlayer 对象时抛出 NullPointer 异常,但在 class 开头声明它时(因此它是null) 然后在 onCreate 方法中以相同的方式初始化它,就可以了。这也发生在我的其他对象上,比如视图,我不明白为什么它是以同样的方式初始化的。
public class MainActivity extends AppCompatActivity {
//Commented code is how it is written to run without problems
// private MediaPlayer player;
private MediaPlayer player = MediaPlayer.create(this, R.raw.test); //Throws NullPointer Exception
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
// player = MediaPlayer.create(this, R.raw.test);
setContentView(R.layout.activity_main);
}
E/AndroidRuntime: FATAL EXCEPTION: main
Process: com.example.android.musicplayer, PID: 17008
java.lang.RuntimeException: Unable to instantiate activity ComponentInfo{com.example.android.musicplayer/com.example.android.musicplayer.MainActivity}: java.lang.NullPointerException: Attempt to invoke virtual method 'android.content.res.Resources android.content.Context.getResources()' on a null object reference
at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:3066
欢迎使用 Whosebug。
作为错误状态,create
方法需要上下文。此上下文仅在 activity 的 onCreate
方法上创建。也就是说,当您创建变量时,没有上下文,因为 onCreate
还没有发生。
进行以下更改:
public class MainActivity extends AppCompatActivity {
//Commented code is how it is written to run without problems
// private MediaPlayer player;
private MediaPlayer player; //Remove the MediaPlayer.create from here
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
// player = MediaPlayer.create(this, R.raw.test);
setContentView(R.layout.activity_main);
player = MediaPlayer.create(this, R.raw.test); //and put it here
}
在 class 开头使用上下文和资源初始化 MediaPlayer 对象时抛出 NullPointer 异常,但在 class 开头声明它时(因此它是null) 然后在 onCreate 方法中以相同的方式初始化它,就可以了。这也发生在我的其他对象上,比如视图,我不明白为什么它是以同样的方式初始化的。
public class MainActivity extends AppCompatActivity {
//Commented code is how it is written to run without problems
// private MediaPlayer player;
private MediaPlayer player = MediaPlayer.create(this, R.raw.test); //Throws NullPointer Exception
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
// player = MediaPlayer.create(this, R.raw.test);
setContentView(R.layout.activity_main);
}
E/AndroidRuntime: FATAL EXCEPTION: main
Process: com.example.android.musicplayer, PID: 17008
java.lang.RuntimeException: Unable to instantiate activity ComponentInfo{com.example.android.musicplayer/com.example.android.musicplayer.MainActivity}: java.lang.NullPointerException: Attempt to invoke virtual method 'android.content.res.Resources android.content.Context.getResources()' on a null object reference
at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:3066
欢迎使用 Whosebug。
作为错误状态,create
方法需要上下文。此上下文仅在 activity 的 onCreate
方法上创建。也就是说,当您创建变量时,没有上下文,因为 onCreate
还没有发生。
进行以下更改:
public class MainActivity extends AppCompatActivity {
//Commented code is how it is written to run without problems
// private MediaPlayer player;
private MediaPlayer player; //Remove the MediaPlayer.create from here
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
// player = MediaPlayer.create(this, R.raw.test);
setContentView(R.layout.activity_main);
player = MediaPlayer.create(this, R.raw.test); //and put it here
}