異常:java.lang.UnsupportedOperationException: addView(View, LayoutParams) is not supported in AdapterView
實現代碼:
protected void onCreate(Bundle paramBundle)
{
super.onCreate(paramBundle);
requestWindowFeature(1);
setContentView(R.layout.activity_main);
_listView = (ListView)findViewById(R.id.listView1);
final String[] arrayOfString = SMSHelper.getInstance(this).getSMSCategoryNames();
SMSHelper.getInstance(this).getSMSCountForCategory(arrayOfString[0]);
SMSHelper.getInstance(this).getSMSForCategory(arrayOfString[0]).get(0);
SMSHelper.getInstance(this).getSMSPreviewForCategory(arrayOfString[0], "eat");
MyCustomAdapter localMyCustomAdapter = new MyCustomAdapter(this, 0, getCategoriesData());
this.b = new Bundle();
_listView.setAdapter(localMyCustomAdapter);
_listView.setOnItemClickListener(new AdapterView.OnItemClickListener()
{
public void onItemClick(AdapterView<?> paramAnonymousAdapterView, View paramAnonymousView, int paramAnonymousInt, long paramAnonymousLong)
{
Intent localIntent = new Intent(MainActivity.this, SmsList.class);
localIntent.putExtra("Category", arrayOfString[paramAnonymousInt].toString());
MainActivity.this.startActivity(localIntent);
}
});
}
class MyCustomAdapter extends ArrayAdapter<String>
{
Context ctx;
String[] listDataForView;
int paramArrayOfString;
public MyCustomAdapter(Context paramInt, int paramArrayOfString, String[] arg4)
{
super(paramInt, paramArrayOfString, arg4);
this.ctx = paramInt;
this.paramArrayOfString = paramArrayOfString;
this.listDataForView=arg4;
}
private String getCategoriesLength(int paramInt)
{
return SMSHelper.getInstance(this.ctx).getSMSCountForCategory(this.listDataForView[paramInt].toString());
}
public class MyCustomHolder {
public TextView localTextView1,localTextView2;;
}
@Override
public View getView(final int paramInt, View paramView, ViewGroup paramViewGroup)
{
View localView=paramView;
MyCustomHolder holder = null;
if(localView==null){
localView = MainActivity.this.getLayoutInflater().inflate(R.layout.listitem, paramViewGroup);
holder = new MyCustomHolder();
holder.localTextView1 = (TextView)localView.findViewById(R.id.textView1);
holder.localTextView2 = (TextView)localView.findViewById(R.id.textView2);
localView.setTag(holder);
}
else
{
holder = (MyCustomHolder)localView.getTag();
}
if (paramInt % 2 == 0){
localView.setBackgroundColor(Color.parseColor("#FFDDFEFE"));
}
holder.localTextView1.setText(listDataForView[paramInt].toString());
holder.localTextView2.setText(getCategoriesLength(paramInt));
return localView;
}
}
應該是你的布局文件錯了,看一下你的ListView是不是有子View,即
< ListView >
< XxxView >
< / XxxView >
< / ListView >
你把< XxxView >去掉就行了