无法启动接收器java.lang.NullPointerException

时间:2014-02-13 22:54:53

标签: android android-widget android-mediaplayer

尝试创建一个小部件,从3中随机选择音频并播放小部件。如果错误“无法启动接收器”任何帮助都会很棒!

第40行= mp = MediaPlayer.create(context.getApplicationContext(),mfile [rnd.nextInt(NUM_SOUND_FILES)]);

代码:

public class MyWidget extends AppWidgetProvider {

  public static String ACTION_WIDGET_RECEIVER = "ActionReceiverWidget";
  private MediaPlayer mp;
  private final int NUM_SOUND_FILES = 3;  //*****REPLACE THIS WITH THE ACTUAL NUMBER OF SOUND FILES YOU HAVE*****
    private int mfile[] = new int[NUM_SOUND_FILES];
    private Random rnd = new Random();



  @Override
  public void onUpdate(Context context, AppWidgetManager appWidgetManager, int[] appWidgetIds) {
       RemoteViews remoteViews = new RemoteViews(context.getPackageName(), R.layout.widget);
       Intent active = new Intent(context, MyWidget.class);
       active.setAction(ACTION_WIDGET_RECEIVER);
       PendingIntent actionPendingIntent = PendingIntent.getBroadcast(context, 0, active, 0);
       remoteViews.setOnClickPendingIntent(R.id.pauseicon, actionPendingIntent);
       appWidgetManager.updateAppWidget(appWidgetIds, remoteViews);
  }

  @Override
  public void onReceive(Context context, Intent intent)
  {
      mfile[0] = R.raw.sound04;  //****REPLACE THESE WITH THE PROPER NAMES OF YOUR SOUND FILES
      mfile[1] = R.raw.sound05;  //PLACE THE SOUND FILES IN THE /res/raw/ FOLDER IN YOUR PROJECT*****
      mfile[2] = R.raw.sound06;

      if (mp == null)
          mp=MediaPlayer.create(context.getApplicationContext(), mfile[rnd.nextInt(NUM_SOUND_FILES)]);
      final String action = intent.getAction();

      if (ACTION_WIDGET_RECEIVER.equals(action)) {
           if (mp.isPlaying())
               mp.stop(); 
            else
                mp.start();

       }
      super.onReceive(context, intent);

 }

}

错误:

02-13 17:50:20.610: E/AndroidRuntime(2663): FATAL EXCEPTION: main
02-13 17:50:20.610: E/AndroidRuntime(2663): java.lang.RuntimeException: Unable to start receiver com.app.test: java.lang.NullPointerException
02-13 17:50:20.610: E/AndroidRuntime(2663):     at android.app.ActivityThread.handleReceiver(ActivityThread.java:2431)
02-13 17:50:20.610: E/AndroidRuntime(2663):     at     android.app.ActivityThread.access$1500(ActivityThread.java:141)
02-13 17:50:20.610: E/AndroidRuntime(2663):     at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1332)
02-13 17:50:20.610: E/AndroidRuntime(2663):     at android.os.Handler.dispatchMessage(Handler.java:99)
02-13 17:50:20.610: E/AndroidRuntime(2663):     at android.os.Looper.loop(Looper.java:137)
 02-13 17:50:20.610: E/AndroidRuntime(2663):    at android.app.ActivityThread.main(ActivityThread.java:5103)
 02-13 17:50:20.610: E/AndroidRuntime(2663):    at java.lang.reflect.Method.invokeNative(Native Method)
 02-13 17:50:20.610: E/AndroidRuntime(2663):    at java.lang.reflect.Method.invoke(Method.java:525)
 02-13 17:50:20.610: E/AndroidRuntime(2663):    at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:737)
 02-13 17:50:20.610: E/AndroidRuntime(2663):    at      com.android.internal.os.ZygoteInit.main(ZygoteInit.java:553)
 02-13 17:50:20.610: E/AndroidRuntime(2663):    at dalvik.system.NativeStart.main(Native Method)
 02-13 17:50:20.610: E/AndroidRuntime(2663): Caused by: java.lang.NullPointerException
 02-13 17:50:20.610: E/AndroidRuntime(2663):    at com.app.test.MyWidget.onReceive(MyWidget.java:40)
 02-13 17:50:20.610: E/AndroidRuntime(2663):    at android.app.ActivityThread.handleReceiver(ActivityThread.java:2424)
 02-13 17:50:20.610: E/AndroidRuntime(2663):    ... 10 more

2 个答案:

答案 0 :(得分:1)

首先看一下 - 在检查mp为null之前调用mp.reset()。

答案 1 :(得分:1)

context null @此行:

  mp=MediaPlayer.create(context.getApplicationContext(), mfile[rnd.nextInt(NUM_SOUND_FILES)]);
      final String action = intent.getAction();

尝试使用context

  mp=MediaPlayer.create(context, mfile[rnd.nextInt(NUM_SOUND_FILES)]);
      final String action = intent.getAction();

<强>更新

请参阅此示例,在/raw中创建文件夹/res并粘贴.mp3文件,此示例使用/res/raw/mysong.mp3文件。

private static MediaPlayer mp;



  @Override
      public void onReceive(Context context, Intent intent)
      {
          if (mp == null)
              mp = MediaPlayer.create(context.getApplicationContext(), R.raw.mysong);

          final String action = intent.getAction();

          if (ACTION_WIDGET_RECEIVER.equals(action)) {
               if (mp.isPlaying())
               {
                   mp.stop();
                   mp.release();
                   mp = MediaPlayer.create(context.getApplicationContext(), R.raw.mysong);

               }
                else
                    mp.start();

           }
          super.onReceive(context, intent);
         }
      }
              super.onReceive(context, intent);
    }


  @Override
  public void onUpdate(Context context, AppWidgetManager appWidgetManager, int[] appWidgetIds) {
       RemoteViews remoteViews = new RemoteViews(context.getPackageName(), R.layout.widget);
       Intent active = new Intent(context, HappyBabyWidget.class);
       active.setAction(ACTION_WIDGET_RECEIVER);
       PendingIntent actionPendingIntent = PendingIntent.getBroadcast(context, 0, active, 0);
       remoteViews.setOnClickPendingIntent(R.id.ButtonPlay, actionPendingIntent);
       appWidgetManager.updateAppWidget(appWidgetIds, remoteViews);
  }