Unable to instantiate activity:Attempt to invoke virtual method android.content.Context.getResources()

谁都会走 提交于 2021-02-07 20:50:45

问题


i am a beginner.

First of all say that I have read previous posts but did not answer.

I try to test a audio player,But when I try to run the app, it instantly crashes because of a null object reference.

This is Logcat: (this log say: Error is in Line 28 of java code)

EXCEPTION: main
  Process: ir.pluto.mediaplayer, PID: 19100
  java.lang.RuntimeException: Unable to instantiate activity ComponentInfo{ir.pluto.mediaplayer/ir.pluto.mediaplayer.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:3132)
      at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:3415)
      at android.app.ActivityThread.access$1100(ActivityThread.java:229)
      at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1821)
      at android.os.Handler.dispatchMessage(Handler.java:102)
      at android.os.Looper.loop(Looper.java:148)
      at android.app.ActivityThread.main(ActivityThread.java:7325)
      at java.lang.reflect.Method.invoke(Native Method)
      at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:1230)
      at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:1120)
   Caused by: java.lang.NullPointerException: Attempt to invoke virtual method 'android.content.res.Resources android.content.Context.getResources()' on a null object reference
      at android.content.ContextWrapper.getResources(ContextWrapper.java:92)
      at android.view.ContextThemeWrapper.getResources(ContextThemeWrapper.java:81)
      at android.view.View.<init>(View.java:4054)
      at android.view.View.<init>(View.java:4186)
      at android.widget.ProgressBar.<init>(ProgressBar.java:313)
      at android.widget.AbsSeekBar.<init>(AbsSeekBar.java:188)
      at android.widget.SeekBar.<init>(SeekBar.java:85)
      at android.widget.SeekBar.<init>(SeekBar.java:81)
      at android.widget.SeekBar.<init>(SeekBar.java:77)
      at android.widget.SeekBar.<init>(SeekBar.java:73)
      at ir.pluto.mediaplayer.MainActivity.<init>(MainActivity.java:28)
      at java.lang.Class.newInstance(Native Method)
      at android.app.Instrumentation.newActivity(Instrumentation.java:1096)
      at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:3122)
      at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:3415) 
      at android.app.ActivityThread.access$1100(ActivityThread.java:229) 
      at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1821) 
      at android.os.Handler.dispatchMessage(Handler.java:102) 
      at android.os.Looper.loop(Looper.java:148) 
      at android.app.ActivityThread.main(ActivityThread.java:7325) 
      at java.lang.reflect.Method.invoke(Native Method) 
      at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:1230) 
      at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:1120)

 And part of my java code is:

import android.app.Activity;
import android.content.Intent;
import android.media.MediaPlayer;
import android.os.Bundle;
import android.os.Handler;
import android.view.View;
import android.widget.ImageButton;
import android.widget.SeekBar;
import android.widget.TextView;
import android.widget.Toast;
import java.io.IOException;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.Random;

public class MainActivity extends Activity  implements MediaPlayer.OnCompletionListener {

    ImageButton btnPlay;
    ImageButton btnForward;
    ImageButton btnBackward;
    ImageButton btnNext;
    ImageButton btnPrevious;
    ImageButton btnPlaylist;
    ImageButton btnRepeat;
    ImageButton btnShuffle;
    SeekBar songProgressBar=new SeekBar(this);
    TextView songTitleLabel;
    TextView songCurrentDurationLabel;
    TextView songTotalDurationLabel;

    // Media Player
    MediaPlayer mp;

    // Handler to update UI timer, progress bar etc,.
    Handler mHandler = new Handler();
    SongsManager songManager;
    Utilities utils;
    int seekForwardTime = 5000; // 5000 milliseconds
    int seekBackwardTime = 5000; // 5000 milliseconds
    int currentSongIndex = 0;
    boolean isShuffle = false;
    boolean isRepeat = false;
    ArrayList<HashMap<String, String>> songsList = new ArrayList<>();

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

        btnPlay = (ImageButton) findViewById(R.id.btnPlay);
        btnForward = (ImageButton) findViewById(R.id.btnForward);
        btnBackward = (ImageButton) findViewById(R.id.btnBackward);
        btnNext = (ImageButton) findViewById(R.id.btnNext);
        btnPrevious = (ImageButton) findViewById(R.id.btnPrevious);
        btnPlaylist = (ImageButton) findViewById(R.id.btnPlaylist);
        btnRepeat = (ImageButton) findViewById(R.id.btnRepeat);
        btnShuffle = (ImageButton) findViewById(R.id.btnShuffle);
        songProgressBar = (SeekBar) findViewById(R.id.songProgressBar);
        songTitleLabel = (TextView) findViewById(R.id.songTitle);
        songCurrentDurationLabel = (TextView) findViewById(R.id.songCurrentDurationLabel);
        songTotalDurationLabel = (TextView) findViewById(R.id.songTotalDurationLabel);

        // Media player
        mp = new MediaPlayer();
        songManager = new SongsManager();
        utils = new Utilities();

        // Listeners
        songProgressBar.setOnSeekBarChangeListener(new SeekBar.OnSeekBarChangeListener() {
            @Override
            public void onProgressChanged(SeekBar seekBar, int progress, boolean fromUser) {
            }...

and line 28 is:

SeekBar songProgressBar=new SeekBar(this);

Thank you for your answers ...

After Update my code by @PavneetSingh answer,The same error occurred again but this time on line 70 and it is:

songProgressBar.setOnSeekBarChangeListener(this); // Important

Complete code of Main Activty after edit by first answer is:

package ir.pluto.mediaplayer;

import android.app.Activity;
import android.content.Intent;
import android.media.MediaPlayer;
import android.os.Bundle;
import android.os.Handler;
import android.view.View;
import android.widget.ImageButton;
import android.widget.SeekBar;
import android.widget.TextView;
import android.widget.Toast;
import java.io.IOException;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.Random;

public class MainActivity extends Activity  implements MediaPlayer.OnCompletionListener,SeekBar.OnSeekBarChangeListener {

    ImageButton btnPlay;
    ImageButton btnForward;
    ImageButton btnBackward;
    ImageButton btnNext;
    ImageButton btnPrevious;
    ImageButton btnPlaylist;
    ImageButton btnRepeat;
    ImageButton btnShuffle;
    SeekBar songProgressBar;
    TextView songTitleLabel;
    TextView songCurrentDurationLabel;
    TextView songTotalDurationLabel;

    // Media Player
    MediaPlayer mp;

    // Handler to update UI timer, progress bar etc,.
    Handler mHandler = new Handler();
    SongsManager songManager;
    Utilities utils;
    int seekForwardTime = 5000; // 5000 milliseconds
    int seekBackwardTime = 5000; // 5000 milliseconds
    int currentSongIndex = 0;
    boolean isShuffle = false;
    boolean isRepeat = false;
    ArrayList<HashMap<String, String>> songsList = new ArrayList<>();

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);               
        btnPlay = (ImageButton) findViewById(R.id.btnPlay);
        btnForward = (ImageButton) findViewById(R.id.btnForward);
        btnBackward = (ImageButton) findViewById(R.id.btnBackward);
        btnNext = (ImageButton) findViewById(R.id.btnNext);
        btnPrevious = (ImageButton) findViewById(R.id.btnPrevious);
        btnPlaylist = (ImageButton) findViewById(R.id.btnPlaylist);
        btnRepeat = (ImageButton) findViewById(R.id.btnRepeat);
        btnShuffle = (ImageButton) findViewById(R.id.btnShuffle);
        songProgressBar = (SeekBar) findViewById(R.id.songProgressBar);
        songTitleLabel = (TextView) findViewById(R.id.songTitle);
        songCurrentDurationLabel = (TextView) findViewById(R.id.songCurrentDurationLabel);
        songTotalDurationLabel = (TextView) findViewById(R.id.songTotalDurationLabel);

        // Media player
        mp = new MediaPlayer();
        songManager = new SongsManager();
        utils = new Utilities();

        // Listeners
        songProgressBar.setOnSeekBarChangeListener(this); // Important
        mp.setOnCompletionListener(this); // Important

        // Getting all songs list
        songsList = songManager.getPlayList();

        /*
          Button Click event for Play list click event
          Launches list activity which displays list of songs
          */
        btnPlaylist.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View arg0) {
                Intent i = new Intent(getApplicationContext(), PlayListActivity.class);
                startActivityForResult(i, 100);
            }
        });

        /*
          Forward button click event
          Forwards song specified seconds
          */
        btnForward.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View arg0) {
                // get current song position
                int currentPosition = mp.getCurrentPosition();
                // check if seekForward time is lesser than song duration
                if(currentPosition + seekForwardTime <= mp.getDuration()){
                    // forward song
                    mp.seekTo(currentPosition + seekForwardTime);
                }else{
                    // forward to end position
                    mp.seekTo(mp.getDuration());
                }
            }
        });

        /*
          Backward button click event
          Backward song to specified seconds
          */
        btnBackward.setOnClickListener(new View.OnClickListener() {

            @Override
            public void onClick(View arg0) {
                // get current song position
                int currentPosition = mp.getCurrentPosition();
                // check if seekBackward time is greater than 0 sec
                if(currentPosition - seekBackwardTime >= 0){
                    // forward song
                    mp.seekTo(currentPosition - seekBackwardTime);
                }else{
                    // backward to starting position
                    mp.seekTo(0);
                }

            }
        });

        /*
          Next button click event
          Plays next song by taking currentSongIndex + 1
          */
        btnNext.setOnClickListener(new View.OnClickListener() {

            @Override
            public void onClick(View arg0) {
                // check if next song is there or not
                if(currentSongIndex < (songsList.size() - 1)){
                    playSong(currentSongIndex + 1);
                    currentSongIndex = currentSongIndex + 1;
                }else{
                    // play first song
                    playSong(0);
                    currentSongIndex = 0;
                }

            }
        });

        /*
          Back button click event
          Plays previous song by currentSongIndex - 1
          */
        btnPrevious.setOnClickListener(new View.OnClickListener() {

            @Override
            public void onClick(View arg0) {
                if(currentSongIndex > 0){
                    playSong(currentSongIndex - 1);
                    currentSongIndex = currentSongIndex - 1;
                }else{
                    // play last song
                    playSong(songsList.size() - 1);
                    currentSongIndex = songsList.size() - 1;
                }

            }
        });

        /*
         * Button Click event for Repeat button
         * Enables repeat flag to true
         */
        btnRepeat.setOnClickListener(new View.OnClickListener() {

            @Override
            public void onClick(View arg0) {
                if(isRepeat){
                    isRepeat = false;
                    Toast.makeText(getApplicationContext(), "Repeat is OFF", Toast.LENGTH_LONG).show();
                    btnRepeat.setImageResource(R.drawable.btn_repeat);
                }else{
                    // make repeat to true
                    isRepeat = true;
                    Toast.makeText(getApplicationContext(), "Repeat is ON", Toast.LENGTH_LONG).show();
                    // make shuffle to false
                    isShuffle = false;
                    btnRepeat.setImageResource(R.drawable.btn_repeat);
                    btnShuffle.setImageResource(R.drawable.btn_shuffle);
                }
            }
        });

        /*
         * Button Click event for Shuffle button
         * Enables shuffle flag to true
         */
        btnShuffle.setOnClickListener(new View.OnClickListener() {

            @Override
            public void onClick(View arg0) {
                if(isShuffle){
                    isShuffle = false;
                    Toast.makeText(getApplicationContext(), "Shuffle is OFF", Toast.LENGTH_SHORT).show();
                    btnShuffle.setImageResource(R.drawable.btn_shuffle);
                }else{
                    // make repeat to true
                    isShuffle= true;
                    Toast.makeText(getApplicationContext(), "Shuffle is ON", Toast.LENGTH_SHORT).show();
                    // make shuffle to false
                    isRepeat = false;
                    btnShuffle.setImageResource(R.drawable.btn_shuffle);
                    btnRepeat.setImageResource(R.drawable.btn_repeat);
                }
            }
        });

    }


    //************Outside of onCreate()**************

     /**
     * Receiving song index from playlist view
     * and play the song
     **/
    @Override
    protected void onActivityResult(int requestCode,int resultCode, Intent data) {
        super.onActivityResult(requestCode, resultCode, data);
        if(resultCode == 100){
            currentSongIndex = data.getExtras().getInt("songIndex");
            // play selected song
            playSong(currentSongIndex);
        }

    }

    /**
     * Function to play a song
     * @param songIndex - index of song
     * */
    public void  playSong(int songIndex){
        // Play song
        try {
            mp.reset();
            mp.setDataSource(songsList.get(songIndex).get("songPath"));
            mp.prepare();
            mp.start();
            // Displaying Song title
            String songTitle = songsList.get(songIndex).get("songTitle");
            songTitleLabel.setText(songTitle);

            // Changing Button Image to pause image
            btnPlay.setImageResource(R.drawable.btn_pause);

            // set Progress bar values
            songProgressBar.setProgress(0);
            songProgressBar.setMax(100);

            // Updating progress bar
            updateProgressBar();
        } catch (IllegalArgumentException | IllegalStateException | IOException e) {
            e.printStackTrace();
        }
    }

     /**
     * Update timer on seekbar
     **/
     public void updateProgressBar() {
         mHandler.postDelayed(mUpdateTimeTask, 100);
     }

    /**
     * Background Runnable thread
     * */
    private Runnable mUpdateTimeTask = new Runnable() {
        public void run() {
            long totalDuration = mp.getDuration();
            long currentDuration = mp.getCurrentPosition();

            // Displaying Total Duration time
            songTotalDurationLabel.setText(utils.milliSecondsToTimer(totalDuration));
            // Displaying time completed playing
            songCurrentDurationLabel.setText(utils.milliSecondsToTimer(currentDuration));

            // Updating progress bar
            int progress = utils.getProgressPercentage(currentDuration, totalDuration);
            //Log.d("Progress", ""+progress);
            songProgressBar.setProgress(progress);

            // Running this thread after 100 milliseconds
            mHandler.postDelayed(this, 100);
        }
    };


    /*
     * On Song Playing completed
     * if repeat is ON play same song again
     * if shuffle is ON play random song
     */
    @Override
    public void onCompletion(MediaPlayer mp) {
        // check for repeat is ON or OFF
        if(isRepeat){
            // repeat is on play same song again
            playSong(currentSongIndex);
        } else if(isShuffle){
            // shuffle is on - play a random song
            Random rand = new Random();
            currentSongIndex = rand.nextInt((songsList.size() - 1) + 1);
            playSong(currentSongIndex);
        } else{
            // no repeat or shuffle ON - play next song
            if(currentSongIndex < (songsList.size() - 1)){
                playSong(currentSongIndex + 1);
                currentSongIndex = currentSongIndex + 1;
            }else{
                // play first song
                playSong(0);
                currentSongIndex = 0;
            }
        }
    }

    @Override
    public void onProgressChanged(SeekBar seekBar, int progress, boolean fromUser) {
    }

    /*
     * When user starts moving the progress handler
    */
    @Override
    public void onStartTrackingTouch(SeekBar seekBar) {
        // remove message Handler from updating progress bar
        mHandler.removeCallbacks(mUpdateTimeTask);
    }


    /*
     * When user stops moving the progress hanlder
    */
    @Override
    public void onStopTrackingTouch(SeekBar seekBar) {
        mHandler.removeCallbacks(mUpdateTimeTask);
        int totalDuration = mp.getDuration();
        int currentPosition = utils.progressToTimer(seekBar.getProgress(), totalDuration);

        // forward or backward to certain seconds
        mp.seekTo(currentPosition);

        // update timer progress again
        updateProgressBar();
    }
}

And complete error detail is(MainActivity.java:70):

EXCEPTION: main
  Process: ir.pluto.mediaplayer, PID: 28285
  java.lang.RuntimeException: Unable to start activity ComponentInfo{ir.pluto.mediaplayer/ir.pluto.mediaplayer.MainActivity}: java.lang.NullPointerException: Attempt to invoke virtual method 'void android.widget.SeekBar.setOnSeekBarChangeListener(android.widget.SeekBar$OnSeekBarChangeListener)' on a null object reference
      at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:3319)
      at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:3415)
      at android.app.ActivityThread.access$1100(ActivityThread.java:229)
      at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1821)
      at android.os.Handler.dispatchMessage(Handler.java:102)
      at android.os.Looper.loop(Looper.java:148)
      at android.app.ActivityThread.main(ActivityThread.java:7325)
      at java.lang.reflect.Method.invoke(Native Method)
      at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:1230)
      at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:1120)
   Caused by: java.lang.NullPointerException: Attempt to invoke virtual method 'void android.widget.SeekBar.setOnSeekBarChangeListener(android.widget.SeekBar$OnSeekBarChangeListener)' on a null object reference
      at ir.pluto.mediaplayer.MainActivity.onCreate(MainActivity.java:70)
      at android.app.Activity.performCreate(Activity.java:6904)
      at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1136)
      at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:3266)
      at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:3415) 
      at android.app.ActivityThread.access$1100(ActivityThread.java:229) 
      at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1821) 
      at android.os.Handler.dispatchMessage(Handler.java:102) 
      at android.os.Looper.loop(Looper.java:148) 
      at android.app.ActivityThread.main(ActivityThread.java:7325) 
      at java.lang.reflect.Method.invoke(Native Method) 
      at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:1230) 
      at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:1120) 

Design is under this File:

Player.xml

 <?xml version="1.0" encoding="utf-8"?>
    <RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:background="@drawable/player_background">          

            <!--another codes-->

        <!-- Progress Bar/Seek bar -->
        <SeekBar
            android:id="@+id/songProgressBar"
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:layout_above="@id/player_footer_bg"
            android:layout_marginBottom="1dp"
            android:layout_marginLeft="20dp"
            android:layout_marginRight="20dp"
            android:paddingLeft="6dp"
            android:paddingRight="6dp"
            android:progressDrawable="@drawable/seekbar_progress"
            android:thumb="@drawable/seekbar_thumb" />
<!--another codes-->                          
    </RelativeLayout

>


回答1:


this doesn't mean anything because before onCreate it will be null because Activity gets it's context(which you are referring as this ) from Application when onCreate get called so move your songProgressBar=new SeekBar(this); inside Oncreate

public class MainActivity extends Activity  implements MediaPlayer.OnCompletionListener {
    //..
    SeekBar songProgressBar;
    //..

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        songProgressBar=new SeekBar(this);

Update : you are initializing the songProgressBar reference from XML using findViewById so you don't need this part at all

songProgressBar=new SeekBar(this);

so do this

public class MainActivity extends Activity  implements MediaPlayer.OnCompletionListener {
    // .. code
    SeekBar songProgressBar;
    // .. code

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

        songProgressBar = (SeekBar) findViewById(R.id.songProgressBar);
        // .. code

Update 2 : Use setContentView(R.layout.Player); instead of setContentView(R.layout.activity_main); although it's you must rename XML file to lowercase letters



来源:https://stackoverflow.com/questions/42444928/unable-to-instantiate-activityattempt-to-invoke-virtual-method-android-content

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!