cannot be cast to android.app.fragment,片段中的适配器 - android.app.Application无法强制转换为android.app.Activity(...

片段中的适配器 - android.app.Application无法强制转换为android.app.Activity(adapter in fragment - android.app.Application cannot be cast to android.app.Activity)

所以现在我们有了

CustomAdapter adapter = new CustomAdapter(getActivity().getApplicationContext(), R.layout.list_style, RowBean_data);

ListView lista = (ListView) rootView.findViewById(R.id.lista);

lista.setAdapter(adapter);

在片段中,我们希望它显示包含项目和文本的列表。

其他课程:

我们怎样才能让它发挥作用?

so now we've got a

CustomAdapter adapter = new CustomAdapter(getActivity().getApplicationContext(), R.layout.list_style, RowBean_data);

ListView lista = (ListView) rootView.findViewById(R.id.lista);

lista.setAdapter(adapter);

in fragment, we want it to show a list with items and text.

Right now we're getting an error with the following code: http://pastebin.com/BV9X6Dys

The other classes:

How can we make it work?

原文:https://stackoverflow.com/questions/32523767

更新时间:2019-10-30 20:01

最满意答案

您正在将Context为Activity 。

错误在这里..

LayoutInflater inflater = ((Activity)context).getLayoutInflater();

因此,解决方案是使用context来获取充气机服务,就像这样

LayoutInflater inflater = (LayoutInflater)context.getSystemService(Context.LAYOUT_INFLATER_SERVICE);

另一种替代方式就是这样

LayoutInflater inflater = LayoutInflater.from(context);

就像@Zharf说的那样

编辑

另外我认为将getActivity()传递给CustomAdapter的构造函数就足够了。

CustomAdapter adapter = new CustomAdapter(getActivity(), R.layout.list_style, RowBean_data);

You are casting Context to Activity.

The error is here..

LayoutInflater inflater = ((Activity)context).getLayoutInflater();

So the solution is to use the context to get the inflator service, like this

LayoutInflater inflater = (LayoutInflater)context.getSystemService(Context.LAYOUT_INFLATER_SERVICE);

Another alternate way is that like this

LayoutInflater inflater = LayoutInflater.from(context);

Like @Zharf said

EDIT

Also I think that passing getActivity() to the CustomAdapter's constructor is enough.

CustomAdapter adapter = new CustomAdapter(getActivity(), R.layout.list_style, RowBean_data);

2015-09-11

相关问答

您正在将Context为Activity 。 错误在这里.. LayoutInflater inflater = ((Activity)context).getLayoutInflater();

因此,解决方案是使用context来获取充气机服务,就像这样 LayoutInflater inflater = (LayoutInflater)context.getSystemService(Context.LAYOUT_INFLATER_SERVICE);

另一种替代方式就是这样 Layout

...

您的“主”类不会扩展活动。 我只能看到你的清单和StackTrace。 Your "Main" class doesn't extend Activity. That's all I can see with only your manifest and StackTrace.

这里最小清单定义包,应用程序上下文和主要活动: <?xml version="1.0" encoding="utf-8"?>

package="my.app.package.name">

android:name=".MyCustomApplicationContext"

android

...

该错误指出getApplication返回的对象的类型是android.app.Application 。 可能的原因是您无法在清单中定义应用程序。 确保您的清单包含以下内容:

The error states that the type of the object returned by getApplication is android.app.Application. A possib

...

你正在咆哮错误的树;-)你的Service不应该知道你的用户界面,也不应该想知道它。 如果要从Service更改UI,则需要将该任务委派给Activity 。 Activity负责UI。 您可以使用几种记录良好的方法: Activity绑定到Service和Service在Activity上执行回调 Service发送广播Intent和Activity注册一个侦听器,该侦听器在广播时对Intent作出反应 使用Event Bus实现 You are barking up the wrong tre

...

你所做的不是使用activity的正确方法。如果你想要这个类应该在后台运行然后将它声明为Service .Activity有自己的生命周期,你必须遵循 。 What you did is not the correct way of using activity.If you want this class should be run in the background then declare it as Service.Activity is having its own life-cycle

...

我发现了一个更简单的方法 - 我传递了View变量并用Activity初始化代替它。 片段: BackgroundTaskJobs backgroundTaskJobs= new BackgroundTaskJobs(getActivity()); backgroundTaskJobs.execute(); BackgroundTaskJobs backgroundTaskJobs= new BackgroundTaskJobs(getActivity()); backgroundTaskJobs

...

如果您遵循Android命名约定,则错误非常有意义。 public class MainActivity extends Fragment {

片段不是活动,也不能这样生成。 改回来。 public class MainActivity extends AppCompatActivity {

并学习如何正确创建片段 。 If you followed Android naming conventions, then the error makes perfect sense. public

...

即使您在活动中,上下文也不总是活动。 它可以是一个应用程序,也可以是另一个上下文的包装器。 将Context转换为Activity几乎总是错误的。 如果你绝对需要一个,你应该传入一个Activity作为参数,而不是一个Context。 或者更好的是,直接传递支持片段管理器而不是活动,因为这就是你需要的全部内容。 A context is not always an Activity, even when you're in an Activity. It could be an Applicati

...

Activity扩展了ContextThemeWrapper因此导致了转换异常,因为ContextThemeWrapper不是Activity的实例。 它本身可能是另一个子类或ContextThemeWrapper 。 要在片段中获取Activity上下文,可以使用getActivity()而不是getView().getContext() Activity extends ContextThemeWrapper so the cast exception is caused because yo

...

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值