試圖製作一個小部件,它將從3中挑選一個隨機音頻並使用小部件進行播放。 Getiing錯誤「無法啓動接收器」任何幫助將是偉大的!無法啓動接收器java.lang.NullPointerException
第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
你在哪裏初始化MP – Aiapaec
的錯誤是在進myWidget線40 – Aiapaec
是的,我知道,但不知道什麼不對的地方,感謝幫助壽:) – user2407147