Why can't I add methods inside onCreate() because of annotation?What is a NullPointerException, and how do I fix it?Can't create handler inside thread that has not called Looper.prepare()How to handle onClickListener in android?Learning Android Java - application failsAndroid App Keeps Crashingjava.lang.ClassCastException: android.widget.EditTextsetText on button from another activity androidSetting onSeekBarChangeListener causes null object exception even when following all the right codeGetting Phone Number and Name from Contacts ListViewHardwarePropertiesManager is not workingSearch Firestore query don't show data in RecycleView
Shortest amud or daf in Shas?
Failing students when it might cause them economic ruin
What would be the game balance implications for using the Gygax method for applying falling damage?
Can I modify the report menu?
Former Employer just sent me an IP Agreement
Why do academics prefer Mac/Linux?
multicol package causes underfull hbox
Why does the setUID bit work inconsistently?
Is it a good idea to teach algorithm courses using pseudocode?
Windows reverting changes made by Linux to FAT32 partion
how to create an executable file for an AppleScript?
Can 2 light bulbs of 120V in series be used on 230V AC?
In Dutch history two people are referred to as "William III"; are there any more cases where this happens?
Sort a section of a file
Working hours and productivity expectations for game artists and programmers
How come Arya Stark wasn't hurt by this in Game of Thrones Season 8 Episode 5?
Combining two Lorentz boosts
Can ThermodynamicData be used with NSolve?
Should I twist DC power and ground wires from a power supply?
Parse a C++14 integer literal
How to draw pentagram-like shape in Latex?
What color to choose as "danger" if the main color of my app is red
Why is choosing a suitable thermodynamic potential important?
RegEx with d doesn’t work in if-else statement with [[
Why can't I add methods inside onCreate() because of annotation?
What is a NullPointerException, and how do I fix it?Can't create handler inside thread that has not called Looper.prepare()How to handle onClickListener in android?Learning Android Java - application failsAndroid App Keeps Crashingjava.lang.ClassCastException: android.widget.EditTextsetText on button from another activity androidSetting onSeekBarChangeListener causes null object exception even when following all the right codeGetting Phone Number and Name from Contacts ListViewHardwarePropertiesManager is not workingSearch Firestore query don't show data in RecycleView
.everyoneloves__top-leaderboard:empty,.everyoneloves__mid-leaderboard:empty,.everyoneloves__bot-mid-leaderboard:empty height:90px;width:728px;box-sizing:border-box;
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 ComponentInfocom.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));
add a comment |
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 ComponentInfocom.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));
add a comment |
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 ComponentInfocom.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));
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 ComponentInfocom.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));
edited Mar 23 at 10:00
Hamdy
asked Mar 22 at 16:43
HamdyHamdy
34
34
add a comment |
add a comment |
3 Answers
3
active
oldest
votes
Welcome to SO, first off you should read how to
ask; remember to never add
images of code, but add the entire code to the answer, because it is
really hard to replicate any problem having an image of it instead of
the text (which can be copy-pasted).
Now, which is the line throwing error for you? MainActivity:java:48 refers to which line?
This error means you are trying to access a variable which is null
Please add the line 48 of your code which is throwing error; and have a look at What is NullPointerException
PS: This is not entirely an answer, but to help you I preferred add some hints here rather than posting a not formatted comment. Once you provide the needed informations I will improve this
EDIT after your new informations
I think your problems is in how you are initializing variables: in OnCreate method you call something like this:
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);
first off, you don't need the final attribute; but more important you are creating 4 new local variables, you are not using your class-scoped ones which are those:
TextView tvTipAmount;
TextView tvBillTotalAmount;
TextView tvTipPercent;
EditText etBillAmount;
if you want to use your class-variables you just need to call them without re-creating new ones, like this:
etBillAmount=(EditText)findViewById(R.id.etBillAmount);
tvTipAmount = (TextView) findViewById(R.id.tvTipAmount);
tvBillTotalAmount = (TextView) findViewById(R.id.tvBillTotalAmount);
tvTipPercent = (TextView) findViewById(R.id.tvTipPercent);
as you can see, since you already created them in your activity, you just need to call them to get access.
That said, your error came off because you were not initializing the class-variables, but you were creating new ones. for this reason, the next time you try to access them, they are null.
Let me know if this helped you! Have a nice day
line 48: tvTipAmount.setText(getString(R.string.main_msg_tipTotal,tipTotal));
– Hamdy
Mar 22 at 17:52
@Hamdy edited, let me know! Good luck!
– Pier Giorgio Misley
Mar 23 at 11:40
Yes it is excellent thanks
– Hamdy
Mar 23 at 12:16
add a comment |
As an addition to Pier's answer with some Android basics, is tvTipPercent being initialized, for example tvTipPercent = findViewById(R.id.tip_percent); or something which is an assignment to those variables, before line 40 inside onCreate()?
If no (because you didn't post the full MainActivity.java), then it is very possible that the root reason is here. Assign them.
add a comment |
Use below code.
tvTipPercen.setText(getString(R.string.main_msg_tipPercent)+" "+Percentage);
tvBillTotalAmoun.setText(getString(R.string.main_msg_billTotalResult )+" "+finalBillAmount));
tvTipAmoun.setText(getString(R.string.main_msg_tipTotal)+" "+tipTotal));
percentage ,finalBillAmount and tip Total replace placeholder in strings
– Hamdy
Mar 22 at 18:31
Then you can directly set text on Textview.
– Bhavesh Shiyani
Mar 22 at 18:38
tvTipAmoun.setText(String.valueOf(tipTotal));
– Bhavesh Shiyani
Mar 22 at 18:40
You must convert float value into string
– Bhavesh Shiyani
Mar 22 at 18:41
my problem when i try to add this code into method and add it inside oncreate unlike when i use butterknife i can add method
– Hamdy
Mar 22 at 20:11
add a comment |
Your Answer
StackExchange.ifUsing("editor", function ()
StackExchange.using("externalEditor", function ()
StackExchange.using("snippets", function ()
StackExchange.snippets.init();
);
);
, "code-snippets");
StackExchange.ready(function()
var channelOptions =
tags: "".split(" "),
id: "1"
;
initTagRenderer("".split(" "), "".split(" "), channelOptions);
StackExchange.using("externalEditor", function()
// Have to fire editor after snippets, if snippets enabled
if (StackExchange.settings.snippets.snippetsEnabled)
StackExchange.using("snippets", function()
createEditor();
);
else
createEditor();
);
function createEditor()
StackExchange.prepareEditor(
heartbeatType: 'answer',
autoActivateHeartbeat: false,
convertImagesToLinks: true,
noModals: true,
showLowRepImageUploadWarning: true,
reputationToPostImages: 10,
bindNavPrevention: true,
postfix: "",
imageUploader:
brandingHtml: "Powered by u003ca class="icon-imgur-white" href="https://imgur.com/"u003eu003c/au003e",
contentPolicyHtml: "User contributions licensed under u003ca href="https://creativecommons.org/licenses/by-sa/3.0/"u003ecc by-sa 3.0 with attribution requiredu003c/au003e u003ca href="https://stackoverflow.com/legal/content-policy"u003e(content policy)u003c/au003e",
allowUrls: true
,
onDemand: true,
discardSelector: ".discard-answer"
,immediatelyShowMarkdownHelp:true
);
);
Sign up or log in
StackExchange.ready(function ()
StackExchange.helpers.onClickDraftSave('#login-link');
);
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
StackExchange.ready(
function ()
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f55304228%2fwhy-cant-i-add-methods-inside-oncreate-because-of-annotation%23new-answer', 'question_page');
);
Post as a guest
Required, but never shown
3 Answers
3
active
oldest
votes
3 Answers
3
active
oldest
votes
active
oldest
votes
active
oldest
votes
Welcome to SO, first off you should read how to
ask; remember to never add
images of code, but add the entire code to the answer, because it is
really hard to replicate any problem having an image of it instead of
the text (which can be copy-pasted).
Now, which is the line throwing error for you? MainActivity:java:48 refers to which line?
This error means you are trying to access a variable which is null
Please add the line 48 of your code which is throwing error; and have a look at What is NullPointerException
PS: This is not entirely an answer, but to help you I preferred add some hints here rather than posting a not formatted comment. Once you provide the needed informations I will improve this
EDIT after your new informations
I think your problems is in how you are initializing variables: in OnCreate method you call something like this:
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);
first off, you don't need the final attribute; but more important you are creating 4 new local variables, you are not using your class-scoped ones which are those:
TextView tvTipAmount;
TextView tvBillTotalAmount;
TextView tvTipPercent;
EditText etBillAmount;
if you want to use your class-variables you just need to call them without re-creating new ones, like this:
etBillAmount=(EditText)findViewById(R.id.etBillAmount);
tvTipAmount = (TextView) findViewById(R.id.tvTipAmount);
tvBillTotalAmount = (TextView) findViewById(R.id.tvBillTotalAmount);
tvTipPercent = (TextView) findViewById(R.id.tvTipPercent);
as you can see, since you already created them in your activity, you just need to call them to get access.
That said, your error came off because you were not initializing the class-variables, but you were creating new ones. for this reason, the next time you try to access them, they are null.
Let me know if this helped you! Have a nice day
line 48: tvTipAmount.setText(getString(R.string.main_msg_tipTotal,tipTotal));
– Hamdy
Mar 22 at 17:52
@Hamdy edited, let me know! Good luck!
– Pier Giorgio Misley
Mar 23 at 11:40
Yes it is excellent thanks
– Hamdy
Mar 23 at 12:16
add a comment |
Welcome to SO, first off you should read how to
ask; remember to never add
images of code, but add the entire code to the answer, because it is
really hard to replicate any problem having an image of it instead of
the text (which can be copy-pasted).
Now, which is the line throwing error for you? MainActivity:java:48 refers to which line?
This error means you are trying to access a variable which is null
Please add the line 48 of your code which is throwing error; and have a look at What is NullPointerException
PS: This is not entirely an answer, but to help you I preferred add some hints here rather than posting a not formatted comment. Once you provide the needed informations I will improve this
EDIT after your new informations
I think your problems is in how you are initializing variables: in OnCreate method you call something like this:
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);
first off, you don't need the final attribute; but more important you are creating 4 new local variables, you are not using your class-scoped ones which are those:
TextView tvTipAmount;
TextView tvBillTotalAmount;
TextView tvTipPercent;
EditText etBillAmount;
if you want to use your class-variables you just need to call them without re-creating new ones, like this:
etBillAmount=(EditText)findViewById(R.id.etBillAmount);
tvTipAmount = (TextView) findViewById(R.id.tvTipAmount);
tvBillTotalAmount = (TextView) findViewById(R.id.tvBillTotalAmount);
tvTipPercent = (TextView) findViewById(R.id.tvTipPercent);
as you can see, since you already created them in your activity, you just need to call them to get access.
That said, your error came off because you were not initializing the class-variables, but you were creating new ones. for this reason, the next time you try to access them, they are null.
Let me know if this helped you! Have a nice day
line 48: tvTipAmount.setText(getString(R.string.main_msg_tipTotal,tipTotal));
– Hamdy
Mar 22 at 17:52
@Hamdy edited, let me know! Good luck!
– Pier Giorgio Misley
Mar 23 at 11:40
Yes it is excellent thanks
– Hamdy
Mar 23 at 12:16
add a comment |
Welcome to SO, first off you should read how to
ask; remember to never add
images of code, but add the entire code to the answer, because it is
really hard to replicate any problem having an image of it instead of
the text (which can be copy-pasted).
Now, which is the line throwing error for you? MainActivity:java:48 refers to which line?
This error means you are trying to access a variable which is null
Please add the line 48 of your code which is throwing error; and have a look at What is NullPointerException
PS: This is not entirely an answer, but to help you I preferred add some hints here rather than posting a not formatted comment. Once you provide the needed informations I will improve this
EDIT after your new informations
I think your problems is in how you are initializing variables: in OnCreate method you call something like this:
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);
first off, you don't need the final attribute; but more important you are creating 4 new local variables, you are not using your class-scoped ones which are those:
TextView tvTipAmount;
TextView tvBillTotalAmount;
TextView tvTipPercent;
EditText etBillAmount;
if you want to use your class-variables you just need to call them without re-creating new ones, like this:
etBillAmount=(EditText)findViewById(R.id.etBillAmount);
tvTipAmount = (TextView) findViewById(R.id.tvTipAmount);
tvBillTotalAmount = (TextView) findViewById(R.id.tvBillTotalAmount);
tvTipPercent = (TextView) findViewById(R.id.tvTipPercent);
as you can see, since you already created them in your activity, you just need to call them to get access.
That said, your error came off because you were not initializing the class-variables, but you were creating new ones. for this reason, the next time you try to access them, they are null.
Let me know if this helped you! Have a nice day
Welcome to SO, first off you should read how to
ask; remember to never add
images of code, but add the entire code to the answer, because it is
really hard to replicate any problem having an image of it instead of
the text (which can be copy-pasted).
Now, which is the line throwing error for you? MainActivity:java:48 refers to which line?
This error means you are trying to access a variable which is null
Please add the line 48 of your code which is throwing error; and have a look at What is NullPointerException
PS: This is not entirely an answer, but to help you I preferred add some hints here rather than posting a not formatted comment. Once you provide the needed informations I will improve this
EDIT after your new informations
I think your problems is in how you are initializing variables: in OnCreate method you call something like this:
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);
first off, you don't need the final attribute; but more important you are creating 4 new local variables, you are not using your class-scoped ones which are those:
TextView tvTipAmount;
TextView tvBillTotalAmount;
TextView tvTipPercent;
EditText etBillAmount;
if you want to use your class-variables you just need to call them without re-creating new ones, like this:
etBillAmount=(EditText)findViewById(R.id.etBillAmount);
tvTipAmount = (TextView) findViewById(R.id.tvTipAmount);
tvBillTotalAmount = (TextView) findViewById(R.id.tvBillTotalAmount);
tvTipPercent = (TextView) findViewById(R.id.tvTipPercent);
as you can see, since you already created them in your activity, you just need to call them to get access.
That said, your error came off because you were not initializing the class-variables, but you were creating new ones. for this reason, the next time you try to access them, they are null.
Let me know if this helped you! Have a nice day
edited Mar 23 at 11:39
answered Mar 22 at 17:10
Pier Giorgio MisleyPier Giorgio Misley
3,71821643
3,71821643
line 48: tvTipAmount.setText(getString(R.string.main_msg_tipTotal,tipTotal));
– Hamdy
Mar 22 at 17:52
@Hamdy edited, let me know! Good luck!
– Pier Giorgio Misley
Mar 23 at 11:40
Yes it is excellent thanks
– Hamdy
Mar 23 at 12:16
add a comment |
line 48: tvTipAmount.setText(getString(R.string.main_msg_tipTotal,tipTotal));
– Hamdy
Mar 22 at 17:52
@Hamdy edited, let me know! Good luck!
– Pier Giorgio Misley
Mar 23 at 11:40
Yes it is excellent thanks
– Hamdy
Mar 23 at 12:16
line 48: tvTipAmount.setText(getString(R.string.main_msg_tipTotal,tipTotal));
– Hamdy
Mar 22 at 17:52
line 48: tvTipAmount.setText(getString(R.string.main_msg_tipTotal,tipTotal));
– Hamdy
Mar 22 at 17:52
@Hamdy edited, let me know! Good luck!
– Pier Giorgio Misley
Mar 23 at 11:40
@Hamdy edited, let me know! Good luck!
– Pier Giorgio Misley
Mar 23 at 11:40
Yes it is excellent thanks
– Hamdy
Mar 23 at 12:16
Yes it is excellent thanks
– Hamdy
Mar 23 at 12:16
add a comment |
As an addition to Pier's answer with some Android basics, is tvTipPercent being initialized, for example tvTipPercent = findViewById(R.id.tip_percent); or something which is an assignment to those variables, before line 40 inside onCreate()?
If no (because you didn't post the full MainActivity.java), then it is very possible that the root reason is here. Assign them.
add a comment |
As an addition to Pier's answer with some Android basics, is tvTipPercent being initialized, for example tvTipPercent = findViewById(R.id.tip_percent); or something which is an assignment to those variables, before line 40 inside onCreate()?
If no (because you didn't post the full MainActivity.java), then it is very possible that the root reason is here. Assign them.
add a comment |
As an addition to Pier's answer with some Android basics, is tvTipPercent being initialized, for example tvTipPercent = findViewById(R.id.tip_percent); or something which is an assignment to those variables, before line 40 inside onCreate()?
If no (because you didn't post the full MainActivity.java), then it is very possible that the root reason is here. Assign them.
As an addition to Pier's answer with some Android basics, is tvTipPercent being initialized, for example tvTipPercent = findViewById(R.id.tip_percent); or something which is an assignment to those variables, before line 40 inside onCreate()?
If no (because you didn't post the full MainActivity.java), then it is very possible that the root reason is here. Assign them.
answered Mar 23 at 9:18
Geno ChenGeno Chen
2,85061125
2,85061125
add a comment |
add a comment |
Use below code.
tvTipPercen.setText(getString(R.string.main_msg_tipPercent)+" "+Percentage);
tvBillTotalAmoun.setText(getString(R.string.main_msg_billTotalResult )+" "+finalBillAmount));
tvTipAmoun.setText(getString(R.string.main_msg_tipTotal)+" "+tipTotal));
percentage ,finalBillAmount and tip Total replace placeholder in strings
– Hamdy
Mar 22 at 18:31
Then you can directly set text on Textview.
– Bhavesh Shiyani
Mar 22 at 18:38
tvTipAmoun.setText(String.valueOf(tipTotal));
– Bhavesh Shiyani
Mar 22 at 18:40
You must convert float value into string
– Bhavesh Shiyani
Mar 22 at 18:41
my problem when i try to add this code into method and add it inside oncreate unlike when i use butterknife i can add method
– Hamdy
Mar 22 at 20:11
add a comment |
Use below code.
tvTipPercen.setText(getString(R.string.main_msg_tipPercent)+" "+Percentage);
tvBillTotalAmoun.setText(getString(R.string.main_msg_billTotalResult )+" "+finalBillAmount));
tvTipAmoun.setText(getString(R.string.main_msg_tipTotal)+" "+tipTotal));
percentage ,finalBillAmount and tip Total replace placeholder in strings
– Hamdy
Mar 22 at 18:31
Then you can directly set text on Textview.
– Bhavesh Shiyani
Mar 22 at 18:38
tvTipAmoun.setText(String.valueOf(tipTotal));
– Bhavesh Shiyani
Mar 22 at 18:40
You must convert float value into string
– Bhavesh Shiyani
Mar 22 at 18:41
my problem when i try to add this code into method and add it inside oncreate unlike when i use butterknife i can add method
– Hamdy
Mar 22 at 20:11
add a comment |
Use below code.
tvTipPercen.setText(getString(R.string.main_msg_tipPercent)+" "+Percentage);
tvBillTotalAmoun.setText(getString(R.string.main_msg_billTotalResult )+" "+finalBillAmount));
tvTipAmoun.setText(getString(R.string.main_msg_tipTotal)+" "+tipTotal));
Use below code.
tvTipPercen.setText(getString(R.string.main_msg_tipPercent)+" "+Percentage);
tvBillTotalAmoun.setText(getString(R.string.main_msg_billTotalResult )+" "+finalBillAmount));
tvTipAmoun.setText(getString(R.string.main_msg_tipTotal)+" "+tipTotal));
edited Mar 23 at 17:04
Nilesh Rathod
35.3k83565
35.3k83565
answered Mar 22 at 17:13
Bhavesh ShiyaniBhavesh Shiyani
654
654
percentage ,finalBillAmount and tip Total replace placeholder in strings
– Hamdy
Mar 22 at 18:31
Then you can directly set text on Textview.
– Bhavesh Shiyani
Mar 22 at 18:38
tvTipAmoun.setText(String.valueOf(tipTotal));
– Bhavesh Shiyani
Mar 22 at 18:40
You must convert float value into string
– Bhavesh Shiyani
Mar 22 at 18:41
my problem when i try to add this code into method and add it inside oncreate unlike when i use butterknife i can add method
– Hamdy
Mar 22 at 20:11
add a comment |
percentage ,finalBillAmount and tip Total replace placeholder in strings
– Hamdy
Mar 22 at 18:31
Then you can directly set text on Textview.
– Bhavesh Shiyani
Mar 22 at 18:38
tvTipAmoun.setText(String.valueOf(tipTotal));
– Bhavesh Shiyani
Mar 22 at 18:40
You must convert float value into string
– Bhavesh Shiyani
Mar 22 at 18:41
my problem when i try to add this code into method and add it inside oncreate unlike when i use butterknife i can add method
– Hamdy
Mar 22 at 20:11
percentage ,finalBillAmount and tip Total replace placeholder in strings
– Hamdy
Mar 22 at 18:31
percentage ,finalBillAmount and tip Total replace placeholder in strings
– Hamdy
Mar 22 at 18:31
Then you can directly set text on Textview.
– Bhavesh Shiyani
Mar 22 at 18:38
Then you can directly set text on Textview.
– Bhavesh Shiyani
Mar 22 at 18:38
tvTipAmoun.setText(String.valueOf(tipTotal));
– Bhavesh Shiyani
Mar 22 at 18:40
tvTipAmoun.setText(String.valueOf(tipTotal));
– Bhavesh Shiyani
Mar 22 at 18:40
You must convert float value into string
– Bhavesh Shiyani
Mar 22 at 18:41
You must convert float value into string
– Bhavesh Shiyani
Mar 22 at 18:41
my problem when i try to add this code into method and add it inside oncreate unlike when i use butterknife i can add method
– Hamdy
Mar 22 at 20:11
my problem when i try to add this code into method and add it inside oncreate unlike when i use butterknife i can add method
– Hamdy
Mar 22 at 20:11
add a comment |
Thanks for contributing an answer to Stack Overflow!
- Please be sure to answer the question. Provide details and share your research!
But avoid …
- Asking for help, clarification, or responding to other answers.
- Making statements based on opinion; back them up with references or personal experience.
To learn more, see our tips on writing great answers.
Sign up or log in
StackExchange.ready(function ()
StackExchange.helpers.onClickDraftSave('#login-link');
);
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
StackExchange.ready(
function ()
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f55304228%2fwhy-cant-i-add-methods-inside-oncreate-because-of-annotation%23new-answer', 'question_page');
);
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function ()
StackExchange.helpers.onClickDraftSave('#login-link');
);
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function ()
StackExchange.helpers.onClickDraftSave('#login-link');
);
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function ()
StackExchange.helpers.onClickDraftSave('#login-link');
);
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown