我有两个三星平板电脑(三星SM-T580 Android 7.0和SM-T280 Android 5.1)。 我有一个带有自定义适配器的列表视图。它在SM-T580上运行良好,但在方法的第一次迭代时崩溃了:
public override View GetView(int position, View convertView, ViewGroup parent)
在SM-T280平板电脑上。我不明白它如何在SM-T580上完美运行并且在SM-T280上崩溃。崩溃时的错误消息是:
应用程序处于中断模式。
您的应用已进入中断状态,但没有要显示的代码,因为所有线程都在执行外部代码(通常是系统或框架代码。)
我将编译器设置为:
Compile using Android version: (Target Framework)
Android 5.1 (Lollipop)
我正在使用Visual Studio 2017和Xamarin。
以下是我的代码。
布局代码:
<?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">
<TableLayout
android:minWidth="25px"
android:minHeight="25px"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:stretchColumns="2"
android:id="@+id/tableLayout1">
<TableRow
android:id="@+id/tableRow1"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:orientation="horizontal"
android:layout_weight="1">
<TextView
android:id="@+id/CompanyNameLabel"
android:text=""
android:layout_width="match_parent"
android:layout_height="match_parent"
android:layout_weight="1"
android:layout_column="1"
android:layout_gravity="left"
android:textStyle="bold"
android:textSize="16sp"
android:padding="8dip" />
<TextView
android:id="@+id/CompanySalesLabel"
android:text=""
android:layout_width="match_parent"
android:layout_height="match_parent"
android:layout_weight="1"
android:layout_column="2"
android:layout_gravity="right"
android:padding="8dip" />
</TableRow>
</TableLayout>
<TextView
android:id="@+id/CompanyAddressLabel"
android:text=""
android:layout_width="match_parent"
android:layout_height="match_parent"
android:paddingLeft="20dip" />
<TextView
android:id="@+id/CompanyCityStateZipLabel"
android:text=""
android:layout_width="match_parent"
android:layout_height="match_parent"
android:paddingLeft="20dip" />
</LinearLayout>
视图持有人代码:
public class MyMainCompanyRowHolder : Java.Lang.Object
{
public TextView CompanyName { get; set; }
public TextView CompanyAddress { get; set; }
public TextView CompanySales { get; set; }
public TextView CompanyCityStateZipLabel { get; set; }
public static T Cast<T>(Java.Lang.Object obj) where T : MyMainCompanyRowHolder
{
var propInfo = obj.GetType().GetProperty("Instance");
return propInfo == null ? null : propInfo.GetValue(obj, null) as T;
}
}
自定义适配器:
public class ListAdapter : BaseAdapter<Company>
{
List<Company> company;
Activity context;
public ListAdapter(Activity currentContext, List<Company> companyList) : base()
{
this.company = companyList;
this.context = currentContext;
}
public override Company this[int position]
{
get { return company.ToArray()[position]; }
}
public override int Count
{
get { return company.Count; }
}
public override long GetItemId(int position)
{
return position;
}
public override View GetView(int position, View convertView, ViewGroup parent)
{
Models.ViewHolder.MyMainCompanyRowHolder myViewHolder = null;
View view = convertView;
if (view != null)
myViewHolder = Models.ViewHolder.MyMainCompanyRowHolder.Cast<Models.ViewHolder.MyMainCompanyRowHolder>(view.Tag);
if (myViewHolder == null)
{
myViewHolder = new Models.ViewHolder.MyMainCompanyRowHolder();
//view = context.LayoutInflater.Inflate(Android.Resource.Layout.SimpleListItem1, null);
view = context.LayoutInflater.Inflate(Resource.Layout.MainCompanyListRow, null);
myViewHolder.CompanyName = view.FindViewById<TextView>(Resource.Id.CompanyNameLabel);
myViewHolder.CompanyAddress = view.FindViewById<TextView>(Resource.Id.CompanyAddressLabel);
myViewHolder.CompanySales = view.FindViewById<TextView>(Resource.Id.CompanySalesLabel);
myViewHolder.CompanyCityStateZipLabel = view.FindViewById<TextView>(Resource.Id.CompanyCityStateZipLabel);
view.Tag = myViewHolder;
}
myViewHolder.CompanyName.Text = company.ToArray()[position].CompanyName;
myViewHolder.CompanyAddress.Text = company.ToArray()[position].Address;
myViewHolder.CompanySales.Text = company.ToArray()[position].ActualSales.ToString("C0");
myViewHolder.CompanyCityStateZipLabel.Text = company.ToArray()[position].City + ", " + company.ToArray()[position].State + " " + company.ToArray()[position].Zip;
return view;
}
}
活动模块中的调用:
myCompany = myDBF.getCompany("");
var listView = FindViewById<ListView>(Resource.Id.OrderMethodListView);
listView.Adapter = new Models.Adapters.ListAdapter(this, myCompany);
答案 0 :(得分:0)
谢谢!我想到了。 Android 5.1不喜欢:
android:layout_weight="1"
android:layout_column="1"
android:layout_gravity="left"
一旦我从布局代码中删除了这些行,该应用就可以正常工作。