本文介绍了这是实现"setOnClickListener()"的正确方法吗?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

##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​​es.setOnClickListener(onSave);

Problem is that my application crashes everytime I start it. The logcat says that the error is in the line saves.setOnClickListener(onSave);

推荐答案

更改了此行

setContentView(R.layout.activity_main);

setContentView(R.layout.fragment_main);

这篇关于这是实现"setOnClickListener()"的正确方法吗?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

06-19 04:05