我的代码有什么问题,请帮帮我

时间:2018-09-28 10:17:51

标签: java android nullpointerexception

MainActivity.java:

    import android.media.MediaPlayer;
    import android.support.v7.app.AppCompatActivity;
    import android.os.Bundle;
    import android.view.View;
    import android.widget.Button;

    public class MainActivity extends AppCompatActivity {

        int currentPosition;

        @Override
        protected void onCreate(Bundle savedInstanceState) {
            super.onCreate(savedInstanceState);
            setContentView(R.layout.activity_main);

            MyListener myListener = new MyListener();

            Button startButton = (Button) findViewById(R.id.start);
            startButton.setOnClickListener(myListener);

            Button pauseButton = (Button) findViewById(R.id.pause);
            pauseButton.setOnClickListener(myListener);

        }
    }

MyListener.java:

    import android.media.MediaPlayer;
    import android.provider.MediaStore;
    import android.support.v7.app.AppCompatActivity;
    import android.os.Bundle;
    import android.view.View;

    public class MyListener extends AppCompatActivity implements View.OnClickListener{

        MediaPlayer musicPlayer = MediaPlayer.create(this, R.raw.sound_file);

        @Override
        protected void onCreate(Bundle savedInstanceState) {
            super.onCreate(savedInstanceState);
            setContentView(R.layout.activity_my_listener);
        }

        @Override
        public void onClick(View v){

            switch(v.getId()){
                case R.id.start:
                    musicPlayer.start();
                    break;
                case R.id.pause:
                    musicPlayer.pause();
                    break;
            }
        }
    }

Logcat显示以下错误:

    java.lang.RuntimeException: Unable to start 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

我正在尝试创建一个可播放res / raw文件中的音频的应用,但是我的代码似乎有问题,因为它无法正常工作,请帮助我。

1 个答案:

答案 0 :(得分:0)

首先,您的MediaPlayer实例应位于MainActivity而不是MyListener内,并且MyListener不应扩展活动。实际上,您应该将所有代码从MyListener移到MainActivity,在您提供的代码段中我看不出有什么用。

第二,您正在Activity Lifecycle之外创建MediaPlayer,同时仍然尝试向其传递上下文:

public class MainActivity extends AppCompatActivity {

    MediaPlayer musicPlayer = MediaPlayer.create(this, R.raw.sound_file);
    ...
}

在活动生命周期开始之前,该活动没有上下文(this),您在上面的编写方式等同于在构造函数中定义musicPlayer

public class MainActivity extends AppCompatActivity {

    MediaPlayer musicPlayer; 

    public MyListener() {
        musicPlayer = MediaPlayer.create(this, R.raw.sound_file);
    }
    ...
}

由于活动生命周期尚未开始,这也将失败。您需要做的是将musicPlayer声明为该类的成员,然后在onCreate()中创建将初始化上下文的实例:

public class MainActivity extends AppCompatActivity {

    MediaPlayer musicPlayer; 

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_my_listener);
        musicPlayer = MediaPlayer.create(this, R.raw.sound_file);
    }
    ...
}

为解决您的评论,下面举例说明如何使用lambdas将其全部放入您的MainActivity

public class MainActivity extends AppCompatActivity {

    int currentPosition;
    MediaPlayer musicPlayer;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        musicPlayer = MediaPlayer.create(this, R.raw.sound_file);

        Button startButton = (Button) findViewById(R.id.start);
        startButton.setOnClickListener(view -> musicPlayer.start());

        Button pauseButton = (Button) findViewById(R.id.pause);
        pauseButton.setOnClickListener(view -> musicPlayer.pause());
    }
}