Guest User

Untitled

a guest
Jul 19th, 2018
96
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 2.73 KB | None | 0 0
  1. 07-19 03:36:38.068 4150-4150/com.example.developer_4.testproject E/AndroidRuntime: FATAL EXCEPTION: main
  2. Process: com.example.developer_4.testproject, PID: 4150
  3. java.lang.ClassCastException: android.app.NotificationManager cannot be cast to android.content.Context
  4. at com.example.developer_4.testproject.First$1.onClick(First.java:42)
  5. at android.view.View.performClick(View.java:5637)
  6. at android.view.View$PerformClick.run(View.java:22429)
  7. at android.os.Handler.handleCallback(Handler.java:751)
  8. at android.os.Handler.dispatchMessage(Handler.java:95)
  9. at android.os.Looper.loop(Looper.java:154)
  10. at android.app.ActivityThread.main(ActivityThread.java:6119)
  11. at java.lang.reflect.Method.invoke(Native Method)
  12. at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:886)
  13. at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:776)
  14.  
  15. public class First extends Fragment {
  16. Button notification;
  17. private NotificationManager nm;
  18. private final int NOTIFICATION_ID = 127;
  19.  
  20.  
  21. public View onCreateView(LayoutInflater inflater, @Nullable ViewGroup container, @Nullable Bundle savedInstanceState) {
  22. return inflater.inflate(R.layout.fragment_first, container, false);
  23. }
  24.  
  25. @Override
  26. public void onViewCreated(@NonNull View view, @Nullable Bundle savedInstanceState) {
  27. super.onViewCreated(view, savedInstanceState);
  28. notification = view.findViewById(R.id.button3);
  29.  
  30. nm = (NotificationManager) Objects.requireNonNull(getActivity()).getSystemService(NOTIFICATION_SERVICE);
  31. notification.setOnClickListener(new View.OnClickListener() {
  32. @Override
  33. public void onClick(View v) {
  34. Notification.Builder builder = new Notification.Builder((Context) (getActivity()).getSystemService(NOTIFICATION_SERVICE));
  35. //PendingIntent pendingIntent = new PendingIntent.getActivity(getActivity().getCo);
  36. builder
  37. //.setContentIntent(new PendingIntent())
  38. .setSmallIcon(R.drawable.ic_android_black_24dp)
  39. .setLargeIcon(BitmapFactory.decodeResource(getActivity().getResources(),R.drawable.ic_android_black_24dp))
  40. .setTicker("Test notification")
  41. .setWhen(System.currentTimeMillis())
  42. .setAutoCancel(true)
  43. .setContentTitle("Notification")
  44. .setContentText("Press please");
  45.  
  46. Notification notification = builder.build();
  47. nm.notify(NOTIFICATION_ID,notification);
  48. Toast.makeText(getContext(), "1", Toast.LENGTH_SHORT).show();
  49. }
  50. });
  51. }
  52. }
Add Comment
Please, Sign In to add comment