我是 android 新手,我搜索了正确使用 listview 的代码,我找到了这个视频
http://www.youtube.com/watch?v=iJl1wpptl3A
并使用了相同的代码,但它不起作用。
这是代码
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_disgaea_main);
//listview
ListView lv = (ListView)findViewById(R.id.ListView);
List<ListViewItem> items = new ArrayList<DisgaeaMainActivity.ListViewItem>();
items.add(new ListViewItem()
{{
ThumbnailResource = R.drawable.laharlsprite;
Title = "Laharl";
Subtitle = "Overlord";
}});
items.add(new ListViewItem()
{{
ThumbnailResource = R.drawable.etnasprite;
Title = "Etna";
Subtitle = "Vassal";
}});
CustomListViewAdapter adapter = new CustomListViewAdapter(this, items);
lv.setAdapter(adapter);
//imagenes
ImageView logo = (ImageView) findViewById(R.id.logo);
logo.setImageResource(R.drawable.logodisgaea);
//logo.setAdjustViewBounds(true);
}
@Override
public boolean onCreateOptionsMenu(Menu menu) {
// Inflate the menu; this adds items to the action bar if it is present.
getMenuInflater().inflate(R.menu.disgaea_main, menu);
return true;
}
//Botones
public void onClickchar(View view){
startActivity(new Intent("com.holloe.disgaeaguide.Character"));
}
public void onClickclas(View view){
startActivity(new Intent("com.holloe.disgaeaguide.Classes"));
}
public void onClickmon(View view){
startActivity(new Intent("com.holloe.disgaeaguide.monster"));
}
//lista
class ListViewItem
{
public int ThumbnailResource;
public String Title;
public String Subtitle;
}
}
如果我删除此代码
CustomListViewAdapter adapter = new CustomListViewAdapter(this, items);
lv.setAdapter(adapter);
然后它的工作,为什么?
但不显示任何内容有人可以帮助我吗?
这是我的日志猫
05-17 07:41:30.804: D/AndroidRuntime(1196): Shutting down VM
05-17 07:41:30.804: W/dalvikvm(1196): threadid=1: thread exiting with uncaught exception (group=0x409961f8)
05-17 07:41:30.814: E/AndroidRuntime(1196): FATAL EXCEPTION: main
05-17 07:41:30.814: E/AndroidRuntime(1196): java.lang.RuntimeException: Unable to start activity ComponentInfo{com.holloe.disgaeaguide/com.holloe.disgaeaguide.DisgaeaMainActivity}: java.lang.NullPointerException
05-17 07:41:30.814: E/AndroidRuntime(1196): at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:1955)
05-17 07:41:30.814: E/AndroidRuntime(1196): at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:1980)
05-17 07:41:30.814: E/AndroidRuntime(1196): at android.app.ActivityThread.access$600(ActivityThread.java:122)
05-17 07:41:30.814: E/AndroidRuntime(1196): at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1146)
05-17 07:41:30.814: E/AndroidRuntime(1196): at android.os.Handler.dispatchMessage(Handler.java:99)
05-17 07:41:30.814: E/AndroidRuntime(1196): at android.os.Looper.loop(Looper.java:137)
05-17 07:41:30.814: E/AndroidRuntime(1196): at android.app.ActivityThread.main(ActivityThread.java:4340)
05-17 07:41:30.814: E/AndroidRuntime(1196): at java.lang.reflect.Method.invokeNative(Native Method)
05-17 07:41:30.814: E/AndroidRuntime(1196): at java.lang.reflect.Method.invoke(Method.java:511)
05-17 07:41:30.814: E/AndroidRuntime(1196): at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:784)
05-17 07:41:30.814: E/AndroidRuntime(1196): at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:551)
05-17 07:41:30.814: E/AndroidRuntime(1196): at dalvik.system.NativeStart.main(Native Method)
05-17 07:41:30.814: E/AndroidRuntime(1196): Caused by: java.lang.NullPointerException
05-17 07:41:30.814: E/AndroidRuntime(1196): at com.holloe.disgaeaguide.DisgaeaMainActivity.onCreate(DisgaeaMainActivity.java:40)
05-17 07:41:30.814: E/AndroidRuntime(1196): at android.app.Activity.performCreate(Activity.java:4465)
05-17 07:41:30.814: E/AndroidRuntime(1196): at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1049)
05-17 07:41:30.814: E/AndroidRuntime(1196): at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:1919)
05-17 07:41:30.814: E/AndroidRuntime(1196): ... 11 more
这是我的适配器代码
LayoutInflater inflater;
List<com.holloe.disgaeaguide.DisgaeaMainActivity.ListViewItem> items;
public CustomListViewAdapter(Activity context, List<ListViewItem> items) {
super();
this.items = items;
this.inflater = (LayoutInflater)context.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
}
@Override
public int getCount() {
// TODO Auto-generated method stub
return items.size();
}
@Override
public Object getItem(int position) {
// TODO Auto-generated method stub
return null;
}
@Override
public long getItemId(int position) {
// TODO Auto-generated method stub
return 0;
}
@Override
public View getView(final int position, View convertView, ViewGroup parent) {
// TODO Auto-generated method stub
ListViewItem item = items.get(position);
View vi=convertView;
if(convertView==null)
vi = inflater.inflate(R.layout.item_row, null);
ImageView imgThumbnail =(ImageView) vi.findViewById(R.id.imgThumbnail);
TextView txtTitle =(TextView)vi.findViewById(R.id.txtTitle);
TextView txtSubTitle =(TextView)vi.findViewById(R.id.txtSubTitle);
imgThumbnail.setImageResource(item.ThumbnailResource);
txtTitle.setText(item.Title);
txtSubTitle.setText(item.Subtitle);
return vi;
}
}