Avatar
0
quanganh Beginner
quanganh Beginner
getDeclaredMethod not work
Em chào mọi người.

Em sử dụng getDeclaredMethod để show toast nhưng khi run thì không thấy gì. Em chưa biết mình sai chỗ nào, mong mọi người giúp đỡ ạ.

Class toast = Class.forName("android.widget.Toast");
Object mToast = toast.newInstance();
Method make = toast.getDeclaredMethod("makeText", Context.class, CharSequence.class, int.class);
Method show = toast.getDeclaredMethod("show", null);

make.invoke(mToast, getBaseContext(), "Hello", Toast.LENGTH_LONG);
show.invoke(mToast, null);
  • Answer
Remain: 5
1 Answer
Avatar
tvd12 Beginner
tvd12 Beginner
Nó phải thế này em ạ

Class toast = Class.forName("android.widget.Toast");

// Object mToast = toast.newInstance(); không thể tạo được đối tượng vì Toast không có hàm tạo không đối
Method make = toast.getDeclaredMethod("makeText", Context.class, CharSequence.class, int.class);
Method show = toast.getDeclaredMethod("show", null);

Object toastInstance = make.invoke(null, getBaseContext(), "Hello", Toast.LENGTH_LONG);

show.invoke(toastInstance);
  • 1
  • Reply
Cảm ơn a nhiều ạ, để em thử  –  quanganh 1631572380000