java.lang.ClassCastException:android.app.ContextImpl不能强制转换为android.app.Activity

java.lang.ClassCastException:android.app.ContextImpl不能强制转换为android.app.Activity,android,android-fragments,crash,Android,Android Fragments,Crash,我有一个片段类,我希望它像Facebook提要一样。它从远程SQL数据库获取数据,并在运行时创建帖子的布局,并在片段中显示 有没有一个好的简单的方法 我试图通过使用片段中调用的BackgroundTaskJobs来实现这一点。 这是调用部分(从片段内部): 这是BackgroundTaskJobs的初始化: public BackgroundTaskJobs(Context ctx){ this.ctx = ctx; activity = (Activity) ctx; } pub

我有一个片段类,我希望它像Facebook提要一样。它从远程SQL数据库获取数据,并在运行时创建帖子的布局,并在片段中显示

有没有一个好的简单的方法

我试图通过使用片段中调用的
BackgroundTaskJobs
来实现这一点。 这是调用部分(从片段内部):

这是BackgroundTaskJobs的初始化:

public BackgroundTaskJobs(Context ctx){
   this.ctx = ctx;
   activity = (Activity) ctx;
}
public BackgroundTaskJobs(Context ctx, View view){
   this.ctx = ctx;
   this.view= view;
}
在运行时,此初始化会导致my up崩溃,并出现以下错误:

FATAL EXCEPTION: main
Process: com.example.admin.tabbedproject, PID: 27095
java.lang.ClassCastException: android.app.ContextImpl cannot be cast to android.app.Activity

为了解决这个问题,我可以做些什么?

我找到了一种更简单的方法——我传递了视图变量,并用活动初始化替换了它

片段:

BackgroundTaskJobs backgroundTaskJobs= new BackgroundTaskJobs(getActivity());
backgroundTaskJobs.execute(); 
背景任务工作:

public BackgroundTaskJobs(Context ctx){
   this.ctx = ctx;
   activity = (Activity) ctx;
}
public BackgroundTaskJobs(Context ctx, View view){
   this.ctx = ctx;
   this.view= view;
}
当我需要这个命令时:

recyclerView = (RecyclerView)activity.findViewById(R.id.recycler_view);
我用了这个:

recyclerView = (RecyclerView)view.findViewById(R.id.recycler_view);

移除
activity=(activity)ctx但我需要在以后使用它:
recyclerView=(recyclerView)activity.findViewById(R.id.recycler\u视图)