2014-04-13 54 views
0
##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)行中;這是實現'setOnClickListener()'的正確方法嗎?

+0

也許是因爲節省了爲空,後期logcat的。 ---檢查佈局(activity_main)是否包含保存按鈕。 –

+0

我的佈局是指向不同的xml文件,謝謝你的幫助 –

+0

問題解決了嗎?我將發表評論作爲答案,以便您可以接受,並避免在沒有答案的情況下保留此問題。 –

回答

0

改變了這一行

setContentView(R.layout.activity_main); 

setContentView(R.layout.fragment_main); 
相關問題