0

在 ListView 的适配器内膨胀视图时,我从用户那里得到了罕见的错误。LayoutInflater 的检索方式如下:

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

布局在这里被夸大了:

convertView = inflater.inflate(R.layout.bla_bla, null);

Stacktrace 指向由 LayoutInflater 在方法createView()中使用的错误上下文导致的 IllegalArgumentException

如何影响 LayoutInflater 使用的上下文?这是否取决于我是否使用应用程序上下文或活动上下文调用 getSystemService() 方法?

在我当前的代码中,activity 是从 SherlockFragmentActivity ActionBarSherlock 的类扩展而来的。

任何帮助表示赞赏,这是整个堆栈跟踪:

android.view.InflateException: Binary XML file line #57: Error inflating class <unknown>
at android.view.LayoutInflater.createView(LayoutInflater.java:619)
at android.view.LayoutInflater.createViewFromTag(LayoutInflater.java:693)
at android.view.LayoutInflater.rInflate(LayoutInflater.java:752)
at android.view.LayoutInflater.inflate(LayoutInflater.java:495)
at android.view.LayoutInflater.inflate(LayoutInflater.java:397)
at android.view.LayoutInflater.inflate(LayoutInflater.java:353)
at com.blabla.ui.fragments.main.Adapter.getView(Adapter.java:470)
at android.widget.AbsListView.obtainView(AbsListView.java:2613)
at android.widget.ListView.makeAndAddView(ListView.java:1838)
at android.widget.ListView.fillDown(ListView.java:681)
at android.widget.ListView.fillFromTop(ListView.java:742)
at android.widget.ListView.layoutChildren(ListView.java:1659)
at android.widget.AbsListView.onLayout(AbsListView.java:2431)
at android.view.View.layout(View.java:14482)
at android.view.ViewGroup.layout(ViewGroup.java:4469)
at android.widget.LinearLayout.setChildFrame(LinearLayout.java:1669)
at android.widget.LinearLayout.layoutVertical(LinearLayout.java:1527)
at android.widget.LinearLayout.onLayout(LinearLayout.java:1440)
at android.view.View.layout(View.java:14482)
at android.view.ViewGroup.layout(ViewGroup.java:4469)
at android.widget.FrameLayout.onLayout(FrameLayout.java:448)
at android.view.View.layout(View.java:14482)
at android.view.ViewGroup.layout(ViewGroup.java:4469)
at android.support.v4.view.ViewPager.onLayout(ViewPager.java:1585)
at android.view.View.layout(View.java:14482)
at android.view.ViewGroup.layout(ViewGroup.java:4469)
at android.widget.LinearLayout.setChildFrame(LinearLayout.java:1669)
at android.widget.LinearLayout.layoutVertical(LinearLayout.java:1527)
at android.widget.LinearLayout.onLayout(LinearLayout.java:1440)
at android.view.View.layout(View.java:14482)
at android.view.ViewGroup.layout(ViewGroup.java:4469)
at android.support.v4.widget.DrawerLayout.onLayout(DrawerLayout.java:653)
at android.view.View.layout(View.java:14482)
at android.view.ViewGroup.layout(ViewGroup.java:4469)
at android.widget.RelativeLayout.onLayout(RelativeLayout.java:1021)
at android.view.View.layout(View.java:14482)
at android.view.ViewGroup.layout(ViewGroup.java:4469)
at android.widget.FrameLayout.onLayout(FrameLayout.java:448)
at android.view.View.layout(View.java:14482)
at android.view.ViewGroup.layout(ViewGroup.java:4469)
at android.widget.LinearLayout.setChildFrame(LinearLayout.java:1669)
at android.widget.LinearLayout.layoutVertical(LinearLayout.java:1527)
at android.widget.LinearLayout.onLayout(LinearLayout.java:1440)
at android.view.View.layout(View.java:14482)
at android.view.ViewGroup.layout(ViewGroup.java:4469)
at android.widget.FrameLayout.onLayout(FrameLayout.java:448)
at android.view.View.layout(View.java:14482)
at android.view.ViewGroup.layout(ViewGroup.java:4469)
at android.view.ViewRootImpl.performLayout(ViewRootImpl.java:2151)
at android.view.ViewRootImpl.performTraversals(ViewRootImpl.java:1970)
at android.view.ViewRootImpl.doTraversal(ViewRootImpl.java:1183)
at android.view.ViewRootImpl$TraversalRunnable.run(ViewRootImpl.java:4863)
at android.view.Choreographer$CallbackRecord.run(Choreographer.java:749)
at android.view.Choreographer.doCallbacks(Choreographer.java:562)
at android.view.Choreographer.doFrame(Choreographer.java:532)
at android.view.Choreographer$FrameDisplayEventReceiver.run(Choreographer.java:735)
at android.os.Handler.handleCallback(Handler.java:725)
at android.os.Handler.dispatchMessage(Handler.java:92)
at android.os.Looper.loop(Looper.java:137)
at android.app.ActivityThread.main(ActivityThread.java:5328)
at java.lang.reflect.Method.invokeNative(Native Method)
at java.lang.reflect.Method.invoke(Method.java:511)
at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:1102)
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:869)
at dalvik.system.NativeStart.main(Native Method)
Caused by: java.lang.IllegalArgumentException: argument 1 should have type android.content.Context, got com.blabla.ui.fragments.main.MainScreen
at java.lang.reflect.Constructor.constructNative(Native Method)
at java.lang.reflect.Constructor.newInstance(Constructor.java:417)
at android.view.LayoutInflater.createView(LayoutInflater.java:593)
... 64 more
4

2 回答 2

3

如何影响 LayoutInflater 使用的上下文?

从不使用开始getSystemService()。调用. getLayoutInflater()_Activity

这是否取决于我是否使用应用程序上下文或活动上下文调用 getSystemService() 方法?

是的。

在我当前的代码中,activity 是从 SherlockFragmentActivity ActionBarSherlock 的类扩展而来的。

你应该使用更多的理由getLayoutInflater(),这样你才能得到正确的结果。

于 2013-09-06T15:01:12.400 回答
-1

使用covertview=inflated.inflate(getApplicationcontext(),layoutresource,null)。

于 2013-09-06T15:15:19.693 回答