这是实现' setOnClickListener()'的正确方法吗?

时间:2014-04-13 14:48:39

标签: android onclicklistener

##For accepting a string from a EditText on clicking a button##

@Override
public void onCreate(Bundle savedInstanceState)
{
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_main);
    //save button
    //be notified when the button is clicked
    saves=(Button)findViewById(R.id.save);
    saves.setOnClickListener(onSave);
}
private View.OnClickListener onSave=new View.OnClickListener()
{
    public void onClick(View v)
    {
        Restaurant r = new Restaurant();
        EditText name=(EditText)findViewById(R.id.name_enter);
        EditText address=(EditText)findViewById(R.id.address_enter);
        r.setName(name.getText().toString());
        r.setAddress(address.getText().toString());               
     }
};
}

问题是我的应用程序每次启动时都会崩溃。 logcat说错误在sav​​e.setOnClickListener(onSave)行中;

1 个答案:

答案 0 :(得分:0)

更改了这一行

setContentView(R.layout.activity_main);

setContentView(R.layout.fragment_main);