执行MediaPlayer.start()方法时出错

时间:2016-07-11 08:12:39

标签: java android android-mediaplayer

我总是收到这个错误: -

m.example.android.miwok E/AndroidRuntime: FATAL EXCEPTION: main
Process: com.example.android.miwok, PID: 23463
java.lang.NullPointerException: Attempt to invoke virtual method 'void android.media.MediaPlayer.start()' on a null object reference
   at com.example.android.miwok.NumbersActivity$1.onItemClick(NumbersActivity.java:46)
   at android.widget.AdapterView.performItemClick(AdapterView.java:310)
   at android.widget.AbsListView.performItemClick(AbsListView.java:1188)
   at android.widget.AbsListView$PerformClick.run(AbsListView.java:3231)
   at android.widget.AbsListView.onTouchUp(AbsListView.java:4176)
   at android.widget.AbsListView.onTouchEvent(AbsListView.java:3887)
   at android.view.View.dispatchTouchEvent(View.java:8593)
   at android.view.ViewGroup.dispatchTransformedTouchEvent(ViewGroup.java:2513)
   at android.view.ViewGroup.dispatchTouchEvent(ViewGroup.java:2157)
   at android.view.ViewGroup.dispatchTransformedTouchEvent(ViewGroup.java:2519)
   at android.view.ViewGroup.dispatchTouchEvent(ViewGroup.java:2171)
   at android.view.ViewGroup.dispatchTransformedTouchEvent(ViewGroup.java:2519)
   at android.view.ViewGroup.dispatchTouchEvent(ViewGroup.java:2171)
   at android.view.ViewGroup.dispatchTransformedTouchEvent(ViewGroup.java:2519)
   at android.view.ViewGroup.dispatchTouchEvent(ViewGroup.java:2171)
   at android.view.ViewGroup.dispatchTransformedTouchEvent(ViewGroup.java:2519)
   at android.view.ViewGroup.dispatchTouchEvent(ViewGroup.java:2171)
   at android.view.ViewGroup.dispatchTransformedTouchEvent(ViewGroup.java:2519)
   at android.view.ViewGroup.dispatchTouchEvent(ViewGroup.java:2171)
   at com.android.internal.policy.impl.PhoneWindow$DecorView.superDispatchTouchEvent(PhoneWindow.java:2484)
   at com.android.internal.policy.impl.PhoneWindow.superDispatchTouchEvent(PhoneWindow.java:1800)
   at android.app.Activity.dispatchTouchEvent(Activity.java:2797)
   at android.support.v7.view.WindowCallbackWrapper.dispatchTouchEvent(WindowCallbackWrapper.java:60)
   at com.android.internal.policy.impl.PhoneWindow$DecorView.dispatchTouchEvent(PhoneWindow.java:2445)
   at android.view.View.dispatchPointerEvent(View.java:8799)
   at android.view.ViewRootImpl$ViewPostImeInputStage.processPointerEvent(ViewRootImpl.java:4662)
   at android.view.ViewRootImpl$ViewPostImeInputStage.onProcess(ViewRootImpl.java:4520)
   at android.view.ViewRootImpl$InputStage.deliver(ViewRootImpl.java:4034)
   at android.view.ViewRootImpl$InputStage.onDeliverToNext(ViewRootImpl.java:4087)
   at android.view.ViewRootImpl$InputStage.forward(ViewRootImpl.java:4053)
   at android.view.ViewRootImpl$AsyncInputStage.forward(ViewRootImpl.java:4190)
   at android.view.ViewRootImpl$InputStage.apply(ViewRootImpl.java:4061)
   at android.view.ViewRootImpl$AsyncInputStage.apply(ViewRootImpl.java:4247)
   at android.view.ViewRootImpl$InputStage.deliver(ViewRootImpl.java:4034)
   at android.view.ViewRootImpl$InputStage.onDeliverToNext(ViewRootImpl.java:4087)
   at android.view.ViewRootImpl$InputStage.forward(ViewRootImpl.java:4053)
   at android.view.ViewRootImpl$InputStage.apply(ViewRootImpl.java:4061)
   at android.view.ViewRootImpl$InputStage.deliver(ViewRootImpl.java:4034)
   at android.view.ViewRootImpl.deliverInputEvent(ViewRootImpl.java:6463)
   at android.view.ViewRootImpl.doProcessInputEvents(ViewRootImpl.java:6437)
   at android.view.ViewRootImpl.enqueueInputEvent(ViewRootImpl.java:6390)
   at android.view.ViewRootImpl$WindowInputEventReceiver.onInputEvent(ViewRootImpl.java:6622)
   at android.view.InputEventReceiver.dispatchInputEvent(InputEventReceiver.java:185)
   at android.os.MessageQueue.nativePollOnce(Native Method)
   at android.os.MessageQueue.next(MessageQueue.java:148)
   at android.os.Looper.loop(Looper.java:151)
   at android.app.ActivityThread.main(ActivityThread.java:5649)
   at java.lang.reflect.Method.invoke(Native Method)
   at java.lang.reflect.Method.invoke(Method.java:372)
   at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:959)
   at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:754)

这是我的代码(NumbersActivity): -

package com.example.android.miwok;
import android.media.MediaPlayer;
import android.os.Bundle;
import android.support.v7.app.AppCompatActivity;
import android.view.View;
import android.widget.AdapterView;
import android.widget.ListView;

import java.util.ArrayList;

public class NumbersActivity extends AppCompatActivity{

    private MediaPlayer mediaplayer;

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

        final ArrayList<word> Word = new ArrayList<>();
        Word.add(new word("One","Lutti",R.drawable.number_one, R.raw.number_one));
        Word.add(new word("Two","Otiiko",R.drawable.number_two, R.raw.number_two));
        Word.add(new word("Three","Tolookosu",R.drawable.number_three, R.raw.number_three));
        Word.add(new word("Four","Oyyisa",R.drawable.number_four, R.raw.number_four));
        Word.add(new word("Five","Massokka",R.drawable.number_five, R.raw.number_five));
        Word.add(new word("Six","Temmokka",R.drawable.number_six, R.raw.number_six));
        Word.add(new word("Seven","Kenekaku",R.drawable.number_seven, R.raw.number_seven));
        Word.add(new word("Eight","Kawinta",R.drawable.number_eight, R.raw.number_eight));
        Word.add(new word("Nine","Wo’e",R.drawable.number_nine, R.raw.number_nine));
        Word.add(new word("Ten","Na’aacha",R.drawable.number_ten, R.raw.number_ten));

        WordAdapter itemsAdapter = new WordAdapter(this, Word,R.color.category_numbers);
        ListView listView = (ListView) findViewById(R.id.list_num);
        listView.setAdapter(itemsAdapter);

        listView.setOnItemClickListener
                (new AdapterView.OnItemClickListener()
                 {
                     @Override
                     public void onItemClick(AdapterView<?> adapterView, View view, int i, long l)
                     {
                         word Woord = Word.get(i);
                         mediaplayer.create(NumbersActivity.this, Woord.getAudioSRC());
                         mediaplayer.start();
                     }

                 }
                );
    }
}

word.java的代码: -

package com.example.android.miwok;


public class word
{

    private String mDefaultTranslation;
    private String mMiwokTranslation;
    private int mAudio;
    private int mImage = NO_IMAGE_PROVIDED ;
    private static final int NO_IMAGE_PROVIDED = -1;

    public word(String mdefaultTranslation, String mmiwokTranslation, int mimage, int maudio)
    {
        mDefaultTranslation = mdefaultTranslation;
        mMiwokTranslation = mmiwokTranslation;
        mImage = mimage;
        mAudio = maudio;
    }

    public word(String mdefaultTranslation, String mmiwokTranslation, int maudio)
    {
        mDefaultTranslation = mdefaultTranslation;
        mMiwokTranslation = mmiwokTranslation;
        mAudio = maudio;
    }

    public String getDefaultTranslation()
    {
       return(mDefaultTranslation);
    }

    public String getMiwokTranslation()
    {
        return(mMiwokTranslation);
    }

    public int getImageSRC() {return(mImage);}

    public boolean hasImage()
    {
        return(mImage != NO_IMAGE_PROVIDED);
    }

    public int getAudioSRC() {return(mAudio);}

}
WordAdapter的

代码: -

package com.example.android.miwok;

import android.app.Activity;
import android.support.v4.content.ContextCompat;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ArrayAdapter;
import android.widget.ImageView;
import android.widget.TextView;

import java.util.ArrayList;

/**
* Created by chhotu lal jain on 09/07/2016.
*/
public class WordAdapter extends ArrayAdapter<word>
{
int ColorPath = -1;
public WordAdapter(Activity context, ArrayList<word> Wordss, int colorPath)
{
    super(context, 0, Wordss);
    ColorPath = colorPath;
}

@Override
public View getView(int position, View convertView, ViewGroup parent)
{
    word currentword = getItem(position);

    View VconvertView = convertView;
    if(VconvertView == null)
    {
        VconvertView = LayoutInflater.from(getContext()).inflate(R.layout.list_item, parent, false);
    }

    TextView TV1 = (TextView) VconvertView.findViewById(R.id.miwok_text_view);
    TV1.setText(currentword.getMiwokTranslation());

    TV1 = (TextView) VconvertView.findViewById(R.id.default_text_view);
    TV1.setText(currentword.getDefaultTranslation());

    ImageView IV1 = (ImageView) VconvertView.findViewById(R.id.Image_View);

    if(currentword.hasImage())
    {
        IV1.setImageResource(currentword.getImageSRC());
        IV1.setVisibility(View.VISIBLE);
    }
    else
    {
        IV1.setVisibility(View.GONE);
    }

    View textContainer = VconvertView.findViewById(R.id.text_container);
    int color = ContextCompat.getColor(getContext(), ColorPath);
    textContainer.setBackgroundColor(color);

    return VconvertView;


}
}

我认为不需要其他活动和java文件。

*音频文件的所有ID都是100%正确。

1 个答案:

答案 0 :(得分:0)

替换:

 mediaplayer.create(NumbersActivity.this, Woord.getAudioSRC());
 mediaplayer.start();

mediaplayer =  Mediaplayer.create(NumbersActivity.this, Woord.getAudioSRC());
mediaplayer.start();