这是我的.java:
package com.example.clarkrubberfoam;
import android.os.Bundle;
import android.support.v4.app.Fragment;
import android.support.v7.app.ActionBarActivity;
import android.view.LayoutInflater;
import android.view.Menu;
import android.view.MenuItem;
import android.view.View;
import android.view.ViewGroup;
import android.widget.CheckBox;
import android.widget.CompoundButton;
import android.widget.CompoundButton.OnCheckedChangeListener;
import android.widget.Toast;
public class PriceEstimator extends ActionBarActivity implements OnCheckedChangeListener {
double total = 0;
double md_foam25 = 0.31, md_foam50 = 0.63, md_foam75 = 0.81, md_foam100 = 1.01, md_foam125 = 1.25, md_foam150 = 1.34;
double hd_foam25 = 0.34, hd_foam50 = 0.66, hd_foam75 = 0.91, hd_foam100 = 1.19, hd_foam125 = 1.48, hd_foam150 = 1.53;
double ye_foam25 = 0.59, ye_foam50 = 0.94, ye_foam75 = 1.41, ye_foam100 = 1.67, ye_foam125 = 2.09, ye_foam150 = 2.34;
double oe_foam25 = 0.70, oe_foam50 = 1.37, oe_foam75 = 1.69, oe_foam100 = 2.20, oe_foam125 = 2.64, oe_foam150 = 3.16;
CheckBox chkMD = (CheckBox) findViewById(R.id.checkBox1);
CheckBox chkHD = (CheckBox) findViewById(R.id.checkBox2);
CheckBox chkYE = (CheckBox) findViewById(R.id.checkBox3);
CheckBox chkOE = (CheckBox) findViewById(R.id.checkBox4);
/* Called when first opening the app */
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_price_estimator);
//Attach listeners to the checkboxes
chkMD.setOnCheckedChangeListener(this);
chkHD.setOnCheckedChangeListener(this);
chkYE.setOnCheckedChangeListener(this);
chkOE.setOnCheckedChangeListener(this);
}
@Override
public void onCheckedChanged(CompoundButton buttonView, boolean isChecked) {
// TODO Auto-generated method stub
if (buttonView == chkMD) {
if(isChecked) {
mediumDensityPrice();
} else {
total = total + 0;
}
}
if (buttonView == chkHD){
if(isChecked) {
highDensityPrice();
} else {
total = total + 0;
}
}
if (buttonView == chkYE) {
if(isChecked) {
yellowEnduroPrice();
} else {
total = total + 0;
}
}
if (buttonView == chkOE) {
if(isChecked) {
orangeEnduroPrice();
} else {
total = total + 0;
}
}
}
public void mediumDensityPrice() {
Toast.makeText(getBaseContext(), "Medium Density Selected", Toast.LENGTH_LONG).show();
}
public void highDensityPrice() {
Toast.makeText(getBaseContext(), "High Density Selected", Toast.LENGTH_LONG).show();
}
public void yellowEnduroPrice() {
Toast.makeText(getBaseContext(), "Yellow Enduro Selected", Toast.LENGTH_LONG).show();;
}
public void orangeEnduroPrice() {
Toast.makeText(getBaseContext(), "Orange Enduro Selected", Toast.LENGTH_LONG).show();;
}
/**
* A placeholder fragment containing a simple view.
*/
public static class PlaceholderFragment extends Fragment {
public PlaceholderFragment() {
}
@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
View rootView = inflater.inflate(R.layout.fragment_price_estimator,
container, false);
return rootView;
}
}
这是我的.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.clarkrubberfoam.PriceEstimator$PlaceholderFragment" >
<TextView
android:id="@+id/textView1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentLeft="true"
android:layout_alignParentTop="true"
android:layout_marginLeft="2dp"
android:layout_marginTop="2dp"
android:text="@string/price_estimator"
android:textAppearance="?android:attr/textAppearanceLarge" />
<CheckBox
android:id="@+id/checkBox1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignLeft="@+id/textView1"
android:layout_below="@+id/textView1"
android:text="@string/medium_density" />
<CheckBox
android:id="@+id/checkBox2"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignLeft="@+id/checkBox1"
android:layout_below="@+id/checkBox1"
android:text="@string/high_density" />
<CheckBox
android:id="@+id/checkBox3"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignBaseline="@+id/checkBox1"
android:layout_alignBottom="@+id/checkBox1"
android:layout_marginLeft="27dp"
android:layout_toRightOf="@+id/checkBox1"
android:text="@string/enduro_yellow" />
<CheckBox
android:id="@+id/checkBox4"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignLeft="@+id/checkBox3"
android:layout_below="@+id/checkBox3"
android:text="@string/enduro_orange" />
<TextView
android:id="@+id/textView2"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignLeft="@+id/checkBox2"
android:layout_below="@+id/checkBox2"
android:layout_marginTop="10dp"
android:text="@string/enter_width" />
<Button
android:id="@+id/button1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignLeft="@+id/numberPicker1"
android:layout_below="@+id/numberPicker1"
android:layout_marginTop="30dp"
android:onClick="totalPrice"
android:text="@string/calculate_me" />
<NumberPicker
android:id="@+id/numberPicker1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignLeft="@+id/textView2"
android:layout_below="@+id/textView2"
android:layout_marginTop="16dp" />
<NumberPicker
android:id="@+id/numberPicker2"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignBottom="@+id/numberPicker1"
android:layout_centerHorizontal="true" />
<NumberPicker
android:id="@+id/numberPicker3"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignRight="@+id/textView4"
android:layout_alignTop="@+id/numberPicker2" />
<TextView
android:id="@+id/textView3"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_above="@+id/numberPicker1"
android:layout_alignRight="@+id/numberPicker2"
android:text="@string/enter_length" />
<TextView
android:id="@+id/textView4"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_above="@+id/numberPicker1"
android:layout_alignParentRight="true"
android:text="@string/enter_thickness" />
我的问题是,当我运行程序时,它会立即崩溃。我不确定出了什么问题。我目前正在尝试调整其他多个复选框程序以满足我的需求:'http://pulse7.net/android/android-check-box-example/'。
基本上我有四种等级的泡沫和多种厚度,所以每个等级,如果在复选框中选中,将需要导致不同的方法。目前,Toast.makeText部分只是一个占位符,一旦这部分得到解决,最终将会出现在那里:)
干杯!
答案 0 :(得分:0)
findViewById()
来电后,onCreate()
方法必须setContentView()
。
public class MainActivity extends Activity {
CheckBox chkMD;
CheckBox chkHD;
CheckBox chkYE;
CheckBox chkOE;
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_price_estimator);
chkMD = (CheckBox) findViewById(R.id.checkBox1);
chkHD = (CheckBox) findViewById(R.id.checkBox2);
chkYE = (CheckBox) findViewById(R.id.checkBox3);
chkOE = (CheckBox) findViewById(R.id.checkBox4);
// Attach listeners to the checkboxes
chkMD.setOnCheckedChangeListener(this);
chkHD.setOnCheckedChangeListener(this);
chkYE.setOnCheckedChangeListener(this);
chkOE.setOnCheckedChangeListener(this);
}
}