カスタム通知を作成していますが、これまで通知を正常に実装していますが、作成した通知のボタンを使用してMainActivityから関数/私は以下のエラーを取得:MainAcitivtyメソッドを呼び出すと通知ボタンがクリックされます
以下System services not available to Activities before onCreate()
通知UIを更新し、また、メディアプレーヤを停止MainActivityの内側に私によって定義された方法です。
public void attachMediaActivity()
{
//INITIALIZE THE CONTEXT
context =this;
notificationManager=(NotificationManager) getSystemService(NOTIFICATION_SERVICE);
remoteViews=new RemoteViews(this.getPackageName(),R.layout.custom_notification);
remoteViews.setImageViewResource(R.id.notif_icon,R.drawable.stream_icon);
remoteViews.setTextViewText(R.id.notif_title,"stopped");
Intent button_intent= new Intent("button_clicked");
button_intent.putExtra("id",notification_id);
Intent notification_intent=new Intent(context,MainActivity.class);
PendingIntent pendingIntent=PendingIntent.getActivity(context,0,notification_intent,PendingIntent.FLAG_UPDATE_CURRENT);
builder =new NotificationCompat.Builder(context);
builder.setSmallIcon(R.mipmap.ic_launcher)
.setCustomBigContentView(remoteViews)
.setContentIntent(pendingIntent)
.setOngoing(true);
notificationManager.notify(notification_id,builder.build());
if (mediaPlayer.isPlaying())
{
mediaPlayer.stop();
}
}
ブロードキャストリスナーは、メインアクティビティから上記のメソッドを呼び出す通知ボタンにアタッチされます。
public class Button_listener extends BroadcastReceiver{
@Override
public void onReceive(Context context, Intent intent)
{
NotificationManager manager =(NotificationManager)context.getSystemService(Context.NOTIFICATION_SERVICE);
manager.cancel(intent.getExtras().getInt("id"));
Toast.makeText(context, "GENERATED BY NOTIFICATION", Toast.LENGTH_SHORT).show();
new MainActivity().attachMediaActivity();
}
}
LOGCAT:
01-24 11:52:29.010 13062-13062/com.amplitude.tron.samplemediaplayer E/AndroidRuntime: FATAL EXCEPTION: main
Process: com.amplitude.tron.samplemediaplayer, PID: 13062
java.lang.RuntimeException: Unable to start receiver com.amplitude.tron.samplemediaplayer.Button_listener: java.lang.IllegalStateException: System services not available to Activities before onCreate()
at android.app.ActivityThread.handleReceiver(ActivityThread.java:2750)
at android.app.ActivityThread.access$1800(ActivityThread.java:157)
at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1433)
at android.os.Handler.dispatchMessage(Handler.java:102)
at android.os.Looper.loop(Looper.java:148)
at android.app.ActivityThread.main(ActivityThread.java:5525)
at java.lang.reflect.Method.invoke(Native Method)
at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:730)
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:620)
Caused by: java.lang.IllegalStateException: System services not available to Activities before onCreate()
at android.app.Activity.getSystemService(Activity.java:5288)
at com.amplitude.tron.samplemediaplayer.MainActivity.attachMediaActivity(MainActivity.java:159)
at com.amplitude.tron.samplemediaplayer.Button_listener.onReceive(Button_listener.java:21)
at android.app.ActivityThread.handleReceiver(ActivityThread.java:2743)
at android.app.ActivityThread.access$1800(ActivityThread.java:157)
at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1433)
at android.os.Handler.dispatchMessage(Handler.java:102)
at android.os.Looper.loop(Looper.java:148)
at android.app.ActivityThread.main(ActivityThread.java:5525)
at java.lang.reflect.Method.invoke(Native Method)
at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:730)
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:620)
はどこ私はとしてgetPackageNameを()取得されています。..も前に呼び出し通知マネージャに間違っているつもりですNULL 助けてください!事前に感謝
あなたが 'Activity'をインスタンス化することはできません'new'とそれが正しく動作するようにしてください。 –
@MikeM。はい、私はMainActivityを呼び出す場合でも、そのonCreateが呼び出されていないことを知っています。それはなぜソリューション/アドバイスのためにこのクエリを投稿したのですか? – IteratioN7T
'new'でインスタンス化しようとすると' OnCreate'が呼び出されません – fluffyBatman