Listview无法正常工作

时间:2013-05-17 07:29:05

标签: android android-listview

我是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);

然后它正在工作,为什么?

但不显示任何内容 任何人都可以帮助我吗?

这是我的logcat

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;  
}

}

1 个答案:

答案 0 :(得分:0)

如果您是Android初学者,请熟悉调试应用程序。在适配器的开头使用断点并在调试模式下运行应用程序。一旦应用程序在断点处停止使用F6执行一步一步的执行并找出应用程序崩溃的位置。检查该变量是否已正确初始化且不为空。

找到该位置的最简单方法是双击显示Nullpointer Error的Logcat。它会带你到它破碎的位置。 包括适配器的代码,以便我们进一步检查