0

我正面临这个例外: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;

}

}

编辑 LogCat :--

 04-25 07:08:56.328: E/AndroidRuntime(935): FATAL EXCEPTION: main
 04-25 07:08:56.328: E/AndroidRuntime(935): java.lang.UnsupportedOperationException: addView(View, LayoutParams) is not supported in AdapterView
 04-25 07:08:56.328: E/AndroidRuntime(935):     at android.widget.AdapterView.addView(AdapterView.java:461)
 04-25 07:08:56.328: E/AndroidRuntime(935):     at android.view.LayoutInflater.inflate(LayoutInflater.java:415)
 04-25 07:08:56.328: E/AndroidRuntime(935):     at android.view.LayoutInflater.inflate(LayoutInflater.java:320)
 04-25 07:08:56.328: E/AndroidRuntime(935):     at android.view.LayoutInflater.inflate(LayoutInflater.java:276)
 04-25 07:08:56.328: E/AndroidRuntime(935):     at com.example.r.MainActivity$MyCustomAdapter.getView(MainActivity.java:137)
 04-25 07:08:56.328: E/AndroidRuntime(935):     at android.widget.AbsListView.obtainView(AbsListView.java:1315)
 04-25 07:08:56.328: E/AndroidRuntime(935):     at android.widget.ListView.makeAndAddView(ListView.java:1727)
 04-25 07:08:56.328: E/AndroidRuntime(935):     at android.widget.ListView.fillDown(ListView.java:652)
 04-25 07:08:56.328: E/AndroidRuntime(935):     at android.widget.ListView.fillFromTop(ListView.java:709)
 04-25 07:08:56.328: E/AndroidRuntime(935):     at android.widget.ListView.layoutChildren(ListView.java:1580)
      04-25 07:08:56.328: E/AndroidRuntime(935):    at android.widget.AbsListView.onLayout(AbsListView.java:1147)
 04-25 07:08:56.328: E/AndroidRuntime(935):     at android.view.View.layout(View.java:7035)
 04-25 07:08:56.328: E/AndroidRuntime(935):     at android.widget.RelativeLayout.onLayout(RelativeLayout.java:909)
 04-25 07:08:56.328: E/AndroidRuntime(935):     at android.view.View.layout(View.java:7035)
 04-25 07:08:56.328: E/AndroidRuntime(935):     at android.widget.FrameLayout.onLayout(FrameLayout.java:333)
 04-25 07:08:56.328: E/AndroidRuntime(935):     at android.view.View.layout(View.java:7035)
 04-25 07:08:56.328: E/AndroidRuntime(935):     at android.widget.FrameLayout.onLayout(FrameLayout.java:333)
 04-25 07:08:56.328: E/AndroidRuntime(935):     at android.view.View.layout(View.java:7035)
 04-25 07:08:56.328: E/AndroidRuntime(935):     at android.view.ViewRoot.performTraversals(ViewRoot.java:1045)
 04-25 07:08:56.328: E/AndroidRuntime(935):     at android.view.ViewRoot.handleMessage(ViewRoot.java:1727)
 04-25 07:08:56.328: E/AndroidRuntime(935):     at android.os.Handler.dispatchMessage(Handler.java:99)
 04-25 07:08:56.328: E/AndroidRuntime(935):     at android.os.Looper.loop(Looper.java:123)
 04-25 07:08:56.328: E/AndroidRuntime(935):     at android.app.ActivityThread.main(ActivityThread.java:4627)
 04-25 07:08:56.328: E/AndroidRuntime(935):     at java.lang.reflect.Method.invokeNative(Native Method)
 04-25 07:08:56.328: E/AndroidRuntime(935):     at java.lang.reflect.Method.invoke(Method.java:521)
 04-25 07:08:56.328: E/AndroidRuntime(935):     at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:868)
 04-25 07:08:56.328: E/AndroidRuntime(935):     at      04-25 07:08:56.328: E/AndroidRuntime(935):  at dalvik.system.NativeStart.main(Native Method)
4

1 回答 1

1

试试这个:

if(localView==null)
{
      LayoutInflater  inflater = (LayoutInflater) ctx
                    .getSystemService(Context.LAYOUT_INFLATER_SERVICE);
             localView= inflater.inflate(R.layout.listitem, null); 
      holder = new MyCustomHolder();
      holder.localTextView1 = (TextView)localView.findViewById(R.id.textView1);
      holder.localTextView2 = (TextView)localView.findViewById(R.id.textView2);

      localView.setTag(holder);
  }
于 2013-04-25T07:27:49.380 回答