自从我将Number Picker代码改为a后,我遇到了一个问题 所有数字选择器的迭代。到目前为止,单个数字选择器工作正常 - 选择一个数字,它作为int显示在TextView中 NewVal,甚至显示在Toast消息中。 但是,当然,我需要迭代,以便OnValuedChangedListener 会查找xml文件中的所有数字选择器。 当我尝试转换数字选择器而不是之前,应用程序崩溃,所以它显然是监听器代码 - 在NewVal中需要迭代吗? 这是我的MainActivity代码..
import android.app.Activity;
import android.content.Context;
import android.content.Intent;
import android.os.Bundle;
import android.widget.Button;
import android.widget.CheckBox;
import android.widget.NumberPicker;
import android.view.View;
import android.widget.TextView;
import android.view.View.OnClickListener;
import android.widget.Toast;
import android.widget.NumberPicker.OnValueChangeListener;
public class MainActivity extends Activity {
int newVal;
private TextView tv;
Button btnDisplay;
private CheckBox chk1, chk2, chk3, chk7, chk8, chk9, chk10, chk11, chk12, chk13, chk14, chk15,
chk16, chk17, chk18, chk19, chk20, chk21, chk22, chk23, chk24, chk25, chk26, chk27, chk28, chk29, chk30, chk31, chk32, chk33, chk34, chk35, chk36, chk37, chk38, chk39, chk40, chk41, chk42, chk43, chk44, chk45, chk46, chk47, chk48, chk49, chk50, chk51, chk52, chk53;
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
setupUI();
addListenerOnChkIos();
}
public void setupUI()
{
int[] textViewIDs = new int[] {R.id.tvId1, R.id.tvId2, R.id.tvId3, R.id.tvId7,
R.id.tvId8, R.id.tvId9, R.id.tvId10, R.id.tvId11, R.id.tvId12, R.id.tvId13, R.id.tvId14, R.id.tvId15, R.id.tvId16, R.id.tvId17,
R.id.tvId18, R.id.tvId19, R.id.tvId20, R.id.tvId21, R.id.tvId22, R.id.tvId23, R.id.tvId24, R.id.tvId25, R.id.tvId26, R.id.tvId27, R.id.tvId28, R.id.tvId29, R.id.tvId30, R.id.tvId31,
R.id.tvId32, R.id.tvId33, R.id.tvId34, R.id.tvId35, R.id.tvId36, R.id.tvId37, R.id.tvId38, R.id.tvId39, R.id.tvId40, R.id.tvId41, R.id.tvId42, R.id.tvId43, R.id.tvId44, R.id.tvId45, R.id.tvId46, R.id.tvId47, R.id.tvId48, R.id.tvId49, R.id.tvId50, R.id.tvId51, R.id.tvId52, R.id.tvId53};
for(int i=0; i < textViewIDs.length; i++) {
TextView tv = (TextView ) findViewById(textViewIDs[i]);
}
//tv = (TextView) findViewById(R.id.tvId1);
int[] numpickIDs = new int[] {R.id.npId1, R.id.npId2, R.id.npId3, R.id.npId7,
R.id.npId8, R.id.npId9, R.id.npId10, R.id.npId11, R.id.npId12, R.id.npId13, R.id.npId14, R.id.npId15, R.id.npId16, R.id.npId17,
R.id.npId18, R.id.npId19, R.id.npId20, R.id.npId21, R.id.npId22, R.id.npId23, R.id.npId24, R.id.npId25, R.id.npId26, R.id.npId27, R.id.npId28, R.id.npId29, R.id.npId30, R.id.npId31,
R.id.npId32, R.id.npId33, R.id.npId34, R.id.npId35, R.id.npId36, R.id.npId37, R.id.npId38, R.id.npId39, R.id.npId40, R.id.npId41, R.id.npId42, R.id.npId43, R.id.npId44, R.id.npId45, R.id.npId46, R.id.npId47, R.id.npId48, R.id.npId49, R.id.npId50, R.id.npId51, R.id.npId52, R.id.npId53};
for(int j=0; j < numpickIDs.length; j++) {
NumberPicker np = (NumberPicker ) findViewById(numpickIDs[j]);
//NumberPicker np = (NumberPicker) findViewById(R.id.npId1);
np.setOnValueChangedListener(new OnValueChangeListener()
{
public void onValueChange(NumberPicker picker, int oldVal,
int newVal)
{
tv.setText(String.valueOf(newVal));
}
});
np.setMaxValue(12);
np.setMinValue(0);
}
}
public void addListenerOnChkIos() {
chk1 = (CheckBox) findViewById(R.id.chk1);
chk2 = (CheckBox) findViewById(R.id.chk2);
chk3 = (CheckBox) findViewById(R.id.chk3);
chk7 = (CheckBox) findViewById(R.id.chk7);
chk8 = (CheckBox) findViewById(R.id.chk8);
chk9 = (CheckBox) findViewById(R.id.chk9);
chk10 = (CheckBox) findViewById(R.id.chk10);
chk11 = (CheckBox) findViewById(R.id.chk11);
chk12 = (CheckBox) findViewById(R.id.chk12);
chk13 = (CheckBox) findViewById(R.id.chk13);
chk14 = (CheckBox) findViewById(R.id.chk14);
chk15 = (CheckBox) findViewById(R.id.chk15);
chk16 = (CheckBox) findViewById(R.id.chk16);
chk17 = (CheckBox) findViewById(R.id.chk17);
chk18 = (CheckBox) findViewById(R.id.chk18);
chk19 = (CheckBox) findViewById(R.id.chk19);
chk20 = (CheckBox) findViewById(R.id.chk20);
chk21 = (CheckBox) findViewById(R.id.chk21);
chk22 = (CheckBox) findViewById(R.id.chk22);
chk23 = (CheckBox) findViewById(R.id.chk23);
chk24 = (CheckBox) findViewById(R.id.chk24);
chk25 = (CheckBox) findViewById(R.id.chk25);
chk26 = (CheckBox) findViewById(R.id.chk26);
chk27 = (CheckBox) findViewById(R.id.chk27);
chk28 = (CheckBox) findViewById(R.id.chk28);
chk29 = (CheckBox) findViewById(R.id.chk29);
chk30 = (CheckBox) findViewById(R.id.chk30);
chk31 = (CheckBox) findViewById(R.id.chk31);
chk32 = (CheckBox) findViewById(R.id.chk32);
chk33 = (CheckBox) findViewById(R.id.chk33);
chk34 = (CheckBox) findViewById(R.id.chk34);
chk35 = (CheckBox) findViewById(R.id.chk35);
chk36 = (CheckBox) findViewById(R.id.chk36);
chk37 = (CheckBox) findViewById(R.id.chk37);
chk38 = (CheckBox) findViewById(R.id.chk38);
chk39 = (CheckBox) findViewById(R.id.chk39);
chk40 = (CheckBox) findViewById(R.id.chk40);
chk41 = (CheckBox) findViewById(R.id.chk41);
chk42 = (CheckBox) findViewById(R.id.chk42);
chk43 = (CheckBox) findViewById(R.id.chk43);
chk44 = (CheckBox) findViewById(R.id.chk44);
chk45 = (CheckBox) findViewById(R.id.chk45);
chk46 = (CheckBox) findViewById(R.id.chk46);
chk47 = (CheckBox) findViewById(R.id.chk47);
chk48 = (CheckBox) findViewById(R.id.chk48);
chk49 = (CheckBox) findViewById(R.id.chk49);
chk50 = (CheckBox) findViewById(R.id.chk50);
chk51 = (CheckBox) findViewById(R.id.chk51);
chk52 = (CheckBox) findViewById(R.id.chk52);
chk53 = (CheckBox) findViewById(R.id.chk53);
btnDisplay = (Button) findViewById(R.id.btnDisplay);
btnDisplay.setOnClickListener(new OnClickListener() {
//chk1.setOnClickListener(new OnClickListener() {
//Run when button is clicked
@Override
public void onClick(View v) {
//StringBuffer result = new StringBuffer();
//result.append("Haddock-Large : ").append(chk1.isChecked());
//result.append("\nHaddock-Medium : ").append(chk2.isChecked());
//result.append("\nHaddock-Small :").append(chk3.isChecked());
//result.append("\nKids Corner : ").append(chk4.isChecked());
//Toast.makeText(MainActivity.this, tv.getText().toString() + " " + result.toString(),
//Toast.LENGTH_LONG).show();
// if (((CheckBox) v).isChecked()) {
//Toast.makeText(MainActivity.this, tv.getText().toString() + " " + "Haddock Large",
//Toast.LENGTH_LONG).show();
//}
//}
//});
int totalamount=0;
StringBuilder result=new StringBuilder();
result.append("Selected Items:\n");
if(chk1.isChecked()){
result.append(tv.getText().toString() + "x" + "Haddock(Large) 100Rs");
totalamount+=100;
}
if(chk2.isChecked()){
result.append("\nHaddock(Med) 50Rs");
totalamount+=50;
}
if(chk3.isChecked()){
result.append("\nHaddock(Small) 120Rs");
totalamount+=120;
}
if(chk7.isChecked()){
result.append("\nChips 120Rs");
totalamount+=120;
}
if(chk8.isChecked()){
result.append("\nCone of Chips 120Rs");
totalamount+=120;
}
if(chk9.isChecked()){
result.append("\nChip Butty 120Rs");
totalamount+=120;
}
if(chk10.isChecked()){
result.append("\nFries 120Rs");
totalamount+=120;
}
if(chk11.isChecked()){
result.append("\nPattie 120Rs");
totalamount+=120;
}
if(chk12.isChecked()){
result.append("\nPattie Buttie 120Rs");
totalamount+=120;
}
if(chk13.isChecked()){
result.append("\nFish Cake 120Rs");
totalamount+=120;
}
if(chk14.isChecked()){
result.append("\nBreadcakes 120Rs");
totalamount+=120;
}
if(chk15.isChecked()){
result.append("\nHam Fritter 120Rs");
totalamount+=120;
}
if(chk16.isChecked()){
result.append("\nCorned Beef Fritter 120Rs");
totalamount+=120;
}
if(chk17.isChecked()){
result.append("\nFish Fritter 120Rs");
totalamount+=120;
}
if(chk18.isChecked()){
result.append("\nBeef Burger 120Rs");
totalamount+=120;
}
if(chk19.isChecked()){
result.append("\nSmall Sausage 120Rs");
totalamount+=120;
}
if(chk20.isChecked()){
result.append("\nJumbo Sausage 120Rs");
totalamount+=120;
}
if(chk21.isChecked()){
result.append("\nPickled Eggs 120Rs");
totalamount+=120;
}
if(chk22.isChecked()){
result.append("\nPickled Onions 120Rs");
totalamount+=120;
}
if(chk23.isChecked()){
result.append("\n8 Onion Rings 120Rs");
totalamount+=120;
}
if(chk24.isChecked()){
result.append("\nScampi(8) & chips/fries 120Rs");
totalamount+=120;
}
if(chk25.isChecked()){
result.append("\nPattie Bitesx8 120Rs");
totalamount+=120;
}
if(chk26.isChecked()){
result.append("\nHotdog"
+ " 120Rs");
totalamount+=120;
}
if(chk27.isChecked()){
result.append("\nHotdog w/chip/fries and a can 120Rs");
totalamount+=120;
}
if(chk28.isChecked()){
result.append("\nHome Made Fish Cakes 120Rs");
totalamount+=120;
}
if(chk29.isChecked()){
result.append("\nCod Roe 120Rs");
totalamount+=120;
}
if(chk30.isChecked()){
result.append("\n4 Fish Strips 120Rs");
totalamount+=120;
}
if(chk31.isChecked()){
result.append("\nChicken & Mushroom 120Rs");
totalamount+=120;
}
if(chk32.isChecked()){
result.append("\nSteak & Kidney 120Rs");
totalamount+=120;
}
if(chk33.isChecked()){
result.append("\nBeef & Onion 120Rs");
totalamount+=120;
}
if(chk34.isChecked()){
result.append("\n1/4lb Beef Burger 120Rs");
totalamount+=120;
}
if(chk35.isChecked()){
result.append("\n1/2lb Beef Burger 120Rs");
totalamount+=120;
}
if(chk36.isChecked()){
result.append("\n1/4ib Cheese Burger 120Rs");
totalamount+=120;
}
if(chk37.isChecked()){
result.append("\n1/2lb Cheese Burger 120Rs");
totalamount+=120;
}
if(chk38.isChecked()){
result.append("\nChicken Burger 120Rs");
totalamount+=120;
}
if(chk39.isChecked()){
result.append("\nDouble Chicken Burger 120Rs");
totalamount+=120;
}
if(chk40.isChecked()){
result.append("\nPot of Peas 120Rs");
totalamount+=120;
}
if(chk41.isChecked()){
result.append("\nChips spice tub 120Rs");
totalamount+=120;
}
if(chk42.isChecked()){
result.append("\nSalt 250g 120Rs");
totalamount+=120;
}
if(chk43.isChecked()){
result.append("\nVinegar 284ml 120Rs");
totalamount+=120;
}
if(chk44.isChecked()){
result.append("\nTomato Sauce 120Rs");
totalamount+=120;
}
if(chk45.isChecked()){
result.append("\nCans 120Rs");
totalamount+=120;
}
if(chk46.isChecked()){
result.append("\nKids Cup Drinks 120Rs");
totalamount+=120;
}
if(chk47.isChecked()){
result.append("\nBottled Water 120Rs");
totalamount+=120;
}
if(chk48.isChecked()){
result.append("\n2ltr Bottles star 120Rs");
totalamount+=120;
}if(chk49.isChecked()){
result.append("\n 120Rs");
totalamount+=120;
}
if(chk50.isChecked()){
result.append("\nBurger 120Rs");
totalamount+=120;
}
if(chk51.isChecked()){
result.append("\nBurger 120Rs");
totalamount+=120;
}
if(chk52.isChecked()){
result.append("\nBurger 120Rs");
totalamount+=120;
}
if(chk53.isChecked()){
result.append("\nBurger 120Rs");
totalamount+=120;
}
result.append("\nTotal: "+totalamount+"Rs");
//Displaying the message on the toast
Toast.makeText(MainActivity.this, result.toString(), Toast.LENGTH_LONG).show();
}
});
}
}
答案 0 :(得分:1)
在你的监听器中,你正在攻击的textView tv只是null,它是你的类成员从未初始化。
编辑:关于您的其他问题
现在应用程序没有崩溃,我注意到只有最后一个数字选择器工作正常,即npId53 - 所以为什么它只在迭代中选择最后一个数字选择器,而不是npId1,npId2,tvId1,tvId2等等,在利斯特纳?
=&GT;因为你只存储了一个textview,它就是你循环的最后一个。
尝试这样的事情:
private TextView[] tvs;
public void setupUI()
{
int[] textViewIDs = new int[] {R.id.tvId1, R.id.tvId2, R.id.tvId3, R.id.tvId7,
R.id.tvId8, R.id.tvId9, R.id.tvId10, R.id.tvId11, R.id.tvId12, R.id.tvId13, R.id.tvId14, R.id.tvId15, R.id.tvId16, R.id.tvId17,
R.id.tvId18, R.id.tvId19, R.id.tvId20, R.id.tvId21, R.id.tvId22, R.id.tvId23, R.id.tvId24, R.id.tvId25, R.id.tvId26, R.id.tvId27, R.id.tvId28, R.id.tvId29, R.id.tvId30, R.id.tvId31,
R.id.tvId32, R.id.tvId33, R.id.tvId34, R.id.tvId35, R.id.tvId36, R.id.tvId37, R.id.tvId38, R.id.tvId39, R.id.tvId40, R.id.tvId41, R.id.tvId42, R.id.tvId43, R.id.tvId44, R.id.tvId45, R.id.tvId46, R.id.tvId47, R.id.tvId48, R.id.tvId49, R.id.tvId50, R.id.tvId51, R.id.tvId52, R.id.tvId53};
tvs = new TextView[53];
for(int i=0; i < textViewIDs.length; i++) {
TextView tv = (TextView ) findViewById(textViewIDs[i]);
tvs[i] = tv;
}
//tv = (TextView) findViewById(R.id.tvId1);
int[] numpickIDs = new int[] {R.id.npId1, R.id.npId2, R.id.npId3, R.id.npId7,
R.id.npId8, R.id.npId9, R.id.npId10, R.id.npId11, R.id.npId12, R.id.npId13, R.id.npId14, R.id.npId15, R.id.npId16, R.id.npId17,
R.id.npId18, R.id.npId19, R.id.npId20, R.id.npId21, R.id.npId22, R.id.npId23, R.id.npId24, R.id.npId25, R.id.npId26, R.id.npId27, R.id.npId28, R.id.npId29, R.id.npId30, R.id.npId31,
R.id.npId32, R.id.npId33, R.id.npId34, R.id.npId35, R.id.npId36, R.id.npId37, R.id.npId38, R.id.npId39, R.id.npId40, R.id.npId41, R.id.npId42, R.id.npId43, R.id.npId44, R.id.npId45, R.id.npId46, R.id.npId47, R.id.npId48, R.id.npId49, R.id.npId50, R.id.npId51, R.id.npId52, R.id.npId53};
for(int j=0; j < numpickIDs.length; j++) {
NumberPicker np = (NumberPicker ) findViewById(numpickIDs[j]);
final index = j;
//NumberPicker np = (NumberPicker) findViewById(R.id.npId1);
np.setOnValueChangedListener(new OnValueChangeListener()
{
public void onValueChange(NumberPicker picker, int oldVal,
int newVal)
{
tvs[index].setText(String.valueOf(newVal));
}
});
np.setMaxValue(12);
np.setMinValue(0);
}
}