编辑:附件是自定义适配器的代码:
public class ListAdapterFeed extends ArrayAdapter<String>
{
Context mContext;
public ListAdapterFeed(Context context, List<String> feedList){
super(context, R.layout.fragment_news_feed, feedList);
mContext = context;
}
@Override
public View getView(int position, View contentView , ViewGroup parent)
{
View row = contentView;
if (row == null){
row = ((Activity)mContext).getLayoutInflater().inflate(R.layout.fragment_news_feed, null);
}
// update your inner views value here
return row;
}
}
我现在在运行时得到的异常:
java.lang.ClassCastException:android.view.ContextThemeWrapper不能 被强制转换为android.app.Activity
我尝试使用ListFragment和ListView ... 但是我收到运行时错误告诉我:
java.lang.IllegalStateException: ArrayAdapter requires the resource ID to be a TextView
at android.widget.ArrayAdapter.createViewFromResource(ArrayAdapter.java:386)
at android.widget.ArrayAdapter.getView(ArrayAdapter.java:362)
at android.widget.AbsListView.obtainView(AbsListView.java:2627)
at android.widget.ListView.measureHeightOfChildren(ListView.java:1259)
at android.widget.ListView.onMeasure(ListView.java:1171)
at android.view.View.measure(View.java:16831)
at android.widget.RelativeLayout.measureChild(RelativeLayout.java:698)
at android.widget.RelativeLayout.onMeasure(RelativeLayout.java:494)
at android.view.View.measure(View.java:16831)
at android.support.v4.widget.DrawerLayout.onMeasure(DrawerLayout.java:666)
at android.view.View.measure(View.java:16831)
at android.view.ViewGroup.measureChildWithMargins(ViewGroup.java:5245)
at android.widget.FrameLayout.onMeasure(FrameLayout.java:310)
at android.view.View.measure(View.java:16831)
at android.view.ViewGroup.measureChildWithMargins(ViewGroup.java:5245)
at com.android.internal.widget.ActionBarOverlayLayout.onMeasure(ActionBarOverlayLayout.java:302)
at android.view.View.measure(View.java:16831)
at android.view.ViewGroup.measureChildWithMargins(ViewGroup.java:5245)
at android.widget.FrameLayout.onMeasure(FrameLayout.java:310)
at com.android.internal.policy.impl.PhoneWindow$DecorView.onMeasure(PhoneWindow.java:2586)
at android.view.View.measure(View.java:16831)
at android.view.ViewRootImpl.performMeasure(ViewRootImpl.java:2191)
at android.view.ViewRootImpl.measureHierarchy(ViewRootImpl.java:1354)
at android.view.ViewRootImpl.performTraversals(ViewRootImpl.java:1537)
at android.view.ViewRootImpl.doTraversal(ViewRootImpl.java:1251)
at android.view.ViewRootImpl$TraversalRunnable.run(ViewRootImpl.java:6379)
at android.view.Choreographer$CallbackRecord.run(Choreographer.java:791)
at android.view.Choreographer.doCallbacks(Choreographer.java:591)
at android.view.Choreographer.doFrame(Choreographer.java:561)
at android.view.Choreographer$FrameDisplayEventReceiver.run(Choreographer.java:777)
at android.os.Handler.handleCallback(Handler.java:730)
at android.os.Handler.dispatchMessage(Handler.java:92)
at android.os.Looper.loop(Looper.java:137)
at android.app.ActivityThread.main(ActivityThread.java:5493)
at java.lang.reflect.Method.invokeNative(Native Method)
at java.lang.reflect.Method.invoke(Method.java:525)
at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:1209)
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:1025)
at dalvik.system.NativeStart.main(Native Method)
Caused by: java.lang.ClassCastException: android.widget.RelativeLayout cannot be cast to android.widget.TextView
at android.widget.ArrayAdapter.createViewFromResource(ArrayAdapter.java:379)
at android.widget.ArrayAdapter.getView(ArrayAdapter.java:362)
at android.widget.AbsListView.obtainView(AbsListView.java:2627)
at android.widget.ListView.measureHeightOfChildren(ListView.java:1259)
at android.widget.ListView.onMeasure(ListView.java:1171)
at android.view.View.measure(View.java:16831)
at android.widget.RelativeLayout.measureChild(RelativeLayout.java:698)
at android.widget.RelativeLayout.onMeasure(RelativeLayout.java:494)
at android.view.View.measure(View.java:16831)
at android.support.v4.widget.DrawerLayout.onMeasure(DrawerLayout.java:666)
at android.view.View.measure(View.java:16831)
at android.view.ViewGroup.measureChildWithMargins(ViewGroup.java:5245)
at android.widget.FrameLayout.onMeasure(FrameLayout.java:310)
at android.view.View.measure(View.java:16831)
at android.view.ViewGroup.measureChildWithMargins(ViewGroup.java:5245)
at com.android.internal.widget.ActionBarOverlayLayout.onMeasure(ActionBarOverlayLayout.java:302)
at android.view.View.measure(View.java:16831)
at android.view.ViewGroup.measureChildWithMargins(ViewGroup.java:5245)
at android.widget.FrameLayout.onMeasure(FrameLayout.java:310)
at com.android.internal.policy.impl.PhoneWindow$DecorView.onMeasure(PhoneWindow.java:2586)
at android.view.View.measure(View.java:16831)
at android.view.ViewRootImpl.performMeasure(ViewRootImpl.java:2191)
at android.view.ViewRootImpl.measureHierarchy(ViewRootImpl.java:1354)
at android.view.ViewRootImpl.performTraversals(ViewRootImpl.java:1537)
at android.view.ViewRootImpl.doTraversal(ViewRootImpl.java:1251)
at android.view.ViewRootImpl$TraversalRunnable.run(ViewRootImpl.java:6379)
at android.view.Choreographer$CallbackRecord.run(Choreographer.java:791)
at android.view.Choreographer.doCallbacks(Choreographer.java:591)
at android.view.Choreographer.doFrame(Choreographer.java:561)
at android.view.Choreographer$FrameDisplayEventReceiver.run(Choreographer.java:777)
at android.os.Handler.handleCallback(Handler.java:730)
at android.os.Handler.dispatchMessage(Handler.java:92)
at android.os.Looper.loop(Looper.java:137)
at android.app.ActivityThread.main(ActivityThread.java:5493)
at java.lang.reflect.Method.invokeNative(Native Method)
at java.lang.reflect.Method.invoke(Method.java:525)
at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:1209)
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:1025)
at dalvik.system.NativeStart.main(Native Method)
我不想使用TextView,因为我想添加一个监听器,我想在整个过程中向视图中添加项目。
有什么想法吗?
答案 0 :(得分:0)
setListAdapter(new ArrayAdapter(getActivity(), R.layout.fragment_news_feed, rssFeed));
当崩溃日志显示ArrayAdapter
期望列表项至少有一个TextView
来更新rssFeed中的值。
您需要创建一个简单的自定义适配器,然后像这样覆盖getView()
@Override
public View getView(int position, View contentView , ViewGroup parent)
{
View row = contentView;
if (row == null){
row = mContext.getLayoutInflater().inflate(R.layout.fragment_news_feed, null);
}
// update your inner views value here
return row;
}
现在,您可以将自定义Adapter
设置为ListFragment
setAdapter(new CustomAdapter(context));