当我尝试使用 json 显示从服务器接收到的数据列表以与 wcf webservices 通信时,我的代码出现错误。
我收到来自 web 服务的响应,并且数据也被添加到列表中,但我不明白为什么我会收到此错误。请指导我解决错误
这是 logcat 输出:
11-08 13:57:29.668: ERROR/AndroidRuntime(1386): Uncaught handler: thread main exiting due to uncaught exception
11-08 13:57:29.678: ERROR/AndroidRuntime(1386): java.lang.UnsupportedOperationException: addView(View, LayoutParams) is not supported in AdapterView
11-08 13:57:29.678: ERROR/AndroidRuntime(1386): at android.widget.AdapterView.addView(AdapterView.java:461)
11-08 13:57:29.678: ERROR/AndroidRuntime(1386): at android.view.LayoutInflater.inflate(LayoutInflater.java:415)
11-08 13:57:29.678: ERROR/AndroidRuntime(1386): at android.view.LayoutInflater.inflate(LayoutInflater.java:320)
11-08 13:57:29.678: ERROR/AndroidRuntime(1386): at android.view.LayoutInflater.inflate(LayoutInflater.java:276)
11-08 13:57:29.678: ERROR/AndroidRuntime(1386): at com.ecommerce.balajeebazaar.CategoryListAdapter.getView(CategoryListAdapter.java:64)
11-08 13:57:29.678: ERROR/AndroidRuntime(1386): at android.widget.AbsListView.obtainView(AbsListView.java:1273)
11-08 13:57:29.678: ERROR/AndroidRuntime(1386):
at android.widget.ListView.measureHeightOfChildren(ListView.java:1147)
11-08 13:57:29.678: ERROR/AndroidRuntime(1386): at android.widget.ListView.onMeasure(ListView.java:1060)
11-08 13:57:29.678: ERROR/AndroidRuntime(1386): at android.view.View.measure(View.java:7703)
11-08 13:57:29.678: ERROR/AndroidRuntime(1386): at android.view.ViewGroup.measureChildWithMargins(ViewGroup.java:2989)
11-08 13:57:29.678: ERROR/AndroidRuntime(1386): at android.widget.LinearLayout.measureChildBeforeLayout(LinearLayout.java:888)
11-08 13:57:29.678: ERROR/AndroidRuntime(1386): at android.widget.LinearLayout.measureVertical(LinearLayout.java:350)
11-08 13:57:29.678: ERROR/AndroidRuntime(1386): at android.widget.LinearLayout.onMeasure(LinearLayout.java:278)
11-08 13:57:29.678: ERROR/AndroidRuntime(1386): at android.view.View.measure(View.java:7703)
11-08 13:57:29.678: ERROR/AndroidRuntime(1386):
at android.view.ViewGroup.measureChildWithMargins(ViewGroup.java:2989)
11-08 13:57:29.678: ERROR/AndroidRuntime(1386): at android.widget.FrameLayout.onMeasure(FrameLayout.java:245)
11-08 13:57:29.678: ERROR/AndroidRuntime(1386): at android.view.View.measure(View.java:7703)
11-08 13:57:29.678: ERROR/AndroidRuntime(1386): at android.widget.LinearLayout.measureVertical(LinearLayout.java:464)
11-08 13:57:29.678: ERROR/AndroidRuntime(1386): at android.widget.LinearLayout.onMeasure(LinearLayout.java:278)
11-08 13:57:29.678: ERROR/AndroidRuntime(1386): at android.view.View.measure(View.java:7703)
11-08 13:57:29.678: ERROR/AndroidRuntime(1386): at android.view.ViewGroup.measureChildWithMargins(ViewGroup.java:2989)
11-08 13:57:29.678: ERROR/AndroidRuntime(1386): at android.widget.FrameLayout.onMeasure(FrameLayout.java:245)
11-08 13:57:29.678: ERROR/AndroidRuntime(1386): at android.view.View.measure(View.java:7703)
11-08 13:57:29.678: ERROR/AndroidRuntime(1386):
at android.view.ViewRoot.performTraversals(ViewRoot.java:747)
11-08 13:57:29.678: ERROR/AndroidRuntime(1386):
at android.view.ViewRoot.handleMessage(ViewRoot.java:1613)
11-08 13:57:29.678: ERROR/AndroidRuntime(1386):
at android.os.Handler.dispatchMessage(Handler.java:99)
11-08 13:57:29.678: ERROR/AndroidRuntime(1386): at android.os.Looper.loop(Looper.java:123)
11-08 13:57:29.678: ERROR/AndroidRuntime(1386): at android.app.ActivityThread.main(ActivityThread.java:4203)
11-08 13:57:29.678: ERROR/AndroidRuntime(1386): at java.lang.reflect.Method.invokeNative(Native Method)
11-08 13:57:29.678: ERROR/AndroidRuntime(1386): at java.lang.reflect.Method.invoke(Method.java:521)
11-08 13:57:29.678: ERROR/AndroidRuntime(1386): at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:791)
11-08 13:57:29.678: ERROR/AndroidRuntime(1386): at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:549)
11-08 13:57:29.678: ERROR/AndroidRuntime(1386): at dalvik.system.NativeStart.main(Native Method)
我的java文件的代码是:
CategoryArray = CW.CategoryWebservice("http://www.balajeebazaar.com/RestServiceImpl.svc/categorydetails");
for(int i = 0;i<=CategoryArray.length()-1;i++){
try {
Log.v("Categories :- ", CategoryArray.getString(i)+"");
String[] val = new String[3];
val = CategoryArray.getString(i).split(",");
Log.v("val[0] : ", val[0]);
Log.v("val[1] :", val[1]);
CategoryList.add(i, val[0]+" "+val[1]);
val = null;
Log.v("CATEGORY LIST", CategoryList.get(i).toString());
} catch (JSONException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
//CategoryList.size();
adapter = new CategoryListAdapter(CategoryActivity.this, CategoryList);
我的 CategoryListAdapter 类的代码是:
public class CategoryListAdapter extends BaseAdapter {
private Activity activity;
// private String[] data;
private ArrayList<String> CategoryList = new ArrayList<String>();
private static LayoutInflater inflater = null;
// public ImageLoader imageLoader;
public CategoryListAdapter(Activity a, ArrayList<String> categorylist) {
activity = a;
// data = d;
inflater = (LayoutInflater) activity
.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
CategoryList = categorylist;
// imageLoader=new ImageLoader(activity.getApplicationContext());
}
@Override
public int getCount() {
// TODO Auto-generated method stub
return CategoryList.size();
}
@Override
public Object getItem(int position) {
// TODO Auto-generated method stub
return position;
}
@Override
public long getItemId(int position) {
// TODO Auto-generated method stub
return position;
}
public static class ViewHolder {
public TextView text;
public ImageView image;
}
代码编辑:
@Override
public View getView(int position, View convertView, ViewGroup parent) {
// TODO Auto-generated method stub
View vi;
ViewHolder holder;
if (convertView == null) {
vi = new View(activity);
LayoutInflater inflater = activity.getLayoutInflater();
vi = inflater.inflate(R.layout.item, null, true);
holder = new ViewHolder();
holder.text = (TextView) vi.findViewById(R.id.text);
holder.image = (ImageView) vi.findViewById(R.id.image);
vi.setTag(holder);
} else {
vi = convertView;
holder = (ViewHolder) vi.getTag();
holder.text.setText(CategoryList.get(position).toString());
holder.image.setTag(R.drawable.go_arrow);
}
holder.text.setText(CategoryList.get(position).toString());
holder.image.setTag(R.drawable.go_arrow);
vi.setTag(holder);
return vi;
}
}
我将 getView 函数编辑为 ablove(请参阅我的代码编辑),它比每个人都有效地回复