在MyListAdapter类中,我实现了ListAdapter,因此有一些ListAdapter类的方法要覆盖,我得到一个运行时错误,说"不能有viewTypeCount< 1",所以我将getViewTypeCount()的返回值更改为1(默认情况下为零)。请帮我解决这个问题。
07-25 11:03:20.385: E/AndroidRuntime(1731): FATAL EXCEPTION: main
07-25 11:03:20.385: E/AndroidRuntime(1731): Process: com.rajesh.musicmanager, PID: 1731
07-25 11:03:20.385: E/AndroidRuntime(1731): java.lang.RuntimeException: Unable to start activity ComponentInfo{com.rajesh.musicmanager/com.rajesh.musicmanager.MainActivity}: java.lang.IllegalArgumentException: Can't have a viewTypeCount < 1
07-25 11:03:20.385: E/AndroidRuntime(1731): at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2195)
07-25 11:03:20.385: E/AndroidRuntime(1731): at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2245)
07-25 11:03:20.385: E/AndroidRuntime(1731): at android.app.ActivityThread.access$800(ActivityThread.java:135)
07-25 11:03:20.385: E/AndroidRuntime(1731): at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1196)
07-25 11:03:20.385: E/AndroidRuntime(1731): at android.os.Handler.dispatchMessage(Handler.java:102)
07-25 11:03:20.385: E/AndroidRuntime(1731): at android.os.Looper.loop(Looper.java:136)
07-25 11:03:20.385: E/AndroidRuntime(1731): at android.app.ActivityThread.main(ActivityThread.java:5017)
07-25 11:03:20.385: E/AndroidRuntime(1731): at java.lang.reflect.Method.invokeNative(Native Method)
07-25 11:03:20.385: E/AndroidRuntime(1731): at java.lang.reflect.Method.invoke(Method.java:515)
07-25 11:03:20.385: E/AndroidRuntime(1731): at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:779)
07-25 11:03:20.385: E/AndroidRuntime(1731): at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:595)
07-25 11:03:20.385: E/AndroidRuntime(1731): at dalvik.system.NativeStart.main(Native Method)
07-25 11:03:20.385: E/AndroidRuntime(1731): Caused by: java.lang.IllegalArgumentException: Can't have a viewTypeCount < 1
07-25 11:03:20.385: E/AndroidRuntime(1731): at android.widget.AbsListView$RecycleBin.setViewTypeCount(AbsListView.java:6498)
07-25 11:03:20.385: E/AndroidRuntime(1731): at android.widget.ListView.setAdapter(ListView.java:486)
07-25 11:03:20.385: E/AndroidRuntime(1731): at android.app.ListActivity.setListAdapter(ListActivity.java:265)
07-25 11:03:20.385: E/AndroidRuntime(1731): at com.rajesh.musicmanager.MainActivity.onCreate(MainActivity.java:12)
07-25 11:03:20.385: E/AndroidRuntime(1731): at android.app.Activity.performCreate(Activity.java:5231)
07-25 11:03:20.385: E/AndroidRuntime(1731): at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1087)
07-25 11:03:20.385: E/AndroidRuntime(1731): at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2159)
07-25 11:03:20.385: E/AndroidRuntime(1731): ... 11 more
MainActivity.Java
package com.rajesh.musicmanager;
import android.app.ListActivity;
import android.os.Bundle;
public class MainActivity extends ListActivity {
@Override
protected void onCreate(Bundle savedInstanceState) {
setListAdapter(new MylistAdapter(this));
super.onCreate(savedInstanceState);
}
}
MyListAdapter.java
package com.rajesh.musicmanager;
import android.content.Context;
import android.database.DataSetObserver;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ListAdapter;
import android.widget.TextView;
public class MylistAdapter implements ListAdapter {
LayoutInflater layout;
String names[]={"efds","vxcvv","cvfvfdvgr","eriehih","jhfgur"};
String phones[]={"1234","43421","34214","341245","3543254"};
public MylistAdapter(Context context){
super();
layout=(LayoutInflater) context.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
}
public int getCount() {
// TODO Auto-generated method stub
return names.length;
}
@Override
public Object getItem(int position) {
// TODO Auto-generated method stub
return names[position];
}
@Override
public long getItemId(int position) {
// TODO Auto-generated method stub
return 0;
}
@Override
public int getItemViewType(int position) {
// TODO Auto-generated method stub
int j=1;
return j;
}
@Override
public View getView(int position, View v, ViewGroup parent) {
if (v==null) {
v=layout.inflate(R.layout.custom_list, null);
Log.e("error", "getview entered");
}
TextView txt1 =(TextView) v.findViewById(R.id.tv1);
TextView txt2 =(TextView) v.findViewById(R.id.tv2);
txt1.setText(names[position]);
txt2.setText(phones[position]);
// TODO Auto-generated method stub
return v;
}
@Override
public int getViewTypeCount() {
// TODO Auto-generated method stub
return 1;
}
@Override
public boolean hasStableIds() {
// TODO Auto-generated method stub
return false;
}
@Override
public boolean isEmpty() {
// TODO Auto-generated method stub
return false;
}
@Override
public void registerDataSetObserver(DataSetObserver observer) {
// TODO Auto-generated method stub
}
@Override
public void unregisterDataSetObserver(DataSetObserver observer) {
// TODO Auto-generated method stub
}
@Override
public boolean areAllItemsEnabled() {
// TODO Auto-generated method stub
return false;
}
@Override
public boolean isEnabled(int position) {
// TODO Auto-generated method stub
return false;
}
}
答案 0 :(得分:0)
在适配器上试试这个:
public int getItemViewType(int position) {
return 0;
}
希望这有帮助。
答案 1 :(得分:0)
使用此:
@Override
public int getViewTypeCount() {
if(getCount() > 0){
return getCount();
}else{
return super.getViewTypeCount();
}
}
答案 2 :(得分:0)
@Override
public int getViewTypeCount()
{
return 1;
}
在适配器中尝试此操作