我有一段代码试图查询YouTube数据API以获取YouTube视频的MetaData,并使用videoID作为参数。为了能够访问YouTube Data API服务器,该程序执行强加的事务和权限。 为了能够访问YouTube数据API,代码会调用一个对话窗口,其中包含以下消息:"此应用需要您的Google帐户(通过通讯录)":
public class LMMoviesMainActivity extends AppCompatActivity
implements EasyPermissions.PermissionCallbacks {...
// Relevant part of the method of the Interface: EasyPermissions:
EasyPermissions.requestPermissions(
this,
"This app needs to access your Google account (via Contacts).",
REQUEST_PERMISSION_GET_ACCOUNTS,
Manifest.permission.GET_ACCOUNTS);
窗口有“确定”和“取消”按钮。按下确定后,应用程序崩溃。 LogCat是:
11-18 10:27:08.460 4566-4566/com.languagematerial.lmmovies E/AndroidRuntime: FATAL EXCEPTION: main
Process: com.languagematerial.lmmovies, PID: 4566
java.lang.ClassCastException: com.languagematerial.lmmovies.LMMoviesMainActivity cannot be cast to android.support.v4.app.FragmentActivity
at pub.devrel.easypermissions.RationaleDialogClickListener.onClick(RationaleDialogClickListener.java:57)
at android.support.v7.app.AlertController$ButtonHandler.handleMessage(AlertController.java:162)
at android.os.Handler.dispatchMessage(Handler.java:102)
at android.os.Looper.loop(Looper.java:148)
at android.app.ActivityThread.main(ActivityThread.java:5417)
at java.lang.reflect.Method.invoke(Native Method)
at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:726)
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:616)
11-18 10:27:08.461 1642-2985/system_process W/ActivityManager: Force finishing activity com.languagematerial.lmmovies/.LMMoviesMainActivity
11-18 10:27:08.463 1259-1720/? D/PermissionCache: checking android.permission.READ_FRAME_BUFFER for uid=1000 => granted (109 us)
11-18 10:27:08.463 1259-1720/? D/gralloc_ranchu: gralloc_alloc: Creating ashmem region of size 520192
[ 11-18 10:27:08.466 1642: 2985 D/ ]
HostConnection::get() New Host Connection established 0x9cb5b9c0, tid 2985
11-18 10:27:08.484 1259-1720/? E/SurfaceFlinger: ro.sf.lcd_density must be defined as a build property
11-18 10:27:08.484 1642-4704/system_process D/OpenGLRenderer: Use EGL_SWAP_BEHAVIOR_PRESERVED: true
行:RationaleDialogClickListener.java指的是一个名为pub.devrel:easypermissions-0.3.0
的库的文件。第57行中该库文件中的代码是:
ActivityCompat.requestPermissions((FragmentActivity) mHost, mConfig.permissions, mConfig.requestCode);
此RationaleDialogClickListener.java文件无法修改,因为它只能是可读的。即使我可以修改它,我也不知道要放什么!
此外,我自己的应用程序从不使用片段:调用代码位于MainActivity中(如果事实可能相关)。
这种Cast错误有类似的情况,但没有一个有令人满意的答案。有关如何进行的任何想法或建议?感谢