I am new in Android and have a problem with these method these methods to set default values use
@SuppressLint("StringFormatMatches")
public void setTipValues() {
tvTipPercent.setText(getString(R.string.main_msg_tipPercent, Percentage));
tvBillTotalAmount.setText(getString(R.string.main_msg_billTotalResult,finalBillAmount));
tvTipAmount.setText(getString(R.string.main_msg_tipTotal,tipTotal));
}
and to calculate final bill
private void CalculteFinalBill() {
if(Percentage==0)
Percentage=Default_Tip_Percent;
if(!etBillAmount.getText().toString().equals("") && !etBillAmount.getText().toString().equals("."))
totalBillAmount=Float.valueOf(etBillAmount.getText().toString());
else
totalBillAmount=0;
tipTotal=(totalBillAmount*Percentage)/100;
finalBillAmount=totalBillAmount+tipTotal ;
}
and i add these strings to strings.xml
<resources>
<string name="app_name">Tip Calculator</string>
<string name="main_msg_billAmount">Bill AMOUNT</string>
<string name="main_hint_billAmount">Enter Bill Amount Here</string>
<string name="main_msg_serviceRating">Service Rating</string>
<string name="main_msg_tipPercent">%s%% Tip Percent</string>
<string name="main_msg_tipTotal">$%s Tip Total</string>
<string name="main_msg_billTotal">Bill Total</string>
<string name="main_msg_billTotalResult">$%s</string>
the variables are
TextView tvTipPercent;
TextView tvBillTotalAmount;
TextView tvTipAmount;
EditText etBillAmount;
float Percentage = 0;
float tipTotal = 0;
float finalBillAmount = 0;
float Regular_Tip_Percent = 10;
float Default_Tip_Percent = 15;
float Excellent_Tip_Percent = 20;
float totalBillAmount;
ImageButton Im1;
ImageButton Im2;
ImageButton Im3;
when i try to add setTipValues(); to Oncreate and run my app i have a fatal exception
03-23 08:56:00.768 1836-1836/com.example.tipcalculator E/AndroidRuntime: FATAL EXCEPTION: main
java.lang.RuntimeException: Unable to start activity ComponentInfo{com.example.tipcalculator/com.example.tipcalculator.MainActivity}: java.lang.NullPointerException
at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2059)
at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2084)
at android.app.ActivityThread.access$600(ActivityThread.java:130)
at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1195)
at android.os.Handler.dispatchMessage(Handler.java:99)
at android.os.Looper.loop(Looper.java:137)
at android.app.ActivityThread.main(ActivityThread.java:4745)
at java.lang.reflect.Method.invokeNative(Native Method)
at java.lang.reflect.Method.invoke(Method.java:511)
at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:786)
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:553)
at dalvik.system.NativeStart.main(Native Method)
Caused by: java.lang.NullPointerException
at com.example.tipcalculator.MainActivity.setTipValues(MainActivity.java:48)
at com.example.tipcalculator.MainActivity.onCreate(MainActivity.java:40)
at android.app.Activity.performCreate(Activity.java:5008)
at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1079)
at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2023)
in line 48 which refer to `
tvTipPercent.setText(getString(R.string.main_msg_tipPercent, Percentage));
i Know butterKnife solve this annotation problem but how i can without using it
can someone help me?
public class MainActivity extends AppCompatActivity {
TextView tvTipAmount;
TextView tvBillTotalAmount;
TextView tvTipPercent;
EditText etBillAmount;
float Percentage = 0;
float tipTotal = 0;
float finalBillAmount = 0;
float Regular_Tip_Percent = 10;
float Default_Tip_Percent = 15;
float Excellent_Tip_Percent = 20;
float totalBillAmount;
ImageButton Im1;
ImageButton Im2;
ImageButton Im3;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
final EditText etBillAmount=(EditText)findViewById(R.id.etBillAmount);
final TextView tvTipAmount = (TextView) findViewById(R.id.tvTipAmount);
final TextView tvBillTotalAmount = (TextView) findViewById(R.id.tvBillTotalAmount);
final TextView tvTipPercent = (TextView) findViewById(R.id.tvTipPercent);
setTipValues();
}
@SuppressLint("StringFormatMatches")
public void setTipValues() {
tvTipPercent.setText(getString(R.string.main_msg_tipPercent, Percentage));
tvBillTotalAmount.setText(getString(R.string.main_msg_billTotalResult, finalBillAmount));
tvTipAmount.setText(getString(R.string.main_msg_tipTotal, tipTotal));
}
}