无法实例化活动:尝试调用虚拟方法android.content.Context.getResources() [英] Unable to instantiate activity:Attempt to invoke virtual method android.content.Context.getResources()

查看:77
本文介绍了无法实例化活动:尝试调用虚拟方法android.content.Context.getResources()的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我是 初学者 .

i am a beginner.

首先说我读过以前的文章,但没有回答.

我尝试测试音频播放器,但是当我尝试运行该应用程序时,由于对象引用为空,它立即崩溃.

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

这是Logcat:(此日志说:错误在Java代码的第28行中)

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)

我的Java代码的一部分是:

 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) {
            }...

第28行是:

SeekBar songProgressBar=new SeekBar(this);

谢谢您的回答...

通过@PavneetSingh回答更新我的代码后,发生了相同的错误再次,但这一次是在第70行,它是:

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();
    }
}

完整的错误详细信息是(MainActivity.java:70):

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

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

>

推荐答案

没什么意义,因为在 onCreate 之前它将为 null ,因为 Activity onCreate <时从 Application 获取它的 context (您指的是 this )./code>被调用,因此将 songProgressBar = new SeekBar(this); 移到 Oncreate

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);

更新:您正在使用 findViewById XML 初始化 songProgressBar 参考,因此您完全不需要这部分

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

songProgressBar=new SeekBar(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

更新2:使用 setContentView(R.layout.Player); 代替 setContentView(R.layout.activity_main); ,尽管您必须重命名 XML文件转换为小写字母

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

这篇关于无法实例化活动:尝试调用虚拟方法android.content.Context.getResources()的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
相关文章
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆