안녕하세요
이제 안드로이드 코드를 만진지 사흘 정도 된 꼬꼬마입니다.
질문 양식이나 방법이 잘못되었다면 지적 부탁드립니다.
요새 북스홀릭에서 나온 안드로이드 프로그래밍이라는 책을 보고
예제를 연습하고 있는데요.
radiobutton 과 checkbox를 통해 피자를 주문하는 예제입니다.
그런데 책과 똑같이 쳤는데도 오류는 없이 run이 안되어서
밤새 낑낑댔는데 어디를 고쳐야할지 몰라 질문드립니다 ㅠㅠ
src 의 pizzaActivity.java 코드는 아래랑 같구요
package com.example.pizzaorder;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.view.Menu;
import android.view.MenuItem;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
import android.widget.CheckBox;
import android.widget.RadioButton;
import android.widget.RadioGroup;
import android.widget.RadioGroup.OnCheckedChangeListener;
import android.widget.TextView;
public class PizzaActivity extends AppCompatActivity {
RadioGroup radiogroup;
RadioButton radioPizza;
CheckBox checkKetchup;
CheckBox checkPickle;
CheckBox checkSource;
Button button;
TextView text;
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_pizza);
text = (TextView)findViewById(R.id.text);
checkKetchup = (CheckBox)findViewById(R.id.cb_ketchup);
checkPickle = (CheckBox)findViewById(R.id.cb_pickle);
checkSource = (CheckBox)findViewById(R.id.cb_source);
radiogroup = (RadioGroup)findViewById(R.id.radiogroup);
button = (Button)findViewById(R.id.button);
radiogroup.setOnCheckedChangeListener(new OnCheckedChangeListener() {
public void onCheckedChanged(RadioGroup group, int checkedId) {
// TODO Auto-generated method stub
radioPizza = (RadioButton)findViewById(checkedId);
}
});
button.setOnClickListener(new OnClickListener() {
public void onClick(View v) {
// TODO Auto-generated method stub
String strPizza ="";
String strOption ="";
if(radioPizza != null){
strPizza = radioPizza.getText().toString()+"를 주문하셨습니다\n";
if(checkKetchup.isChecked()){
strOption += checkKetchup.getText();
}
if(checkPickle.isChecked()){
strOption += checkPickle.getText();
}
if(checkSource.isChecked()){
strOption += checkSource.getText();
}
text.setText(strPizza+strOption);
}
else {
text.setText("피자를 선택해 주세요");
}
}
});
}
@Override
public boolean onCreateOptionsMenu(Menu menu) {
// Inflate the menu; this adds items to the action bar if it is present.
getMenuInflater().inflate(R.menu.pizza, menu);
return true;
}
@Override
public boolean onOptionsItemSelected(MenuItem item) {
// Handle action bar item clicks here. The action bar will
// automatically handle clicks on the Home/Up button, so long
// as you specify a parent activity in AndroidManifest.xml.
int id = item.getItemId();
if (id == R.id.action_settings) {
return true;
}
return super.onOptionsItemSelected(item);
}
}
layout 의 activity_pizza.xml은 아래와 같습니다.
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:paddingBottom="@dimen/activity_vertical_margin"
android:paddingLeft="@dimen/activity_horizontal_margin"
android:paddingRight="@dimen/activity_horizontal_margin"
android:paddingTop="@dimen/activity_vertical_margin"
tools:context="com.example.pizzaorder.PizzaActivity" >
<TextView
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:text="피자 종류를 선택해 주세요."
android:textSize="20sp"
android:background="#666666"/>
<RadoiGroup
android:id="@+id/radiogroup"
android:orientation="vertical"
android:layout_width="match_parent"
android:layout_height="wrap_content">
<RadioButton
android:id="@+id/rb_combination"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="콤비네이션 피자" />
<RadioButton
android:id="@+id/rb_cheese"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="치즈 피자" />
<RadioButton
android:id="@+id/rb_bulgogi"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="불고기 피자" />
</RadoiGroup>
<TextView
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:text="옵션을 선택해 주세요"
android:textSize="20sp"
android:background="#666666"/>
<CheckBox
android:id="@+id/cb_ketchup"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="케첩 추가" />
<CheckBox
android:id="@+id/cb_pickle"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="피클 추가" />
<CheckBox
android:id="@+id/cb_source"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="소스 추가" />
<Button
android:id="@+id/button"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="클릭해주세요" />"
<TextView
android:id="@+id/text"
android:layout_width="match_parent"
android:layout_height="wrap_content"
/>
</RelativeLayout>
이미지도 같이 첨부합니다

