我在listview android中的代码有问题

时间:2013-09-21 00:32:54

标签: android eclipse listview

我正在尝试将listview与图像和文本放在一起  这是我的代码

package com.example.listview;

import java.util.ArrayList;
import java.util.List;
import android.app.Activity;
import android.os.Bundle;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ArrayAdapter;
import android.widget.ImageView;
import android.widget.ListView;
import android.widget.TextView;

public class Sira extends Activity {

    private List<Kisas> stories = new ArrayList<Kisas>();

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_sira);
        //Generates ListItems
        //Ctrl+1 to create the method from here
        PopulateStories();
        //To Show Items in ListView
        PopulateListView();
    }

    private void PopulateStories() {
        stories.add(new Kisas("Said bn 3amer Al Jam7i",1999,R.drawable.user,"needling work"));
        stories.add(new Kisas("Abu Tal7a Al Ansari",1999,R.drawable.personal,"needling work"));
        stories.add(new Kisas("Al Bara2 bn Malek Al Ansari",1999,R.drawable.smile,"needling work"));
        stories.add(new Kisas("Abu Ayoub Al Ansari",1999,R.drawable.ic_launcher,"needling work"));    
    }

    private void PopulateListView() {
        ArrayAdapter<Kisas> arrayadapter = new MyListAdapter();//we named it mylistadapter
        ListView List= (ListView) findViewById(R.id.listView_Stories);
        List.setAdapter(arrayadapter);
    }

    private class MyListAdapter extends ArrayAdapter<Kisas>{
        public MyListAdapter() {
            super(Sira.this,
                R.layout.item_view,stories);
        }

        @Override
        public View getView(int position, View convertView, ViewGroup parent) {
            View item_View= convertView;

            if(item_View ==null){
                item_View = getLayoutInflater().inflate(R.layout.item_view, parent ,false); 
            }
            Kisas currentStory= stories.get(position);
            ImageView imageview= (ImageView) item_View.findViewById(R.id.item_Icon)  ;
            imageview.setImageResource(currentStory.getIconId());
            TextView makeText = (TextView) item_View.findViewById(R.id.item_textMake);
            makeText.setText(currentStory.getMake());
            TextView yearText = (TextView) item_View.findViewById(R.id.item_textYear);
            yearText.setText(""+ currentStory.getYear()); //Lezem 7awela la string
            TextView conditionText = (TextView) item_View.findViewById(R.id.item_textCondition);
            conditionText.setText(currentStory.getCondition());
            return item_View;
        }
    }
}

它在PopulateListView方法中给我一个错误

任何人都可以提供帮助吗?

我所知道的是代码在这些行中:

private void PopulateListView() {
    ArrayAdapter<Kisas> arrayadapter = new MyListAdapter();//we named it mylistadapter
    ListView List= (ListView) findViewById(R.id.listView_Stories);
    List.setAdapter(arrayadapter);    
}

private class MyListAdapter extends ArrayAdapter<Kisas> {
    public MyListAdapter() {
        super(Sira.this, R.layout.item_view,stories);
    }
}

我是android的新手,所以我按照以下视频中的步骤显示: http://www.youtube.com/watch?v=WRANgDgM2Zg

1 个答案:

答案 0 :(得分:1)

如果您收到NullPointerException,则拨打

findViewById(R.id.listView_Stories);

肯定会回归null。请仔细检查您的ID名称中是否有拼写错误,并且您的布局xml中实际上有一个名为listView_Stories的视图。

PS:注意命名约定,如果你坚持 camelCase (即listViewStories而不是listView_Stories),你可能会有更少的混淆。