Android应用无法启动,活动错误

时间:2012-05-06 07:42:01

标签: android android-activity runtime-error

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 componentInfoLogcat是:

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

我该如何解决这个问题?

1 个答案:

答案 0 :(得分:3)

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

lv = (ListView)findViewById(R.id.listview);