我正在尝试使用片段中的部分实现ListView,但它总是会在我的片段(第3页)中的同一点崩溃,应用程序在返回rootView时崩溃并且在调试时间过长之后
在我最近的尝试之下(受到我在互联网上找到的教程的启发): 我的相关xml文件:
item_view_stocks.xml
(我的列表视图中的项目):
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout
xmlns:android="http://schemas.android.com/apk/res/android"
android:orientation="vertical"
android:layout_width="match_parent"
android:layout_height="match_parent">
<TextView
android:text="TextView"
android:layout_width="69dp"
android:layout_height="wrap_content"
android:id="@+id/tv_lasttrade"
android:layout_alignParentTop="true"
android:layout_centerHorizontal="true"
android:paddingTop="6dp"
android:textSize="16sp"
android:textAlignment="viewEnd"
android:paddingBottom="6dp" />
<TextView
android:text="TextView"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:id="@+id/tv_stockchangepercentage"
android:layout_alignParentTop="true"
android:layout_alignParentEnd="true"
android:paddingTop="6dp"
android:paddingRight="6dp"
android:textSize="16sp" />
<TextView
android:text="TextView"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:id="@+id/tv_lasttradetime"
android:layout_below="@+id/tv_stockname"
android:layout_alignStart="@+id/tv_lasttrade"
android:textAlignment="viewEnd"
android:layout_alignEnd="@+id/tv_lasttrade"
android:textColor="@color/light_text"
android:paddingTop="2dp"
android:textSize="15sp" />
<TextView
android:text="TextView"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:id="@+id/tv_stockchange"
android:layout_alignBaseline="@+id/tv_lasttradetime"
android:layout_alignBottom="@+id/tv_lasttradetime"
android:layout_alignParentEnd="true"
android:textSize="16sp"
android:paddingRight="6dp" />
<TextView
android:text="TextView"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:id="@+id/tv_stockname"
android:gravity="left"
android:foregroundTint="@color/colorPrimary"
android:textColor="@color/colorPrimary"
android:textSize="18sp"
android:paddingTop="6dp"
android:paddingLeft="6dp"
android:layout_alignParentTop="true"
android:layout_alignParentStart="true" />
</RelativeLayout>
item_view_stocks_separator.xml(列表视图中我的分隔符项的xml文件):
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:orientation="vertical" android:layout_width="match_parent"
android:layout_height="match_parent">
<TextView
android:text="TextView"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:id="@+id/list_item_section_text"
android:paddingBottom="6dp"
android:paddingLeft="6dp"
android:paddingTop="6dp"
android:textSize="15sp"
android:textColor="@android:color/black"
android:backgroundTint="?android:attr/colorAccent"
android:background="@color/colorAccent" />
</LinearLayout>
fragment_page3.xml(我的ListFragment的xml文件):
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout
xmlns:android="http://schemas.android.com/apk/res/android"
android:orientation="vertical"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:background="#dcdcdc">
<ListView
android:layout_width="match_parent"
android:layout_height="match_parent"
android:id="@android:id/list"/>
</LinearLayout>
这里是我的CustomAdapter的JavaCode(这里称为StockAdapter):
public class StockAdapter extends BaseAdapter {
private static final int TYPE_ITEM = 0;
private static final int TYPE_SEPARATOR = 1;
private ArrayList<String> mData = new ArrayList<String>();
private TreeSet<Integer> sectionHeader = new TreeSet<Integer>();
private LayoutInflater mInflater;
public StockAdapter(Context context){
mInflater = (LayoutInflater) context.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
}
public void addItem(final String item){
mData.add(item);
notifyDataSetChanged();
}
public void addSectionHeaderItem(final String item){
mData.add(item);
sectionHeader.add(mData.size()-1);
notifyDataSetChanged();
}
@Override
public int getItemViewType(int position) {
return sectionHeader.contains(position) ? TYPE_SEPARATOR : TYPE_ITEM;
}
@Override
public int getViewTypeCount() {
return 2;
}
@Override
public int getCount() {
return mData.size();
}
@Override
public String getItem(int position) {
return mData.get(position);
}
@Override
public long getItemId(int position) {
return position;
}
public View getView(int position, View convertView, ViewGroup parent) {
ViewHolder holder = null;
int rowType = getItemViewType(position);
if (convertView == null) {
holder = new ViewHolder();
switch (rowType) {
case TYPE_ITEM:
convertView = mInflater.inflate(R.layout.item_view_stocks, null);
holder.textView = (TextView) convertView.findViewById(R.id.text);
break;
case TYPE_SEPARATOR:
convertView = mInflater.inflate(R.layout.item_view_stocks_separator, null);
holder.textView = (TextView) convertView.findViewById(R.id.list_item_section_text);
break;
}
convertView.setTag(holder);
} else {
holder = (ViewHolder) convertView.getTag();
}
holder.textView.setText(mData.get(position));
return convertView;
}
public static class ViewHolder {
public TextView textView;
}
}
现在我的Page3.java(它的ListFragment):
public class Page3 extends ListFragment {
private StockAdapter mAdapter;
@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
View rootView = inflater.inflate(R.layout.fragment_page3, container, false);
mAdapter = new StockAdapter(this.getContext());
for (int i = 1; i < 30; i++) {
mAdapter.addItem("Row Item #" + i);
if (i % 4 == 0) {
mAdapter.addSectionHeaderItem("Section #" + i);
}
}
setListAdapter(mAdapter);
return rootView;
}
}
我非常感谢你能给我的每一个帮助和建议。 提前谢谢。
修改
我的logcat只是说:
02-28 17:27:33.647 27910-27910 / com.example(...)E / Dialog:show mWindowManager.addView RuntimeException
修改 我的logcat看起来像这样:[logcat]
我很抱歉,但我无法向您展示任何Stacktrace /: