0

代码Activity

public class MainActivity extends Activity {

    private ListView lv;
    private CustomAdapter adapter;
    private ArrayList<Event> fetch = new ArrayList<Event>();
    private Button button;
    int counter = 0;

    /** Called when the activity is first created. */
    @Override
    public void onCreate(Bundle savedInstanceState) {

        super.onCreate(savedInstanceState);
        setContentView(R.layout.main);

            adapter = new CustomAdapter(MainActivity.this, R.id.listview,fetch);
        lv.setAdapter(adapter);
        button = (Button) findViewById(R.id.button);
        button.setOnClickListener(new OnClickListener(){

            @Override
            public void onClick(View v) {
                // TODO Auto-generated method stub
                Event temp=new Event("Date","Details","GeoLocation","Location","EvtName");
                fetch.add(temp);
                counter++;
                adapter.notifyDataSetChanged();

            } });

    }

}

该应用程序失败,出现Unable to start Activity componentInfo错误。Logcat是:

05-06 12:56:34.248: E/AndroidRuntime(2215): java.lang.RuntimeException: Unable to start activity ComponentInfo{com.nasotech/com.nasotech.MainActivity}: java.lang.NullPointerException

我怎样才能解决这个问题?

4

1 回答 1

3

首先通过 Id 找到列表视图,例如

lv = (ListView)findViewById(R.id.listview);
于 2012-05-06T07:46:55.633 回答