我尝试使用自己的自定义适配器填充片段中的ListView,而我似乎只是在运行时选择选项卡时才会出现此异常。单击“事件”选项卡时,导致此异常的主要原因是什么?
这是MyAdapter类
package com.lucasmikesoftware.messaround;
import android.content.Context;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ArrayAdapter;
import android.widget.ImageView;
import android.widget.TextView;
public class MyAdapter extends ArrayAdapter<String> {
public MyAdapter(Context context, String[] values) {
super(context, R.layout.events_past_list_item, values);
}
@Override
public View getView(int position, View convertView, ViewGroup parent) {
LayoutInflater theInflater = LayoutInflater.from(getContext());
View theView = theInflater.inflate(R.layout.events_past_list_item,parent,false);
String itemTitle = getItem(position);
TextView theTextView = (TextView) theView.findViewById(R.id.text_view);
theTextView.setText(itemTitle);
ImageView theImageView = (ImageView) theView.findViewById(R.id.image_view);
theImageView.setImageResource(R.drawable.icon_events);
return theView;
}
}
这是EventsPast类
package com.lucasmikesoftware.messaround;
import android.os.Bundle;
import android.support.v4.app.Fragment;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.AdapterView;
import android.widget.ListAdapter;
import android.widget.ListView;
public class EventsPast extends Fragment {
public EventsPast() {
// Required empty public constructor
}
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setHasOptionsMenu(true);
}
@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
View theView = inflater.inflate(R.layout.fragment_events_past, container, true);
String[] fakeData = {"Donald Trump is running for president","Twins trade for Clayton Kershaw",
"Derek Banas's Android Tutorial Series Helped Me Make This","This is some more fake news"};
ListView listView = (ListView) theView.findViewById(R.id.fragment_events_past_list_view);
ListAdapter adapter = new MyAdapter(getContext(),fakeData);
listView.setAdapter(adapter);
listView.setOnItemClickListener(new AdapterView.OnItemClickListener() {
@Override
public void onItemClick(AdapterView<?> adapterView, View view, int i, long l) {
}
});
// Inflate the layout for this fragment
return theView;
}
}
以下是xml文件 fragment_events_past.xml
<FrameLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
tools:context=".EventsPast">
<!-- TODO: Update blank fragment layout -->
<ListView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:id="@+id/fragment_events_past_list_view"
android:layout_gravity="left|top" />
</FrameLayout>
events_past_list_item.xml
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:orientation="horizontal"
android:layout_height="match_parent">
<ImageView
android:layout_width="100dp"
android:layout_height="100dp"
android:layout_marginLeft="5dp"
android:layout_marginRight="15dp"
android:id="@+id/image_view"
android:src="@drawable/icon_events"/>
<TextView
android:layout_width="match_parent"
android:layout_height="100dp"
android:textSize="16sp"
android:id="@+id/text_view"
android:gravity="center_vertical"/>
</LinearLayout>
错误讯息
07-30 16:38:14.888 22018-22018 / com.lucasmikesoftware.messaround D /错误:错误:堆栈= java.lang.StackOverflowError:堆栈大小8MB 在android.view.View.invalidateInternal(View.java:11749) 在android.view.View.invalidate(View.java:11711) 在android.view.View.invalidateDrawable(View.java:15845) 在android.graphics.drawable.Drawable.invalidateSelf(Drawable.java:367) 在android.graphics.drawable.RippleDrawable.jumpToCurrentState(RippleDrawable.java:221) 在android.widget.AbsListView.jumpDrawablesToCurrentState(AbsListView.java:2974) 在android.view.ViewGroup.jumpDrawablesToCurrentState(ViewGroup.java:5848) 在android.widget.FrameLayout.jumpDrawablesToCurrentState(FrameLayout.java:224) 在android.view.ViewGroup.jumpDrawablesToCurrentState(ViewGroup.java:5848) 在android.view.ViewGroup.jumpDrawablesToCurrentState(ViewGroup.java:5848) 在android.view.ViewGroup.jumpDrawablesToCurrentState(ViewGroup.java:5848) 在android.view.ViewGroup.jumpDrawablesToCurrentState(ViewGroup.java:5848) 在android.view.ViewGroup.jumpDrawablesToCurrentState(ViewGroup.java:5848) 在android.view.ViewGroup.jumpDrawablesToCurrentState(ViewGroup.java:5848) 在android.view.ViewGroup.jumpDrawablesToCurrentState(ViewGroup.java:5848) 在android.view.ViewGroup.jumpDrawablesToCurrentState(ViewGroup.java:5848) 在android.view.ViewGroup.jumpDrawablesToCurrentState(ViewGroup.java:5848) 在android.view.ViewGroup.jumpDrawablesToCurrentState(ViewGroup.java:5848) 在android.view.ViewGroup.jumpDrawablesToCurrentState(ViewGroup.java:5848) 在android.view.ViewGroup.jumpDrawablesToCurrentState(ViewGroup.java:5848) 在android.view.ViewGroup.jumpDrawablesToCurrentState(ViewGroup.java:5848) 在android.view.ViewGroup.jumpDrawablesToCurrentState(ViewGroup.java:5848) 在android.view.ViewGroup.jumpDrawablesToCurrentState(ViewGroup.java:5848) 在android.view.ViewGroup.jumpDrawablesToCurrentState(ViewGroup.java:5848) 在android.view.ViewGroup.jumpDrawablesToCurrentState(ViewGroup.java:5848) 在android.view.ViewGroup.jumpDrawablesToCurrentState(ViewGroup.java:5848) 在android.view.ViewGroup.jumpDrawablesToCurrentState(ViewGroup.java:5848) 在android.view.ViewGroup.jumpDrawablesToCurrentState(ViewGroup.java:5848) 在android.view.ViewGroup.jumpDrawablesToCurrentState(ViewGroup.java:5848) 在android.view.ViewGroup.jumpDrawablesToCurrentState(ViewGroup.java:5848) 在android.view.ViewGroup.jumpDrawablesToCurrentState(ViewGroup.java:5848) 在android.view.ViewGroup.jumpDrawablesToCurrentState(ViewGroup.java:5848) 在android.view.ViewGroup.jumpDrawablesToCurrentState(ViewGroup.java:5848) 在android.view.ViewGroup.jumpDrawablesToCurrentState(ViewGroup.java:5848) 在android.view.ViewGroup.jumpDrawablesToCurrentState(ViewGroup.java:5848) 在android.view.ViewGroup.jumpDrawablesToCurrentState(ViewGroup.java:5848) 在android.view.ViewGroup.jumpDrawablesToCurrentState(ViewGroup.java:5848) 在android.view.ViewGroup.jumpDrawablesToCurrentState(ViewGroup.java:5848) 在android.view.ViewGroup.jumpDrawablesToCurrentState(ViewGroup.java:5848) 在android.view.ViewGroup.jumpDrawablesToCurrentState(ViewGroup.java:5848) 在android.view.ViewGroup.jumpDrawablesToCurrentState(ViewGroup.java:5848) 在android.view.ViewGroup.jumpDrawablesToCurrentState(ViewGroup.java:5848) 在android.view.ViewGroup.jumpDrawablesToCurrentState(ViewGroup.java:5848) 在android.view.ViewGroup.jumpDrawablesToCurrentState(ViewGroup.java:5848) 在android.view.ViewGroup.jumpDrawablesToCurrentState(ViewGroup.java:5848) 在android.view.ViewGroup.jumpDrawablesToCurrentState(ViewGroup.java:5848) 在android.view.ViewGroup.jumpDrawablesToCurrentState(ViewGroup.java:5848) 在android.view.ViewGroup.jumpDrawablesToCurrentState(ViewGroup.java:5848) 在android.view.ViewGroup.jumpDrawablesToCurrentState(ViewGroup.java:5848) 在android.view.ViewGroup.jumpDrawablesToCurrentState(ViewGroup.java:5848) 在android.view.ViewGroup.jumpDrawablesToCurrentState(ViewGroup.java:5848) 在android.view.ViewGroup.jumpDrawablesToCurrentState(ViewGroup.java:5848) 在android.view.ViewGroup.jumpDrawablesToCurrentState(ViewGroup.java:5848) 在android.view.ViewGrou 07-30 16:38:14.898 22018-22053 / com.lucasmikesoftware.messaround I / art:背景部分并发标记扫描GC释放41(3024B)AllocSpace对象,1(22MB)LOS对象,14%免费,92MB / 108MB,暂停1.953ms总计215.717ms 07-30 16:38:14.908 22018-22018 / com.lucasmikesoftware.messaround E / JavaBinder:!!!失败的粘合剂交易! 07-30 16:38:14.908 22018-22018 / com.lucasmikesoftware.messaround D /错误:错误:总计写作:19877752 07-30 16:38:14.908 22018-22018 / com.lucasmikesoftware.messaround E / AndroidRuntime:错误报告崩溃 android.os.TransactionTooLargeException 在android.os.BinderProxy.transactNative(原生方法) 在android.os.BinderProxy.transact(Binder.java:504) 在android.app.ActivityManagerProxy.handleApplicationCrash(ActivityManagerNative.java:4179) 在com.android.internal.os.RuntimeInit $ UncaughtHandler.uncaughtException(RuntimeInit.java:118) at java.lang.ThreadGroup.uncaughtException(ThreadGroup.java:693) at java.lang.ThreadGroup.uncaughtException(ThreadGroup.java:690) 07-30 16:38:14.908 22018-22018 / com.lucasmikesoftware.messaround D / Process:killProcess,pid = 22018 07-30 16:38:14.918 22018-22018 / com.lucasmikesoftware.messaround D / Process:com.android.internal.os.RuntimeInit $ UncaughtHandler.uncaughtException:138 java.lang.ThreadGroup.uncaughtException:693 java.lang.ThreadGroup .uncaughtException:690
答案 0 :(得分:0)
使用
行在EventsPast.onCreateView
中对您的布局进行充气时
View theView = inflater.inflate(R.layout.fragment_events_past, container, true);
第三个参数boolean attachToRoot
应为false
。您不应该在通货膨胀时将新膨胀的布局附加到根(在这种情况下为container
),因为Fragment
旨在按照theView
的要求返回onCreateView
后执行此操作{1}}。
能够让您的代码使用此修改。