This code creates a seekbar and makes the seekbar create as many EditText fields as the slider is at / remove ones that would be too much. This code is in OnActivityCreated
final LinearLayout linearLayout = (LinearLayout) getActivity()
.findViewById(R.id.npv_calcfields);
EditText editText = new EditText(getActivity());
editText.setId(i);
editText.setLayoutParams(new LayoutParams(
LayoutParams.FILL_PARENT, LayoutParams.WRAP_CONTENT));
SeekBar bar = (SeekBar) getActivity().findViewById(R.id.npv_seekbar);
final TextView selection = (TextView) getActivity()
.findViewById(R.id.npv_selected);
bar.setOnSeekBarChangeListener(new OnSeekBarChangeListener() {
public void onProgressChanged(SeekBar seekbar, int progress,
boolean fromUser) {
selection.setText("You have selected " + progress + " periods.");
if (progress == 0) {
String normalstring = getActivity().getResources()
.getString(R.string.npv1);
selection.setText(normalstring);
}
if (i > progress) {
while (i > progress) {
i--;
EditText editText = (EditText) getActivity()
.findViewById(i);
linearLayout.removeView(editText);
}
} else {
while (i < progress) {
EditText editText = new EditText(getActivity());
editText.setId(i);
editText.setLayoutParams(new LayoutParams(
LayoutParams.FILL_PARENT,
LayoutParams.WRAP_CONTENT));
linearLayout.addView(editText);
editText.setHint("Cash Flow " + i);
i++;
}
}
}
public void onStopTrackingTouch(SeekBar arg0) {
}
public void onStartTrackingTouch(SeekBar arg0) {
}
});
This code is in the general class area:
int i = 0;
EditText r = (EditText) getActivity().findViewById(R.id.npv_rate);
Button calc = (Button) getActivity().findViewById(R.id.npv_calc);
EditText[] DynamicField = new EditText[16];
Now I want users to input numbers into those edittext fields and then I want to do some math on them: Entry / (Math.pow(1+r, i) with i beeing the id of the field. The first entry should therefore be calculated as this: entry/(1+r)^0. This is what I tried but it doesn’t work. It just crashes on startup.
calc.setOnClickListener(new OnClickListener() {
public void onClick(View arg0) {
Double r1 = Double.parseDouble(r.getText().toString());
EditText editText = (EditText) getActivity().findViewById(i);
TextView answer = (TextView) getActivity().findViewById(R.id.npv_answer);
double[] CashFlows;
CashFlows = new double[i];
double result = 0;
CashFlows[i] = (Double.parseDouble(editText.getText()
.toString())) / (Math.pow(1 + r1, i));
for (double d : CashFlows) {
result += d;
}
answer.setText("answer is " + result);
}
});
What did I do wrong? by the way only the last code segment isnt working. if i comment that out it all works fine i tested it 🙂 just dosent do anything obviuosly 🙂
ok a little background on the errorlog that you can see here: http://pastebin.com/G8iX6Pkm
EDIT: the entire class file can be seen here: http://pastebin.com/dxA91dst, the entire project can be found here: https://github.com/killerpixler/Android-Financial-Calculator.git
the class file is a fragment that gets loaded in a DetailsActivity when somebody clicks on a listitem from the Main activity. Like i said the error has to be in the button listener because it was working before i added it.
That
NullPointerExceptioncomes from the fact that you initialize yourViewsusing thegetActivity()method where you declare them as fields in theF_NPVclass. The methodgetActivity()method will return a validActivityreference after the callbackonAttach()is called, so the way you initialize the views will not work as, at that moment(when the fields of theFragmentclass are initialized) the methodgetActivitywill returnnull, no valid reference. The correct way to do that initialization is doing it in theonActivityCreatedcallback:Also, if I may, some suggestions regarding your code:
You’re doing some double’s parsing from Strings and it may be a good idea to check the input so you don’t throw a
NumberFormatException. For example, if the user creates someEditTextsand then clicks the calculateButton(I know, it sounds silly, but there are chances the user will do it(I did it for example)), you’ll throw aNumberFormatExceptionas you try to parse an emptyString. Instead make a little check:Also, even if you have correct values in the
EditTextthe above code will throw aNullPointerException, as theeditTextvariable will benull. The reason for this is in thewhileloops that you used to create the fields. For example, if the user moves theSeekBarto 3 than thewhileloop will run 3 times, each time incrementing theivalue. Soiwill be0,1,2, so far correct but because you incrementieach time the finaliwill be4. Now in theonClickmethod you’ll look for anEditTextwith the idi, but as there is noEditTextin the layout with the id4, the view will benull.Also, try to give your classes better names, you may know very well what they mean but you could be making things worse for someone that reads your code(like F_PNV, F_PV etc).
Code for the
onActivityCreatedmethod. This should solve what you’re trying to do(if I understand what you want):