Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- 07-19 03:36:38.068 4150-4150/com.example.developer_4.testproject E/AndroidRuntime: FATAL EXCEPTION: main
- Process: com.example.developer_4.testproject, PID: 4150
- java.lang.ClassCastException: android.app.NotificationManager cannot be cast to android.content.Context
- at com.example.developer_4.testproject.First$1.onClick(First.java:42)
- at android.view.View.performClick(View.java:5637)
- at android.view.View$PerformClick.run(View.java:22429)
- at android.os.Handler.handleCallback(Handler.java:751)
- at android.os.Handler.dispatchMessage(Handler.java:95)
- at android.os.Looper.loop(Looper.java:154)
- at android.app.ActivityThread.main(ActivityThread.java:6119)
- at java.lang.reflect.Method.invoke(Native Method)
- at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:886)
- at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:776)
- public class First extends Fragment {
- Button notification;
- private NotificationManager nm;
- private final int NOTIFICATION_ID = 127;
- public View onCreateView(LayoutInflater inflater, @Nullable ViewGroup container, @Nullable Bundle savedInstanceState) {
- return inflater.inflate(R.layout.fragment_first, container, false);
- }
- @Override
- public void onViewCreated(@NonNull View view, @Nullable Bundle savedInstanceState) {
- super.onViewCreated(view, savedInstanceState);
- notification = view.findViewById(R.id.button3);
- nm = (NotificationManager) Objects.requireNonNull(getActivity()).getSystemService(NOTIFICATION_SERVICE);
- notification.setOnClickListener(new View.OnClickListener() {
- @Override
- public void onClick(View v) {
- Notification.Builder builder = new Notification.Builder((Context) (getActivity()).getSystemService(NOTIFICATION_SERVICE));
- //PendingIntent pendingIntent = new PendingIntent.getActivity(getActivity().getCo);
- builder
- //.setContentIntent(new PendingIntent())
- .setSmallIcon(R.drawable.ic_android_black_24dp)
- .setLargeIcon(BitmapFactory.decodeResource(getActivity().getResources(),R.drawable.ic_android_black_24dp))
- .setTicker("Test notification")
- .setWhen(System.currentTimeMillis())
- .setAutoCancel(true)
- .setContentTitle("Notification")
- .setContentText("Press please");
- Notification notification = builder.build();
- nm.notify(NOTIFICATION_ID,notification);
- Toast.makeText(getContext(), "1", Toast.LENGTH_SHORT).show();
- }
- });
- }
- }
Add Comment
Please, Sign In to add comment