public class Main extends Activity implements OnClickListener {
TextView ePrice;
TextView InputPrice;
TextView InputPercent;
TextView ePercent;
private SeekBar volumeControl = null;
#Override
protected void onCreate(Bundle savedInstanceState) {
/** Remove title bar */
requestWindowFeature(Window.FEATURE_NO_TITLE);
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
/** Hide Auto Keyboard */
getWindow().setSoftInputMode(
WindowManager.LayoutParams.SOFT_INPUT_STATE_ALWAYS_HIDDEN);
/** Hide Auto Keyboard End here */
InputPrice = (TextView) findViewById(R.id.ePrice);
InputPercent = (TextView) findViewById(R.id.ePercents);
ePrice = (TextView) findViewById(R.id.ePrice);
ePercent = (TextView) findViewById(R.id.ePercents);
Here is my SeekBar. Before this SeekBar I have been using EditText and every thing was ok but after i started using this SeekBar the SECOND result is so weird!!
SeekBarPer = (SeekBar) findViewById(R.id.volume_bar);
SeekBarPer.setOnSeekBarChangeListener(new OnSeekBarChangeListener() {
int progressChanged = 0;
public void onProgressChanged(SeekBar seekBar, int progress, boolean fromUser){
progressChanged = progress;
TextView label = (TextView) findViewById(R.id.ePercents);
label.setText(""+progressChanged);
}
public void onStartTrackingTouch(SeekBar seekBar) {
}
public void onStopTrackingTouch(SeekBar seekBar) {
}
});
what I have:
One EditText up One TextView under the SeekBar and Two another TextView are down to show the result of calculating the Price and the Percent! The First result is going well but the second is NOT!
For example if i calculate 30 dollars minus 50% i got the first result 15 and the second one 35.
What I want:
Just to correct the second result
Here is the Calculation:
// show the price to get
final TextView tR = (TextView) findViewById(R.id.viewResult);
// show the price to pay
final TextView tRpay = (TextView) findViewById(R.id.viewResultPay);
Button buttonConvert = (Button) findViewById(R.id.buttonConvert);
buttonConvert.setOnClickListener(new OnClickListener() {
#Override
public void onClick(View v) {
try {
double price = Double.parseDouble(ePrice.getText().toString());
double percent = Double.parseDouble(ePercent.getText().toString());
double pri = (price / 100.0f) * percent;;
double per = percent * ((100.0f - price)/100.0f);
tR.setText(String.valueOf(pri));
tRpay.setText(String.valueOf(per));
tR.setText("" + pri);
tRpay.setText("" + per);
// catch
} catch (NumberFormatException ex) {
// write a message to users
tR.setText("");
}
}
});
}
Update:
I am using the SeekBar as a percent. So when I move the SeekBar there will be numbers 0-100 in the second TextView.
Thanks in advance!
This should be what you want:
DiscountValue = price*discount/100.0f;
ValueToPay = price - DiscountValue;
Related
I am a beginner Android developer and I have a runtime error in my code: when I want to run it in a emulator or a device it show "force close" massage. My log is here:
http://upir.ir/934/1_5e07a.jpg
My Java code:
public class Third extends Activity
{
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
Button d = (Button) findViewById(R.id.btn4);
d.setOnClickListener(new OnClickListener() {
#Override
public void onClick(View arg0) {
dialog();
}
});
}
public void dialog(){
final Dialog dialog = new Dialog(Third.this);
dialog.setContentView(R.layout.dialog);
dialog.setTitle("واحد عدد وارد شده را انتخاب کنید");
dialog.show();
RadioGroup rg = (RadioGroup) dialog.findViewById(R.id.rg);
final RadioButton rb1 = (RadioButton) dialog.findViewById(R.id.rb1);
final RadioButton rb2 = (RadioButton) dialog.findViewById(R.id.rb2);
rg.setOnCheckedChangeListener(new OnCheckedChangeListener() {
#Override
public void onCheckedChanged(RadioGroup arg0, int arg1) {
if(rb1.isChecked()) {
dialog.dismiss();
Button t = (Button)findViewById(R.id.btn5);
t.setOnClickListener(new OnClickListener() {
public void onClick(View arg0) {
EditText ft1 = (EditText)findViewById(R.id.f3);
TextView foot = (TextView)findViewById(R.id.foot);
TextView mile = (TextView)findViewById(R.id.mile);
TextView inch = (TextView)findViewById(R.id.inch);
TextView yard = (TextView)findViewById(R.id.yard);
TextView mm = (TextView)findViewById(R.id.millymeter);
TextView dm = (TextView)findViewById(R.id.decimeter);
TextView mim = (TextView)findViewById(R.id.micrometer);
TextView nm = (TextView)findViewById(R.id.nanometer);
TextView hand = (TextView)findViewById(R.id.hand);
TextView iron = (TextView)findViewById(R.id.iron);
TextView point = (TextView)findViewById(R.id.point);
if(ft1.getText().toString().length() == 0 ){return;}
int first = Integer.parseInt(ft1.getText().toString());
double equal = first *0.0328;
DecimalFormat formatf = new DecimalFormat("#.####");
String x = formatf.format(equal)+" فوت";
foot.setText(x);
first = Integer.parseInt(ft1.getText().toString());
equal = first * 0.000005;
DecimalFormat formatm = new DecimalFormat("#.####");
x = formatm .format(equal)+"مایل";
mile.setText(x);
equal = first * 0.393;
DecimalFormat formati = new DecimalFormat("#.####");
x = formati.format(equal)+"اینچ";
inch.setText(x);
equal = first * 0.0109;
DecimalFormat formaty = new DecimalFormat("#.#####");
x = formaty.format(equal)+"یارد";
yard.setText(x);
equal = first / 10;
DecimalFormat formatmi = new DecimalFormat("#.##");
x = formatmi.format(equal)+"دسی متر";
dm.setText(x);
int equalmm = first * 10;
x = equalmm+"میلی متر";
mm.setText(x);
int equalm = first * 10000;
x = equalm+"میکرو متر";
mim.setText(x);
int equaln = first * 10000000;
x = equaln + "نانو متر";
nm.setText(x);
equal = first * 0.098;
DecimalFormat formath = new DecimalFormat("#####.#####");
x = formath.format(equal)+"هَند";
hand.setText(x);
equal = first * 19;
x = equal+"آیرون";
iron.setText(x);
equal = first * 28;
x = equal+"پوینت";
point.setText(x);
}
});
}
You need to inflate your activity with a layout resource first before you can use the findViewById() method to retrieve views. If there's no layout, then there are no views which could possibly be found.
You are missing an important part. You need to add a view to your Activity in order to find the elements of that view like the Button you are trying to instantiate.
In your OnCreate method add this:
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.the_xml_file); //Here add the xml file with the view you want to show
Button d = (Button) findViewById(R.id.btn4);
d.setOnClickListener(new OnClickListener() {
#Override
public void onClick(View arg0) {
dialog();
}
});
}
After your super.onCreate(savedInstanceState); are you setting the content view?
e.g.
setContentView(R.layout.nameofxmlfilehere);
I am nearly complete with my first android app, which is a basic tip calculator. I am having trouble with line 36
amountDisplayTextView = (TextView) findViewById(R.id.amountDisplayTextView);
I am getting these errors:
Multiple markers at this line
-Syntax error, insert ";" to complete FieldDeclaration
-Syntax error on token ".", ... expected
-Syntax error on token "amountDisplayTextView", VariableDeclaratorId expected after this token
-Return type for the method is missing
-Syntax error on token ")", { expected after this token
-Syntax error on token "amountDisplayTextView", VariableDeclaratorId expected after this token
I have tried to trouble shoot, but I hit a wall. Any assistance is appreciated! Here is the rest of the class.
package com.example.tipcalc;
import java.text.NumberFormat;
import android.app.Activity;
import android.os.Bundle;
import android.text.TextWatcher;
import android.widget.SeekBar.OnSeekBarChangeListener;
import android.widget.TextView;
public class MainActivity extends Activity {
//currency and percent formatters
private static final NumberFormat currencyFormat =
NumberFormat.getCurrencyInstance();
private static final NumberFormat percentFormat =
NumberFormat.getPercentInstance();
private double billAmount = 0.0; //bill amount entered by the user
private double customPercent = 0.18; //initial custom percent value
private TextView amountDisplayTextView; //shows formatted bill amount
private TextView percentCustomTextView;//shows custom tip percentage
private TextView tip15TextView; // shows 15% tip
private TextView total15TextView; // shows total with 15% tip
private TextView tipCustomTextView; // shows custom tip amount
private TextView totalCustomTextView; //shows total with custom tip
//called when activity is first created
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState); //call superclass's version
setContentView(R.layout.activity_main); //inflate GUI
}
//get references to the TextViews
//that MainActivity interacts with programmatically
amountDisplayTextView = (TextView) findViewById(R.id.amountDisplayTextView);
percentCustomTextView = (TextView) findViewById(R.id.percentCustomTextView);
tip15TextView = (TextView) findViewById(R.id.tip15TextView);
total15TextView = (TextView) findViewById(R.id.total15TextView);
tipCustomTextView = (TextView) findViewById(R.id.tipCustomTextView);
totalCustomTextView = (TextView) findViewById(R.id.totalCustomTextView);
}
//update 15% textviews
private void updateStandard()
{
//calculate 15% tip and total
double fifteenPercentTip = billAmount * 0.15;
double fifteenPercentTotal = billAmount + fifteenPercentTip;
//display 15% tip and total formatted as currency
tip15TextView.setText(currencyFormat.format(fifteenPercentTip));
total15TextView.setText(currencyFormat.format(fifteenPercentTotal));
} //end method updateStandard
//updates the custom tip and total TextViews
private void updateCustom()
{
//show customPercent in percentCustomTextView formatted as %
percentCustomTextView.setText(percentFormat.format(customPercent));
//calculate the custom tip and total
double customTip = billAmount * customPercent;
double customTotal = billAmount + customTip;
//display custom tip and total formatted as currency
tipCustomTextView.setText(currencyFormat.format(customTip));
totalCustomTextView.setText(currencyFormat.format(customTotal));
}//end updateCustom
//called when the user changes the position of SeekBar
private OnSeekBarChangeListener customSeekBarListener =
new OnSeekBarChangeListener()
{
//update customPercent, then call updateCustom
#Override
publicvoid onProgressChanged(SeekBar seekBar, int progress, boolean fromUser)
{
//set customPercent to position of the SeekBar's thumb
customPercent = progress / 100.0; //update the custom tip TextViews
updateCustom(); //update the custom tip TextView's
}; //end method onProgressChanged
#Override
public void onStartTrackingTouch(SeekBar seekBar)
{
}// end method onStartTrackingTouch
#Override
public void onStopTrackingTouch(SeekBar seekBar)
{
}// end method onStopTrackingTouch
};//end OnSeekBarChangeListener
//event-handling object that responds to amountEditText's events
private TextWatcher amountEditTextWatcher = new TextWatcher()
{
//called when the user enters a number
#Override
public void onTextChanged(CharSequence s, int start, int before, int count)
{
//convert amountEditText's text to a double
try
{
billAmount = Double.parseDouble(s.toString()) / 100.0;
} //end try
catch (NumberFormatException e)
{
billAmount = 0.0; //default if an exception occurs
}//end catch
//display currency formatted bill amount
amountDisplayTextView.setText(currencyFormat.format(billAmount));
updateStandard(); //update the 15% tip Textviews
updateCustom(); //update the custom tip TextViews
}; //end method onTextChanged
#Override
public void afterTextChanged(Editable s)
{
}//end method afterTextChanged
#Override
public void beforeTextChanged (CharSequence s, int start, int count, int after)
{
} // end method before TextChanged
}; //end amountEditTextWatcher
}//end mainActivity class
its all about your {}{} your declaring your variables at class level and trying to instantiate them too if you instantiate them inside of onCreate where they should be it will work
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState); //call superclass's version
setContentView(R.layout.activity_main); //inflate GUI
//get references to the TextViews
//that MainActivity interacts with programmatically
amountDisplayTextView = (TextView) findViewById(R.id.amountDisplayTextView);
percentCustomTextView = (TextView) findViewById(R.id.percentCustomTextView);
tip15TextView = (TextView) findViewById(R.id.tip15TextView);
total15TextView = (TextView) findViewById(R.id.total15TextView);
tipCustomTextView = (TextView) findViewById(R.id.tipCustomTextView);
totalCustomTextView = (TextView) findViewById(R.id.totalCustomTextView);
}
Button buttonClear;
private TextView inputValue;
private static final String DIGITS = "0123456789.";
private Boolean userIsInTheMiddleOfTypingANumber = false;
here is some animation
TextView image1;
TextView image2;
TextView image3;
TextView image4;
TextView image5;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
inputValue = (TextView) findViewById(R.id.ePrice);
findViewById(R.id.button0).setOnClickListener(this);
findViewById(R.id.button1).setOnClickListener(this);
findViewById(R.id.button2).setOnClickListener(this);
findViewById(R.id.button3).setOnClickListener(this);
findViewById(R.id.button4).setOnClickListener(this);
findViewById(R.id.button5).setOnClickListener(this);
findViewById(R.id.button6).setOnClickListener(this);
findViewById(R.id.button7).setOnClickListener(this);
findViewById(R.id.button8).setOnClickListener(this);
findViewById(R.id.button9).setOnClickListener(this);
findViewById(R.id.buttonDecimalPoint).setOnClickListener(this);
Button buttonClear = (Button) findViewById(R.id.buttonClear);
/** Backspace for in app keyboard */
buttonClear.setOnClickListener(new OnClickListener() {
#Override
public void onClick(View v) {
inputValue.dispatchKeyEvent(new KeyEvent(KeyEvent.ACTION_DOWN,
KeyEvent.KEYCODE_DEL));
}
});
/** Backspace */
/** Disable Key API level 11 where suppose to disable the android keybaord i also tried to change the manifest but the keyboard still coming up */
inputValue.setInputType(InputType.TYPE_NULL);
if (android.os.Build.VERSION.SDK_INT >= 11 ) {
inputValue.setRawInputType(InputType.TYPE_CLASS_TEXT);
inputValue.setTextIsSelectable(true);
}
image1 = (TextView)findViewById(R.id.tPercent);
image2 = (TextView)findViewById(R.id.textPercent);
image3 = (TextView)findViewById(R.id.Percent_10);
image4 = (TextView)findViewById(R.id.Percent_pay);
image5 = (TextView)findViewById(R.id.ePrice);
Animation animationFadeIn1 = AnimationUtils.loadAnimation(this, R.anim.fadein_1);
image1.startAnimation(animationFadeIn1);
Animation animationFadeIn2 = AnimationUtils.loadAnimation(this, R.anim.fadein_2);
image2.startAnimation(animationFadeIn2);
Animation animationFadeIn3 = AnimationUtils.loadAnimation(this, R.anim.fadein_3);
image3.startAnimation(animationFadeIn3);
Animation animationFadeIn4 = AnimationUtils.loadAnimation(this, R.anim.fadein_4);
image4.startAnimation(animationFadeIn4);
Animation animationFadeIn5 = AnimationUtils.loadAnimation(this, R.anim.shake);
image5.startAnimation(animationFadeIn5);
// here is first EditText which i can type normally which in app keyboard
final EditText editPrice = (EditText) findViewById(R.id.ePercents);
// here is second EditText where i can't type anything with in app keyboard
final EditText ePercent = (EditText) findViewById(R.id.ePrice);
// to get the 1 result
final TextView result = (TextView) findViewById(R.id.viewResult);
// to get the 2 result
final TextView result2 = (TextView) findViewById(R.id.viewResultPay);
Button buttonConvert = (Button) findViewById(R.id.buttonConvert);
buttonConvert.setOnClickListener(new OnClickListener() {
/**Here is some calculation happening **/
}
/**here is the onclick for buttons for in app keyboard**/
public void onClick(View v) {
/** Numbers */
String buttonPressed = ((Button) v).getText().toString();
if (DIGITS.contains(buttonPressed)) {
// digit was pressed
if (userIsInTheMiddleOfTypingANumber) {
if (buttonPressed.equals(".")
&& inputValue.getText().toString().contains(".")) {
} else {
inputValue.append(buttonPressed);
}
} else {
if (buttonPressed.equals(".")) {
inputValue.setText(0 + buttonPressed);
} else {
inputValue.setText(buttonPressed);
}
userIsInTheMiddleOfTypingANumber = true;
}
}
/** Numbers */
}
What i want is:
To disable the android keyboard.
I need to type in EditText with my made number buttons in each EditText's.. there is 2 EditText.
I tried to duplicate the onclick for digits but its writing in the same time in both EditText's
Appreciate any help.
1/ For disabling android keyboard, make a class extending EditText, and apply it to your edittexts
editPrice.setInputType(InputType.TYPE_NULL);
2/ For adding number to your editText
// On click on a digit button
editPrice.setText(editPrice.getText().toString().concat(String.valueOf(yourNumber)));
// For removing last char (in case of a delete button)
editPrice.setText(editPrice.getText().toString().substring(0, editPrice.getText().toString().length()-1));
I am making an application in which i have to fetch item name and price from first activity to another and i am able to do that but in second activity i am allowing user to put quantity in numbers and whenever user will click on button a TextView to show total amount, but not able to calculate total Amount for item.
I know this is very simple task to do but i am getting error while write this line in button onClick():
txtResult=txtCost*txtQty
here i am placing second activity code,
please correct this one:-
public class SecondScreenActivity extends Activity {
/** Called when the activity is first created. */
#Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.screen2);
TextView txtName = (TextView) findViewById(R.id.txtName);
TextView txtCost = (TextView) findViewById(R.id.txtCost);
EditText txtQty=(EditText)findViewById(R.id.txtQty);
Button btnClose = (Button) findViewById(R.id.btnCalculate);
TextView txtResult = (TextView) findViewById(R.id.txtResult);
Intent i = getIntent();
// Receiving the Data
String name = i.getStringExtra("name");
String cost = i.getStringExtra("cost");
// Displaying Received data
txtName.setText(name);
txtCost.setText(cost);
// Binding Click event to Button
btnClose.setOnClickListener(new View.OnClickListener() {
public void onClick(View arg0) {
//Closing SecondScreen Activity
//finish();
txtResult=txtCost*txtQty;
}
});
}
}
You can do it by
int cost = Integer.parseInt(txtCost.getText().toString());
int qty = Integer.parseInt(txtQty.getText().toString());
int result = cost*qty;
and then set this result into txtResult
txtResult.setText(result+"");
or you can convert int to String and then apply setText
Why are to multiplying value in Textview
btnClose.setOnClickListener(new View.OnClickListener() {
public void onClick(View arg0) {
Float f_name = new Float(name);
Float f_cost = new Float(cost);
Float f_result=f_name *f_cost ;
txtResult.setText(f_result);
}
});
}
}
txtResult=txtCost*txtQty;
These are TextView objects. You can't do maths on objects. Get the textview's values into integers and then try to multiply.
Do something like this.. depending on the type of variable you want (integer, float, double).
int a = new Integer(txtCost.getText().toString());
int b = new Integer(txtQty.getText().toString());
int c = a * b;
txtResult.setText(d);
There are 2 changes that needs to be made.
Make both txtCost and txtQty global variables.
both these variables cannot be directly multiplied. Instead do somthing like this.
double cost = Double.parseDouble(txtCost.getText().toString());
double qty = Double.parseDouble(txtQty.getText().toString());
txtResult = cost * qty ;
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 NullPointerException comes from the fact that you initialize your Views using the getActivity() method where you declare them as fields in the F_NPV class. The method getActivity() method will return a valid Activity reference after the callback onAttach() is called, so the way you initialize the views will not work as, at that moment(when the fields of the Fragment class are initialized) the method getActivity will return null, no valid reference. The correct way to do that initialization is doing it in the onActivityCreated callback:
EditText r;
Button calc;
//...
#Override
public void onActivityCreated(Bundle savedInstanceState) {
super.onActivityCreated(savedInstanceState);
r = (EditText) getActivity().findViewById(R.id.npv_rate);
calc = (Button) getActivity().findViewById(R.id.npv_calc);
//...
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 some EditTexts and then clicks the calculate Button(I know, it sounds silly, but there are chances the user will do it(I did it for example)), you'll throw a NumberFormatException as you try to parse an empty String. Instead make a little check:
public void onClick(View arg0) {
Double r1 = Double.parseDouble((r.getText().toString())
.equals("") ? "0" : 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;
String tmp = editText.getText().toString();
CashFlows[i] = (Double.parseDouble(tmp.equals("") ? "0" : tmp))
/ (Math.pow(1 + r1, i));
//...
Also, even if you have correct values in the EditText the above code will throw a NullPointerException, as the editText variable will be null. The reason for this is in the while loops that you used to create the fields. For example, if the user moves the SeekBar to 3 than the while loop will run 3 times, each time incrementing the i value. So i will be 0, 1, 2, so far correct but because you increment i each time the final i will be 4. Now in the onClick method you'll look for an EditText with the id i, but as there is no EditText in the layout with the id 4, the view will be null.
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 onActivityCreated method. This should solve what you're trying to do(if I understand what you want):
#Override
public void onActivityCreated(Bundle savedInstanceState) {
super.onActivityCreated(savedInstanceState);
r = (EditText) getActivity().findViewById(R.id.npv_rate);
calc = (Button) getActivity().findViewById(R.id.npv_calc);
final LinearLayout linearLayout = (LinearLayout) getActivity()
.findViewById(R.id.npv_calcfields);
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);
linearLayout.removeAllViews(); // the progress is 0 so
// remove all the views that
// are currently present
} else {
int currentChilds = linearLayout.getChildCount();
if (currentChilds < progress) {
while (currentChilds != progress) {
EditText editText = new EditText(getActivity());
editText.setLayoutParams(new LayoutParams(
LayoutParams.FILL_PARENT,
LayoutParams.WRAP_CONTENT));
linearLayout.addView(editText);
currentChilds++;
}
} else if (currentChilds > progress) {
while (currentChilds != progress) {
linearLayout.removeViewAt(linearLayout
.getChildCount() - 1);
currentChilds--;
}
}
}
}
public void onStopTrackingTouch(SeekBar arg0) {
}
public void onStartTrackingTouch(SeekBar arg0) {
}
});
calc.setOnClickListener(new OnClickListener() {
public void onClick(View view) {
Double r1 = Double.parseDouble((r.getText().toString())
.equals("") ? "0" : r.getText().toString());
TextView answer = (TextView) getActivity().findViewById(
R.id.npv_answer);
final LinearLayout linearLayout = (LinearLayout) getActivity()
.findViewById(R.id.npv_calcfields);
int size = linearLayout.getChildCount();
double[] CashFlows = new double[size];
double result = 0;
for (int i = 0; i < size; i++) {
EditText editText = (EditText) linearLayout.getChildAt(i);
String tmp = editText.getText().toString();
CashFlows[i] = (Double.parseDouble(tmp.equals("") ? "0"
: tmp)) / (Math.pow(1 + r1, i));
}
for (double d : CashFlows) {
result += d;
}
answer.setText("answer is " + result);
}
});
}