This question already has answers here:
What is a NullPointerException, and how do I fix it?
(12 answers)
Closed 6 years ago.
Basically, I'm trying to make this eating healthy app based on their BMI. The calculator is done but now I'm stuck at this where the user clicks on one of these 3 buttons and that list in the Spinner item and the ImageView changes accordingly (if user clicks breakfast button, then the spinner list and image change to breakfast and so). Even the image is not working. The app runs but when I click the button it terminates and I don't know how to fix it. if it's possible, can you guys also let me know the way I'm doing my Spinner will work or not?
MainActivity:
public class MainActivity extends AppCompatActivity implements View.OnClickListener {
Button Toch;
Button BTbtn;
Button LunchBtn;
Button DinnerBtn;
Button Submit;
EditText inweight;
EditText inheight;
EditText inage;
TextView BMR;
RadioButton rdM;
RadioButton rdF;
ImageView maimage;
Spinner spinner;
/**
Button Submit = (Butt
* ATTENTION: This was auto-generated to implement the App Indexing API.
* See https://g.co/AppIndexing/AndroidStudio for more information.
*/
private GoogleApiClient client;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
final Button Submit = (Button)findViewById(R.id.Sumbit);
final Button Toch = (Button) findViewById(R.id.Toch);
Toch.setOnClickListener(this);
final Button BTbtn = (Button) findViewById(R.id.BTbtn);
BTbtn.setOnClickListener(this);
final Button LunchBtn = (Button) findViewById(R.id.LunchBtn);
LunchBtn.setOnClickListener(this);
final Button DinnerBtn = (Button) findViewById(R.id.DinnerBtn);
DinnerBtn.setOnClickListener(this);
//EditText
final EditText inweight = (EditText) findViewById(R.id.inweight);
final EditText inheight = (EditText) findViewById(R.id.inheight);
final EditText inage = (EditText) findViewById(R.id.inage);
//Text View
final TextView BMR = (TextView) findViewById(R.id.BMR);
// final TextView FDName=(TextView)findViewById(R.id.FDName);
//RadioButton
final RadioButton rdM = (RadioButton) findViewById(R.id.rdM);
final RadioButton rdF = (RadioButton) findViewById(R.id.rdF);
//ImageView
final ImageView maimage = (ImageView) findViewById(R.id.maimage);
}
public void onClick(View view) {
switch (view.getId()) {
case R.id.BTbtn: {
maimage.setImageResource(R.drawable.breakfast);
//ArrayAdapter bList = ArrayAdapter.createFromResource(getApplicationContext(), android.R.array.breakfastList, android.R.layout.simple_dropdown_item_1line);
}
case R.id.LunchBtn: {
maimage.setImageResource(R.drawable.dinner);
//ArrayAdapter lList = ArrayAdapter.createFromResource(getApplicationContext(), android.R.array.lunchList, android.R.layout.simple_dropdown_item_1line);
}
case R.id.DinnerBtn: {
maimage.setImageResource(R.drawable.lunch);
//ArrayAdapter dList = ArrayAdapter.createFromResource(getApplicationContext(), android.R.array.dinnerList, android.R.layout.simple_dropdown_item_1line);
}
case R.id.Sumbit: {
double weight = Double.parseDouble(inweight.getText().toString());
double height = Double.parseDouble(inheight.getText().toString());
double age = Double.parseDouble(inage.getText().toString());
double gender;
if (rdM.isChecked()) {
gender = 66;
double ans = gender + (13.7 * weight) + (5 * height) - (6.8 * age);
BMR.setText("" + (int) ans);
}
if (rdF.isChecked()) {
gender = 655;
double ans = gender + (9.6 * weight) + (1.8 * height) - (4.7 * age);
BMR.setText("Your Calories require : " + (int) ans);
}
}
case R.id.Toch: {
inweight.setHint(R.string.chweight);
inheight.setHint(R.string.chhight);
inage.setHint(R.string.chage);
}
}
}
}
R.layout.activity_main:
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:paddingBottom="#dimen/activity_vertical_margin"
android:paddingLeft="#dimen/activity_horizontal_margin"
android:paddingRight="#dimen/activity_horizontal_margin"
android:paddingTop="#dimen/activity_vertical_margin"
tools:context="com.example.cmleu_000.myapplication.MainActivity">
<TextView
android:text="Food Name:"
android:textSize="18dp"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:id="#+id/textView" />
<Button
android:id="#+id/BTbtn"
android:text="Breakfast"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_above="#+id/LunchBtn"
android:layout_alignRight="#+id/radioGroup2"
android:layout_alignEnd="#+id/radioGroup2" />
<Button
android:id="#+id/LunchBtn"
android:text="Lunch"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_above="#+id/DinnerBtn"
android:layout_alignRight="#+id/BTbtn"
android:layout_alignEnd="#+id/BTbtn" />
<Button
android:id="#+id/DinnerBtn"
android:text="Dinner"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignBottom="#+id/maimage"
android:layout_alignLeft="#+id/LunchBtn"
android:layout_alignStart="#+id/LunchBtn" />
<ImageView
android:id="#+id/maimage"
android:src="#drawable/dinner"
android:layout_width="150dp"
android:layout_height="120dp"
android:layout_above="#+id/qweight"
android:layout_alignLeft="#+id/qhight"
android:layout_alignStart="#+id/qhight" />
<RadioGroup
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_above="#+id/Sumbit"
android:layout_alignRight="#+id/Sumbit"
android:layout_alignEnd="#+id/Sumbit"
android:id="#+id/radioGroup2">
<RadioButton
android:id="#+id/rdF"
android:text="F"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignTop="#+id/rdM"
android:layout_toRightOf="#+id/rdM"
android:layout_toEndOf="#+id/rdM"
android:layout_gravity="left" />
<RadioButton
android:id="#+id/rdM"
android:text="M"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignTop="#+id/radioGroup"
android:layout_toRightOf="#+id/qage"
android:layout_toEndOf="#+id/qage"
android:layout_gravity="left" />
</RadioGroup>
<TextView
android:text="Your weight"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:id="#+id/qweight"
android:layout_centerVertical="true"
android:layout_alignParentLeft="true"
android:layout_alignParentStart="true" />
<EditText
android:id="#+id/inweight"
android:hint="Plase input your weight (KG)"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_above="#+id/qhight"
android:layout_alignParentLeft="true"
android:layout_alignParentStart="true" />
<TextView
android:text="Your height"
android:id="#+id/qhight"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_above="#+id/inheight"
android:layout_alignRight="#+id/qweight"
android:layout_alignEnd="#+id/qweight" />
<EditText
android:id="#+id/inheight"
android:hint="Please input your height (CM)"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_above="#+id/qage"
android:layout_alignRight="#+id/inweight"
android:layout_alignEnd="#+id/inweight" />
<TextView
android:text="Your age"
android:id="#+id/qage"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_above="#+id/inage"
android:layout_alignLeft="#+id/BMR"
android:layout_alignStart="#+id/BMR" />
<EditText
android:id="#+id/inage"
android:hint="Please input your age"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_above="#+id/Sumbit"
android:layout_alignParentLeft="true"
android:layout_alignParentStart="true" />
<Button
android:id="#+id/Sumbit"
android:text="Submit"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentBottom="true"
android:layout_alignParentRight="true"
android:layout_alignParentEnd="true" />
<TextView
android:text="Your BMR"
android:id="#+id/BMR"
android:textStyle="bold"
android:textSize="10pt"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentBottom="true"
android:layout_alignLeft="#+id/inheight"
android:layout_alignStart="#+id/inheight" />
<Button
android:text="中文"
android:id="#+id/Toch"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignBottom="#+id/BMR"
android:layout_toLeftOf="#+id/Sumbit"
android:layout_toStartOf="#+id/Sumbit" />
<Spinner
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:id="#+id/spinner"
android:layout_above="#+id/BTbtn"
android:layout_alignParentLeft="true"
android:layout_alignParentStart="true" />
</RelativeLayout>
Logcat:
08-24 22:43:52.039 3104-3104/com.example.cmleu_000.myapplication E/AndroidRuntime: FATAL EXCEPTION: main
Process: com.example.cmleu_000.myapplication, PID: 3104
java.lang.NumberFormatException: Invalid double: ""
at java.lang.StringToReal.invalidReal(StringToReal.java:63)
at java.lang.StringToReal.parseDouble(StringToReal.java:267)
at java.lang.Double.parseDouble(Double.java:301)
at com.example.cmleu_000.myapplication.MainActivity.onClick(MainActivity.java:92)
at android.view.View.performClick(View.java:5198)
at android.view.View$PerformClick.run(View.java:21147)
at android.os.Handler.handleCallback(Handler.java:739)
at android.os.Handler.dispatchMessage(Handler.java:95)
at android.os.Looper.loop(Looper.java:148)
at android.app.ActivityThread.main(ActivityThread.java:5417)
at java.lang.reflect.Method.invoke(Native Method)
at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:726)
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:616)
08-24 22:43:53.601 3104-3110/com.example.cmleu_000.myapplication W/art: Suspending all threads took: 8.662ms
08-24 22:44:10.123 3104-3110/com.example.cmleu_000.myapplication W/art: Suspending all threads took: 5.787ms
*new error after edditing
You've defined the maimage variable twice. The class variable maimage that you're trying to access in the onClick(...) method is null (it was never initialized).
To fix this, change:
final ImageView maimage = (ImageView) findViewById(R.id.maimage);
to:
maimage = (ImageView) findViewById(R.id.maimage);
This way you will initialize the class variable instead of defining a new variable with the same name.
The same goes for all the other variables that you're defining again in the onCreate(...) method.
Related
Although I've checked the solutions for the related questions in this website, I couldn't solve my problem. I'm trying to build a quiz app that takes the correct answer, adds 1 to the score and updates the score on the score TextView. I tried calling the score method through android:onClick and also tried the setOnClickListener methods but none of them seem to work.
This is my XML:
<?xml version="1.0" encoding="utf-8"?>
<ScrollView xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent">
<LinearLayout
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical"
android:paddingBottom="#dimen/activity_vertical_margin"
android:paddingLeft="#dimen/activity_horizontal_margin"
android:paddingRight="#dimen/activity_horizontal_margin"
android:paddingTop="#dimen/activity_vertical_margin">
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Welcome to the Canada Quiz"
android:textSize="16sp"
android:textColor="#000000"
android:textStyle="bold"
android:layout_marginBottom="16dp"
android:layout_gravity="center"/>
<EditText
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:hint="Enter name"
android:layout_marginBottom="8dp"
/>
<TextView
android:id="#+id/scoreText"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginLeft="4dp"
android:layout_marginBottom="16dp"
android:textColor="#000000"
android:textStyle="bold"
/>
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="1. What is the capital of Canada?"
android:textSize="20dp"
android:textColor="#000000"
android:textStyle="bold"
/>
<RadioGroup
android:layout_width="wrap_content"
android:layout_height="wrap_content">
<RadioButton
android:id="#+id/tokyo"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Tokyo"
android:layout_marginLeft="24dp"
android:textStyle="bold"
android:textSize="16dp"/>
<RadioButton
android:id="#+id/newYork"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="New York"
android:layout_marginLeft="24dp"
android:textStyle="bold"
android:textSize="16dp"/>
<RadioButton
android:id="#+id/ottawa"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Ottawa"
android:layout_marginLeft="24dp"
android:textStyle="bold"
android:textSize="16dp"/>
<RadioButton
android:id="#+id/hongKong"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Hong Kong"
android:layout_marginLeft="24dp"
android:textStyle="bold"
android:onClick="calculateScore"
android:textSize="16dp"/>
</RadioGroup>
<Button
android:id="#+id/scoreButton"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_gravity="center"
android:text="Calculate score"
android:layout_marginTop="16dp"
/>
</LinearLayout>
</ScrollView>
And this is my Java:
public class MainActivity extends AppCompatActivity {
int score = 0;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
RadioButton rb = (RadioButton)findViewById(R.id.ottawa);
Button calculate = (Button) findViewById(R.id.scoreButton);
final TextView scoreShow = (TextView) findViewById(R.id.scoreText);
final boolean rbChecked = rb.isChecked();
calculate.setOnClickListener(new View.OnClickListener(){
public void onClick (View v){
if(rbChecked){
score += 1;
scoreShow.setText("Your score is: " + score + "/10");
}
}
});
}
// public void calculateScore(){
// RadioButton rb = (RadioButton)findViewById(R.id.ottawa);
// //Button calculate = (Button) findViewById(R.id.scoreButton);
// TextView scoreShow = (TextView) findViewById(R.id.scoreText);
//
// boolean rbChecked = rb.isChecked();
// if(rbChecked){
// score += 1;
// scoreShow.setText("Your score is: " + score + "/10");
// }
// }
}
Honestly, it really looks like it would work but it doesn't.
You are only storing the value of the checked state when the view is loaded and it never is updated.
Always check rb.isChecked() inside the click listener instead of storing the boolean value outside of it.
It should be:
if(rbChecked.isChecked()){
score += 1;
scoreShow.setText("Your score is: " + score + "/10");
}
you need to check for the state of radiobutton when user click on calculate button....your code should be like this...
calculate.setOnClickListener(new View.OnClickListener(){
public void onClick (View v){
rbChecked = rb.isChecked();
if(rbChecked){
score += 1;
scoreShow.setText("Your score is: " + score + "/10");
}
}
});
Hello fellow programmers!
I have a very disturbing situation here...I'm developing an Android App with several activities.In them I have buttons.The app itself is for measuring calories and other sports releated stuff.
I have a calorie calculator which I have built in the app,spread across 2 activities.In activity 1,the calculator measures the Basal Metabolic Rate and In the second activity,the app calculates the calories needed,when clicking a button for the desired activity level.My app crashes in the second activity,when clicking "Sedentary" button and I have no clue why,because these buttons are built just like the buttons in my first activity.I'll post photos and my code so you can get orientation.
Btw,the app crashes also when clicking "Null Data" in the first activity with the BMR...The button doesn't have any coding on it and does nothing.I'm not calling calculate() method in the second activity,instead I tried doing the button click in the main method,but it still didn't work.
Hello fellow programmers!
I have a very disturbing situation here...I'm developing an Android App with several activities.In them I have buttons.The app itself is for measuring calories and other sports releated stuff.
I have a calorie calculator which I have built in the app,spread across 2 activities.In activity 1,the calculator measures the Basal Metabolic Rate and In the second activity,the app calculates the calories needed,when clicking a button for the desired activity level.My app crashes in the second activity,when clicking "Sedentary" button and I have no clue why,because these buttons are built just like the buttons in my first activity.I'll post photos and my code so you can get orientation.
Btw,the app crashes also when clicking "Null Data" in the first activity with the BMR...The button doesn't have any coding on it and does nothing.I'm not calling calculate() method in the second activity,instead I tried doing the button click in the main method,but it still didn't work.First ActivitySecond Activity
`package com.petartonkov.foodandsportsinfo;
import android.content.Intent;
import android.os.Bundle;
import android.os.Parcelable;
import android.support.v7.app.AppCompatActivity;
import android.support.v7.widget.Toolbar;
import android.view.View;
import android.widget.Button;
import android.widget.CheckBox;
import android.widget.EditText;
import android.widget.TextView;
public class CalorieCalculator extends AppCompatActivity {
public Button nextstepbtn;
public TextView BMR;
public EditText height;
public EditText weight;
public EditText age;
public void calculate() {
final Button calculateBMR = (Button) findViewById(R.id.CalcBtn);
final TextView BMR = (TextView) findViewById(R.id.textView_BMR);
final EditText weight = (EditText) findViewById(R.id.editText_weight);
final EditText height = (EditText) findViewById(R.id.editText_height);
final EditText age = (EditText) findViewById(R.id.editText_age);
assert calculateBMR != null;
calculateBMR.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
assert weight != null;
float weight1 = Float.parseFloat(weight.getText().toString());
assert height != null;
float height1 = Float.parseFloat(height.getText().toString());
assert age != null;
float age1 = Float.parseFloat(age.getText().toString());
float BMR1 = (13.75f * weight1 + 5.003f * height1 - 6.755f * age1) + 66.75f;
assert BMR != null;
BMR.setText(Float.toString(BMR1));
}
});
}
public void calculate1() {
final Button calculateBMR = (Button) findViewById(R.id.CalcBtnFemale);
final TextView BMR = (TextView) findViewById(R.id.textView_BMR);
final EditText weight = (EditText) findViewById(R.id.editText_weight);
final EditText height = (EditText) findViewById(R.id.editText_height);
final EditText age = (EditText) findViewById(R.id.editText_age);
assert calculateBMR != null;
calculateBMR.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
assert weight != null;
float weight1 = Float.parseFloat(weight.getText().toString());
assert height != null;
float height1 = Float.parseFloat(height.getText().toString());
assert age != null;
float age1 = Float.parseFloat(age.getText().toString());
float BMR1 = (9.563f * weight1 + 1.85f * height1 - 4.676f * age1) + 655.1f;
assert BMR != null;
BMR.setText(Float.toString(BMR1));
}
});
}
public void init4(){
nextstepbtn = (Button)findViewById(R.id.NextStepBtn);
nextstepbtn.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
Intent intent1 = new Intent(getBaseContext(), CalorieCalculatorSecondActivity.class);
intent1.putExtra("BMR", (Parcelable) BMR);
startActivity(intent1);
}
});
}
#Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_calorie_calculator);
Toolbar toolbar = (Toolbar) findViewById(R.id.toolbar);
setSupportActionBar(toolbar);
init4();
calculate();
calculate1();
}
}
`
` package com.petartonkov.foodandsportsinfo;
import android.os.Bundle;
import android.support.design.widget.FloatingActionButton;
import android.support.design.widget.Snackbar;
import android.support.v7.app.AppCompatActivity;
import android.support.v7.widget.Toolbar;
import android.view.View;
import android.widget.Button;
import android.widget.TextView;
public class CalorieCalculatorSecondActivity extends CalorieCalculator {
public void totalcalories() {
final Button calculateBtn = (Button) findViewById(R.id.button_sedentary);
final TextView totalcalories = (TextView) findViewById(R.id.textView_totalcalories);
final TextView BMR = (TextView) findViewById(R.id.textView_BMR);
assert calculateBtn != null;
calculateBtn.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
assert BMR != null;
float BMR2 = Float.parseFloat(BMR.getText().toString());
float totalcalories1 = BMR2 * 1.2f;
assert totalcalories != null;
totalcalories.setText(Float.toString(totalcalories1));
}
});
}
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_calorie_calculator_second);
Toolbar toolbar = (Toolbar) findViewById(R.id.toolbar);
setSupportActionBar(toolbar);
Bundle extras = getIntent().getExtras();
final Button calcBtn = (Button)findViewById(R.id.button_sedentary);
final TextView totalcalories2 = (TextView)findViewById(R.id.textView_totalcalories);
final TextView BMR1 = (TextView) findViewById(R.id.textView_BMR);
assert calcBtn != null;
calcBtn.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
assert BMR1 != null;
float BMR3 = Float.parseFloat(BMR1.getText().toString());
float totalcalories4 = BMR3 * 1.2f;
assert totalcalories2 != null;
totalcalories2.setText(Float.toString(totalcalories4));
}
});
}
}
`
`
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:textAppearance="?android:attr/textAppearanceMedium"
android:text="Enter your data below to calculate your BMR(Basal Metabolic Rate) and then enter your level of physical activity to determine your daily calorie needs.BMR-this number is the calories needed from the body to maintain basic living functions."
android:id="#+id/textView2"
android:textColor="#040404"
android:layout_alignParentTop="true"
android:layout_alignParentLeft="true"
android:layout_alignParentStart="true"
android:layout_marginTop="30dp" />
<Button
style="?android:attr/buttonStyleSmall"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="go to next step"
android:id="#+id/NextStepBtn"
android:layout_alignParentBottom="true"
android:layout_centerHorizontal="true" />
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:textAppearance="?android:attr/textAppearanceMedium"
android:text="BMR:"
android:id="#+id/textView11"
android:textColor="#070707"
android:layout_below="#+id/CalcBtn"
android:layout_alignParentLeft="true"
android:layout_alignParentStart="true"
android:layout_marginTop="26dp" />
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:textAppearance="?android:attr/textAppearanceMedium"
android:text="CAL"
android:id="#+id/textView13"
android:layout_alignTop="#+id/textView_BMR"
android:layout_centerHorizontal="true"
android:textColor="#980606" />
<Button
style="?android:attr/buttonStyleSmall"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="NULL DATA"
android:id="#+id/btn_nulldata"
android:layout_alignBaseline="#+id/NextStepBtn"
android:layout_alignBottom="#+id/NextStepBtn"
android:layout_alignRight="#+id/textView2"
android:layout_alignEnd="#+id/textView2"
android:onClick="onCheckboxClicked"/>
<EditText
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:inputType="numberDecimal"
android:ems="10"
android:id="#+id/editText_age"
android:layout_alignParentLeft="true"
android:layout_alignParentStart="true"
android:layout_marginTop="27dp"
android:hint="Enter age"
android:layout_below="#+id/textView2" />
<EditText
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:inputType="numberDecimal"
android:ems="10"
android:id="#+id/editText_weight"
android:layout_below="#+id/editText_age"
android:layout_alignParentLeft="true"
android:layout_alignParentStart="true"
android:hint="Enter weight(In KG)"
android:layout_alignRight="#+id/NextStepBtn"
android:layout_alignEnd="#+id/NextStepBtn" />
<EditText
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:inputType="numberDecimal"
android:ems="10"
android:id="#+id/editText_height"
android:layout_below="#+id/editText_weight"
android:layout_alignParentLeft="true"
android:layout_alignParentStart="true"
android:hint="Enter height(In CM)" />
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:textAppearance="?android:attr/textAppearanceMedium"
android:id="#+id/textView_BMR"
android:layout_alignTop="#+id/textView11"
android:layout_toLeftOf="#+id/NextStepBtn"
android:layout_toStartOf="#+id/NextStepBtn"
android:textColor="#980606" />
<Button
style="?android:attr/buttonStyleSmall"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="CALCULATE BMR-MALE"
android:id="#+id/CalcBtn"
android:background="?attr/colorPrimary"
android:textColor="#ffffff"
android:layout_marginTop="31dp"
android:layout_below="#+id/editText_height"
android:layout_toLeftOf="#+id/textView13"
android:layout_toStartOf="#+id/textView13" />
<Button
style="?android:attr/buttonStyleSmall"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="CALCULATE BMR-FEMALE"
android:id="#+id/CalcBtnFemale"
android:background="?attr/colorPrimary"
android:textColor="#ffffff"
android:layout_alignBottom="#+id/CalcBtn"
android:layout_toRightOf="#+id/textView13"
android:layout_toEndOf="#+id/textView13" />
</RelativeLayout>`
`
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:textAppearance="?android:attr/textAppearanceMedium"
android:text="SELECT YOUR LEVEL OF PHYSICAL ACTIVITY:"
android:id="#+id/textView10"
android:layout_alignParentTop="true"
android:layout_alignParentLeft="true"
android:layout_alignParentStart="true" />
<Button
style="?android:attr/buttonStyleSmall"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="sedentary"
android:id="#+id/button_sedentary"
android:layout_below="#+id/textView10"
android:layout_alignParentLeft="true"
android:layout_alignParentStart="true"
android:clickable="true"
android:enabled="true"
android:onClick="setContentView" />
<Button
style="?android:attr/buttonStyleSmall"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="moderately active"
android:id="#+id/button_moderatelyactive"
android:layout_alignTop="#+id/button_sedentary"
android:layout_alignRight="#+id/textView14"
android:layout_alignEnd="#+id/textView14" />
<Button
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Very active"
android:id="#+id/button_veryactive"
android:layout_below="#+id/button_sedentary"
android:layout_alignParentLeft="true"
android:layout_alignParentStart="true" />
<Button
style="?android:attr/buttonStyleSmall"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Extremely active"
android:id="#+id/button_extremelyactive"
android:layout_alignBottom="#+id/button_veryactive"
android:layout_alignLeft="#+id/button_moderatelyactive"
android:layout_alignStart="#+id/button_moderatelyactive" />
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:textAppearance="?android:attr/textAppearanceSmall"
android:text="Sedentary - Little or no exercise,little walking"
android:id="#+id/textView14"
android:layout_below="#+id/button_veryactive"
android:layout_alignParentLeft="true"
android:layout_alignParentStart="true"
android:textColor="#e10e0e" />
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:textAppearance="?android:attr/textAppearanceSmall"
android:text="Lighty active - light exercise 1-3 times/week + walking"
android:id="#+id/textView15"
android:layout_below="#+id/textView14"
android:layout_alignParentLeft="true"
android:layout_alignParentStart="true"
android:textColor="#e10e0e" />
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:textAppearance="?android:attr/textAppearanceSmall"
android:text="Moderately active - exercise 3-5 times/week + walking"
android:id="#+id/textView16"
android:layout_below="#+id/textView15"
android:layout_alignParentLeft="true"
android:layout_alignParentStart="true"
android:textColor="#e10e0e" />
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:textAppearance="?android:attr/textAppearanceSmall"
android:text="Very active - hard exercise 5-7 times/week + walking"
android:id="#+id/textView17"
android:textColor="#e10e0e"
android:layout_below="#+id/textView16"
android:layout_alignParentLeft="true"
android:layout_alignParentStart="true" />
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:textAppearance="?android:attr/textAppearanceMedium"
android:text="TOTAL CALORIES NEEDED:"
android:id="#+id/textView18"
android:layout_centerVertical="true"
android:layout_alignParentLeft="true"
android:layout_alignParentStart="true"
android:textColor="#080808" />
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:textAppearance="?android:attr/textAppearanceMedium"
android:id="#+id/textView_totalcalories"
android:layout_alignTop="#+id/textView18"
android:layout_toRightOf="#+id/textView18"
android:layout_toEndOf="#+id/textView18"
android:editable="false"
android:enabled="false"
android:text="0" />
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:textAppearance="?android:attr/textAppearanceMedium"
android:text="CAL"
android:id="#+id/textView20"
android:layout_alignBottom="#+id/textView_totalcalories"
android:layout_alignParentRight="true"
android:layout_alignParentEnd="true"
android:textColor="#090909" />
<Button
style="?android:attr/buttonStyleSmall"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="NULL DATA"
android:id="#+id/btn_nulldata2"
android:layout_marginBottom="48dp"
android:layout_alignParentBottom="true"
android:layout_alignRight="#+id/button_sedentary"
android:layout_alignEnd="#+id/button_sedentary" />
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:textAppearance="?android:attr/textAppearanceSmall"
android:text="Extra active - hard exercise 5-7 times/week + physical job + walking"
android:id="#+id/textView3"
android:textColor="#e10e0e"
android:layout_below="#+id/textView17"
android:layout_alignParentLeft="true"
android:layout_alignParentStart="true" />
<Button
style="?android:attr/buttonStyleSmall"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Lightly active"
android:id="#+id/button"
android:layout_alignBottom="#+id/button_extremelyactive"
android:layout_alignParentRight="true"
android:layout_alignParentEnd="true"
android:layout_toRightOf="#+id/button_extremelyactive"
android:layout_toEndOf="#+id/button_extremelyactive" />
</RelativeLayout>`
`
--------- beginning of crash
06-14 06:10:35.167 2481-2481/com.petartonkov.foodandsportsinfo E/AndroidRuntime: FATAL EXCEPTION: main
Process: com.petartonkov.foodandsportsinfo, PID: 2481
java.lang.NullPointerException: Attempt to invoke virtual method 'java.lang.CharSequence android.widget.TextView.getText()' on a null object reference
at com.petartonkov.foodandsportsinfo.CalorieCalculatorSecondActivity$2.onClick(CalorieCalculatorSecondActivity.java:44)
at android.view.View.performClick(View.java:5198)
at android.view.View$PerformClick.run(View.java:21147)
at android.os.Handler.handleCallback(Handler.java:739)
at android.os.Handler.dispatchMessage(Handler.java:95)
at android.os.Looper.loop(Looper.java:148)
at android.app.ActivityThread.main(ActivityThread.java:5417)
at java.lang.reflect.Method.invoke(Native Method)
at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:726)
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:616)
`
This metod is throwing
calcBtn.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
assert BMR1 != null;
float BMR3 = Float.parseFloat(BMR1.getText().toString());
float totalcalories4 = BMR3 * 1.2f;
assert totalcalories2 != null;
totalcalories2.setText(Float.toString(totalcalories4));
}
});
You have no TextView (BMR1) with the id of textView_BMR in your secondActivity layout
To pass the value from one activity to another you can use Intent extra
Intent extra is a bundle of additional information you want to pass from one activity to another. You can create extras like this
//First Activity
Intent i = new Intent(FirstActiviy.this, SecondActivity.class);
String value = "some string you want to put in the extra";
int value2 = 14
//put extra as key-value pairs
i.putExtra("KEY", value);
i.putExtra("KEY2", value2);
startActivity(i);
//Second activity
//Get the values from previous Activity by keys you set
String stringValueFromPreviousActivity = getIntent().getStringExtra("key");
//Note: when you getting integer as the second parameter pass the default value if there is no passed integer
int intValueFromPreviousActiviy = getIntent().getIntExtra("key",4);
I tried setting up the calculator in one activity...I rearanged the buttons and guess what..?My float from BMR TextView is NULL,althrough I have a string in BMR TextView.Why is this happening?I wrote the code for the total calories in another method,in the first activity.
public void calculatetotalcalories()
{
final Button calcbtn_sedentary = (Button)findViewById(R.id.button_sedentary);
final TextView totalcalories = (TextView)findViewById(R.id.textView_totalcalories);
calcbtn_sedentary.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
float BMRconverted= Float.parseFloat(String.valueOf(BMR));
float totalcalories1 =BMRconverted*1.2f;
totalcalories.setText(Float.toString(totalcalories1));
}
});
I solved the problem.It was an null object reference in the start of the activity.Everything now works!
I'm trying to convert a string to a float.
Basically, I have an edittext that's numerical only, and I'm trying to get the input of it and do some math with it before outputting the result onto another edittext.
Here is my main activity.
public class MainActivity extends Activity {
#Override
protected void onCreate(Bundle savedInstanceState) {
//Declare views
EditText ptv = (EditText) findViewById(R.id.priceet);
EditText ttv = (EditText) findViewById(R.id.taxet);
EditText totaltv = (EditText) findViewById(R.id.totalet);
//Declare variables
float price = 0;
price = Float.valueOf(ptv.getText().toString());
float tax = 0.0F;
tax = Float.valueOf(ttv.getText().toString()) / 100;
float total = price*tax + price;
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
}
Here is my activity_main.xml
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools" android:layout_width="match_parent"
android:layout_height="match_parent" android:paddingLeft="#dimen/activity_horizontal_margin"
android:paddingRight="#dimen/activity_horizontal_margin"
android:paddingTop="#dimen/activity_vertical_margin"
android:paddingBottom="#dimen/activity_vertical_margin" tools:context=".MainActivity">
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:textAppearance="?android:listSeparatorTextViewStyle"
android:text="Price"
android:id="#+id/cost"
android:layout_alignParentEnd="true"
android:layout_alignParentTop="true"
android:layout_alignStart="#+id/priceet" />
<EditText
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:inputType="numberDecimal"
android:ems="10"
android:id="#+id/priceet"
android:layout_below="#+id/cost"
android:layout_alignParentStart="true"
android:layout_alignParentEnd="true" />
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:textAppearance="?android:listSeparatorTextViewStyle"
android:text="Tax %"
android:id="#+id/taxtv"
android:layout_below="#+id/priceet"
android:layout_alignParentStart="true" />
<EditText
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:inputType="numberDecimal"
android:ems="10"
android:id="#+id/taxet"
android:layout_below="#+id/taxtv"
android:layout_alignParentStart="true"
android:layout_alignEnd="#+id/totalet" />
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:textAppearance="?android:listSeparatorTextViewStyle"
android:text="Total"
android:id="#+id/totaltv"
android:layout_below="#+id/taxet"
android:layout_alignParentStart="true" />
<EditText
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:inputType="numberDecimal"
android:ems="10"
android:id="#+id/totalet"
android:layout_below="#+id/totaltv"
android:layout_alignParentStart="true"
android:clickable="false"
android:cursorVisible="false"
android:focusable="false"
android:focusableInTouchMode="false"
android:layout_alignEnd="#+id/cost" />
and here is the error I'm getting
java.lang.RuntimeException: Unable to start activity ComponentInfo{com.jimlarck.taxcalculator/com.jimlarck.taxcalculator.MainActivity}: java.lang.NullPointerException: Attempt to invoke virtual method 'android.text.Editable android.widget.EditText.getText()' on a null object reference
The error points to this line of code:
price = Float.valueOf(ptv.getText().toString());
How would I fix this? I read somewhere around here that this error is thrown when something isn't initialized properly but everything looks fine on my end. Any help is appreciated. Thank you :)
You need to instantiate your EditTexts after you call super.onCreate() and setContentView():
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
//Declare views
EditText ptv = (EditText) findViewById(R.id.priceet);
EditText ttv = (EditText) findViewById(R.id.taxet);
EditText totaltv = (EditText) findViewById(R.id.totalet);
//Declare variables
float price = 0;
price = Float.valueOf(ptv.getText().toString());
float tax = 0.0F;
tax = Float.valueOf(ttv.getText().toString()) / 100;
float total = price*tax + price;
}
I am trying to have the user fill out a form then those fields auto-populate into an email. I was able to convert the EditText to strings as well as the spinner, but I am confused as to how to do it with RadioGroup. I want the text from the button which is selected to appear in the email. I know that getText won't work, but what will. Thank you
JAVA
//This happens when you press the submit button at the bottom of the form.
public void submit(View button) {
// Do click handling here
//What happens here is the input from each field is taken in and converted into
//Strings and placed into their correct variables.
final EditText FirstNameField = (EditText) findViewById(R.id.EditTextFirstName);
fName = FirstNameField.getText().toString();
final EditText LastNameField = (EditText) findViewById(R.id.EditTextLastName);
lName = LastNameField.getText().toString();
final EditText emailField = (EditText) findViewById(R.id.EditTextEmail);
email = emailField.getText().toString();
final EditText feedbackField = (EditText) findViewById(R.id.EditTextFeedbackBody);
feedback = feedbackField.getText().toString();
final Spinner feedbackSpinner = (Spinner) findViewById(R.id.SpinnerFeedbackType);
feedbackType = feedbackSpinner.getSelectedItem().toString();
final RadioGroup ApproveorReject = (RadioGroup) findViewById(R.id.radiochoice);
fAnswer = ApproveorReject.getText().toString();
//calls the sendEmail() from below to pull up a list of apps installed
//on the phone that can handle emailing.
sendEmail();
}
//This bit of code pulls up a list of apps that can handle emailing.
private void sendEmail() {
//When the user chooses an app of the list that pops up, these lines below
//will auto-populate the fields of the email with the input from above.
//The user can take one last look at the email before hitting that send button
Intent i = new Intent(Intent.ACTION_SEND);
i.setType("message/rfc822");
i.putExtra(Intent.EXTRA_EMAIL, new String[]{"recipient#example.com"});
i.putExtra(Intent.EXTRA_SUBJECT, feedbackType);
i.putExtra(Intent.EXTRA_TEXT, lName + ", " + fName + "\n" + email + "\n" + feedback + fAnswer );
startActivity(Intent.createChooser(i, "Send mail..."));
}
}
XML
<ScrollView
xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:scrollbars="vertical">
<RelativeLayout
android:layout_width="fill_parent"
android:orientation="vertical"
android:layout_height="wrap_content">
<TextView
android:id="#+id/TextViewTitle"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentTop="true"
android:text="#string/feedbacktitle"
android:textSize="10pt">
</TextView>
<!--First Name-->
<EditText
android:id="#+id/EditTextFirstName"
android:layout_height="wrap_content"
android:hint="#string/feedbackfirst"
android:inputType="textPersonName"
android:layout_width="fill_parent"
android:layout_below="#+id/TextViewTitle">
</EditText>
<!--Last Name-->
<EditText
android:id="#+id/EditTextLastName"
android:layout_height="wrap_content"
android:hint="#string/feedbacklast"
android:inputType="textPersonName"
android:layout_width="fill_parent"
android:layout_below="#id/EditTextFirstName">
</EditText>
<!-- Email -->
<EditText
android:id="#+id/EditTextEmail"
android:layout_height="wrap_content"
android:hint="#string/feedbackemail"
android:inputType="textEmailAddress"
android:layout_width="fill_parent"
android:layout_below="#id/EditTextLastName">
</EditText>
<Spinner
android:id="#+id/SpinnerFeedbackType"
android:layout_height="wrap_content"
android:layout_width="match_parent"
android:entries="#array/Types_of_Errors"
android:layout_below="#+id/EditTextEmail">
</Spinner>
<EditText
android:id="#+id/EditTextFeedbackBody"
android:layout_height="wrap_content"
android:hint="#string/feedbackbody"
android:inputType="textMultiLine"
android:lines="5"
android:layout_width="fill_parent"
android:layout_below="#+id/SpinnerFeedbackType">
</EditText>
<RadioGroup
android:id="#+id/radiochoice"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:layout_below="#+id/EditTextFeedbackBody"
android:orientation="horizontal">
<RadioButton
android:id="#+id/AcceptRadio"
android:layout_width="wrap_content"
android:layout_marginStart="50dp"
android:layout_height="wrap_content"
android:text="#string/acceptbutton" />
<RadioButton
android:id="#+id/RejectRadio"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="#string/rejectbutton"
android:layout_marginStart="115dp" />
</RadioGroup>
<EditText
android:id="#+id/RejectResponse"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:hint="#string/rejectreason"
android:layout_marginTop="410dp"
android:layout_alignParentEnd="true"
android:layout_marginRight="20dp"
android:inputType="text"/>
<Button
android:id="#+id/ButtonSend"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:text="#string/sendform"
android:layout_centerHorizontal="true"
android:layout_marginTop="590dp"
android:onClick="submit"/>
</RelativeLayout>
Try this
RadioButton btn=(RadioButton)findViewById(ApproveorReject.getCheckedRadioButtonId());
String selectedText=btn.getText().toString();
Try
ApproveorReject.setOnCheckedChangeListener(new RadioGroup.OnCheckedChangeListener()
{
public void onCheckedChanged(RadioGroup group, int checkedId) {
RadioButton rb=(RadioButton)findViewById(checkedId);
String param = rb.getText();
}
});
I'm doing a very simple login screen where the user inputs their username and password, clicks the login button, and then the inputs are displayed on the next screen.
I get a NullPointerException every time I try to change the textview values. I've been googling for a long time and come up with nothing, and it has to be something simple that I'm just completely missing.
Following is my code:
public class LoggedIn extends Activity{
String username = "";
String password = "";
TextView name;
TextView pwd;
Button infoDisp;
protected void onCreate(Bundle savedInstanceState){
super.onCreate(savedInstanceState);
setContentView(R.layout.logged_in);
Intent intent = getIntent();
name = (TextView)findViewById(R.id.username);
pwd = (TextView)findViewById(R.id.password);
username = intent.getStringExtra("nameInfo");
password = intent.getStringExtra("passInfo");
name.setText(username);
pwd.setText(password);
}
}
Edit: I changed the bundle to an intent above, also here's the rest of the code (The first activity)
public class LoginActivity extends Activity {
Button loginBtn;
Button registerBtn;
EditText username;
EditText pword;
static String name;
static String pass;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_login);
username = (EditText) this.findViewById(R.id.username);
pword = (EditText) this.findViewById(R.id.password);
loginBtn = (Button) this.findViewById(R.id.loginBtn);
loginBtn.setOnClickListener(new OnClickListener() {
public void onClick(View arg0){
Intent intent = new Intent(LoginActivity.this, LoggedIn.class);
name = username.getText().toString();
pass = pword.getText().toString();
intent.putExtra("nameInfo", name);
intent.putExtra("passInfo", pass);
startActivity(intent);
}
});
}
}
I have no error checking for if the value is null on purpose because this was supposed to be just a quick run and done thing. I figure as long as I input something in each EditText, then the strings can't be null and I won't have a problem... right?
Stacktrace:
FATAL EXCEPTION: main
Process: com.example.loginscreen, PID: 1677
java.lang.RuntimeException: Unable to start activity ComponentInfo{com.example.loginscreen/com.example.loginscreen.LoggedIn}: java.lang.NullPointerException
at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2176)
at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2226)
at android.app.ActivityThread.access$700(ActivityThread.java:135)
at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1397)
at android.os.Handler.dispatchMessage(Handler.java:102)
at android.os.Looper.loop(Looper.java:137)
at android.app.ActivityThread.main(ActivityThread.java:4998)
at java.lang.reflect.Method.invokeNative(Native Method)
at java.lang.reflect.Method.invoke(Method.java:515)
at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:777)
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:593)
at dalvik.system.NativeStart.main(Native Method)
Caused by: java.lang.NullPointerException
at com.example.loginscreen.LoggedIn.onCreate(LoggedIn.java:30)
at android.app.Activity.performCreate(Activity.java:5243)
at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1087)
at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2140)
... 11 more
Aaaand xml files:
activity_login:
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:paddingBottom="#dimen/activity_vertical_margin"
android:paddingLeft="#dimen/activity_horizontal_margin"
android:paddingRight="#dimen/activity_horizontal_margin"
android:paddingTop="#dimen/activity_vertical_margin"
tools:context=".LoginActivity" >
<TextView
android:id="#+id/textView1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentLeft="true"
android:text="Login:"
android:textAppearance="?android:attr/textAppearanceMedium" />
<TextView
android:id="#+id/textView2"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignLeft="#+id/textView1"
android:layout_below="#+id/textView1"
android:layout_marginTop="38dp"
android:text="Password:"
android:textAppearance="?android:attr/textAppearanceMedium" />
<EditText
android:id="#+id/username"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_above="#+id/textView2"
android:layout_alignParentTop="true"
android:layout_marginLeft="20dp"
android:layout_toRightOf="#+id/textView1"
android:ems="10" />
<EditText
android:id="#+id/password"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignBottom="#+id/textView2"
android:layout_alignParentRight="true"
android:layout_toRightOf="#+id/textView2"
android:ems="10"
android:inputType="textPassword" >
<requestFocus />
</EditText>
<Button
android:id="#+id/loginBtn"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignLeft="#+id/textView2"
android:layout_below="#+id/textView2"
android:layout_marginTop="26dp"
android:text="Login" />
<Button
android:id="#+id/registerBtn"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignBaseline="#+id/loginBtn"
android:layout_alignBottom="#+id/loginBtn"
android:layout_alignLeft="#+id/textView3"
android:layout_marginLeft="46dp"
android:text="Register" />
<CheckBox
android:id="#+id/remPass"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignLeft="#+id/loginBtn"
android:layout_below="#+id/loginBtn"
android:layout_marginTop="19dp"
android:text="Remember Password" />
<TextView
android:id="#+id/textView3"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_below="#+id/remPass"
android:layout_marginTop="28dp"
android:layout_toRightOf="#+id/textView1"
android:text="Forgot Password" />
logged_in:
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:paddingBottom="#dimen/activity_vertical_margin"
android:paddingLeft="#dimen/activity_horizontal_margin"
android:paddingRight="#dimen/activity_horizontal_margin"
android:paddingTop="#dimen/activity_vertical_margin"
tools:context=".LoginActivity" >
<TextView
android:id="#+id/textView1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentTop="true"
android:layout_centerHorizontal="true"
android:text="Logged In!"
android:textAppearance="?android:attr/textAppearanceLarge" />
<TextView
android:id="#+id/textView2"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_below="#+id/textView1"
android:layout_marginTop="56dp"
android:text="Username: "
android:textAppearance="?android:attr/textAppearanceLarge" />
<TextView
android:id="#+id/textView3"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignLeft="#+id/textView2"
android:layout_below="#+id/textView2"
android:layout_marginTop="56dp"
android:text="Password: "
android:textAppearance="?android:attr/textAppearanceLarge" />
<TextView
android:id="#+id/recUsername"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignBottom="#+id/textView2"
android:layout_marginLeft="33dp"
android:layout_toRightOf="#+id/textView2"
android:editable="true"
android:text=" "
android:textAppearance="?android:attr/textAppearanceLarge" />
<TextView
android:id="#+id/recPassword"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignBottom="#+id/textView3"
android:layout_alignRight="#+id/textView4"
android:editable="true"
android:text=" "
android:textAppearance="?android:attr/textAppearanceLarge" />
<Button
android:id="#+id/infoBtn"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignLeft="#+id/textView1"
android:layout_alignRight="#+id/textView1"
android:layout_below="#+id/textView3"
android:layout_marginTop="62dp"
android:text="Press to Display Info" />
The problem is because you are finding id for TextView s are wrong.. so just change from
username = (TextView) this.findViewById(R.id.username);
pword = (TextView) this.findViewById(R.id.password);
to
username = (TextView) this.findViewById(R.id.recUsername);
pword = (TextView) this.findViewById(R.id.recPassword);
You should replace this in your LoggedIn Activity. You have wrong ids when initialization of views logged_in.xml
username = (EditText) this.findViewById(R.id.username);
pword = (EditText) this.findViewById(R.id.password);
loginBtn = (Button) this.findViewById(R.id.loginBtn);
With
TextView username = (TextView)findViewById(R.id.recUsername);
TextView pword = (TextView)findViewById(R.id.recPassword);
loginBtn = (Button)findViewById(R.id.infoBtn);
Change
name = (TextView)findViewById(R.id.username);
pwd = (TextView)findViewById(R.id.password);
to
name = (TextView)findViewById(R.id.recUsername);
pwd = (TextView)findViewById(R.id.recPassword);
In LoggedIn.java
Your NUllPointerExceptipon is ccoz you referencing wrong ids for your views. findViewById looks for a view in the current infalted layout. Since it does not find one. Your initialization fails leading to NullPointerExcpetion.
I think there is a problem with following line
Intent intent = new Intent(LoginActivity.this, LoggedIn.class);
change it to
Intent intent = new Intent(this, LoggedIn.class);
or
Intent intent = new Intent(getApplicationContext(), LoggedIn.class);
In your LoggedIn class the ids are wrong
name = (TextView)findViewById(R.id.username);
pwd = (TextView)findViewById(R.id.password);
change the above ids to defined ids in the logged_in.xml layout and try again
Change this...
Bundle bundle = getIntent().getExtras();
to...
Intent bundle = getIntent();
Then use this to retrieve extras....
username = bundle.getStringExtra("nameInfo");
password = bundle.getStringExtra("passInfo");
Update:
You are trying to get TextViews using those ids which does not exist in the current layout xml...that is, you are using wrong id to retrieve your Textviews. So, change those ids in these following lines...
username = (TextView) this.findViewById(R.id.username);
pword = (TextView) this.findViewById(R.id.password);
to
username = (TextView) this.findViewById(R.id.recUsername);
pword = (TextView) this.findViewById(R.id.recPassword);