Johnatan56
Honorary Master
Hi,
My class exercise is to make a basic android app that takes a name and salary on the first screen, multiplies that by 1.15 and prints it out (after clicking a button). Then it has two buttons going to other screens (screen 2 and 3). On screen 2 there are just two buttons, one going home, the other to screen 3. Screen 3 has 3 buttons, one to generate random numbers, output them and the sum of them and print a message depending on the number; the other two buttons link back home and screen 2.
The exception:
Caused by: java.lang.NullPointerException: Attempt to invoke virtual method 'android.view.View android.view.Window.findViewById(int)' on a null object reference
at android.app.Activity.findViewById(Activity.java:2072)
at marco.classexercisesexteenthsep.ScreenOne.<init>(ScreenOne.java:15)
at java.lang.reflect.Constructor.newInstance(Native Method)
The line in question is when I start this:
EditText userName = (EditText)findViewById(R.id.name);
Could you tell me what I am doing wrong? Still new to programming/android.
The code:
Screen1 (main)
XML of screen1
Screen 2
XML of screen 2
Screen 3
My class exercise is to make a basic android app that takes a name and salary on the first screen, multiplies that by 1.15 and prints it out (after clicking a button). Then it has two buttons going to other screens (screen 2 and 3). On screen 2 there are just two buttons, one going home, the other to screen 3. Screen 3 has 3 buttons, one to generate random numbers, output them and the sum of them and print a message depending on the number; the other two buttons link back home and screen 2.
The exception:
Caused by: java.lang.NullPointerException: Attempt to invoke virtual method 'android.view.View android.view.Window.findViewById(int)' on a null object reference
at android.app.Activity.findViewById(Activity.java:2072)
at marco.classexercisesexteenthsep.ScreenOne.<init>(ScreenOne.java:15)
at java.lang.reflect.Constructor.newInstance(Native Method)
The line in question is when I start this:
EditText userName = (EditText)findViewById(R.id.name);
Could you tell me what I am doing wrong? Still new to programming/android.
The code:
Screen1 (main)
Code:
package marco.classexercisesexteenthsep;
import android.content.Intent;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.view.Menu;
import android.view.MenuItem;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;
import android.widget.TextView;
public class ScreenOne extends AppCompatActivity {
EditText userName = (EditText)findViewById(R.id.name);
EditText salary = (EditText)findViewById(R.id.salary);
Double oldSalary = Double.parseDouble(salary.getText().toString());
TextView output = (TextView)findViewById(R.id.output);
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_screen_one);
Button calcSalary = (Button)findViewById(R.id.calcSalary);
calcSalary.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
Double newSalary = oldSalary * 1.15;
output.setText("The new salary for " + userName + " is: " + newSalary);
}
});
}
public void sendScreen2(){
Intent toScreen2 = new Intent(this, ScreenTwo.class);
startActivity(toScreen2);
}
public void sendScreen3(){
Intent toScreen3 = new Intent(this, ScreenThree.class);
startActivity(toScreen3);
}
@Override
public boolean onCreateOptionsMenu(Menu menu) {
// Inflate the menu; this adds items to the action bar if it is present.
getMenuInflater().inflate(R.menu.menu_screen_one, menu);
return true;
}
@Override
public boolean onOptionsItemSelected(MenuItem item) {
// Handle action bar item clicks here. The action bar will
// automatically handle clicks on the Home/Up button, so long
// as you specify a parent activity in AndroidManifest.xml.
int id = item.getItemId();
//noinspection SimplifiableIfStatement
if (id == R.id.action_settings) {
return true;
}
return super.onOptionsItemSelected(item);
}
}
Code:
<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=".ScreenOne">
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Welcome to the Home Screen"
android:id="@+id/welcomeScreen"
android:layout_alignParentTop="true"
android:layout_centerHorizontal="true"
android:layout_marginTop="35dp"
android:textSize="25dp" />
<EditText
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:inputType="textPersonName"
android:text="Enter name"
android:ems="10"
android:id="@+id/name"
android:layout_marginTop="34dp"
android:layout_below="@+id/welcomeScreen"
android:layout_alignParentLeft="true"
android:layout_alignParentStart="true" />
<EditText
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:inputType="numberDecimal"
android:ems="10"
android:id="@+id/salary"
android:text="Enter Salary"
android:layout_alignTop="@+id/name"
android:layout_toRightOf="@+id/toScreen3"
android:layout_toEndOf="@+id/toScreen3" />
<Button
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Screen 2"
android:id="@+id/toScreen2"
android:layout_centerVertical="true"
android:layout_alignParentLeft="true"
android:layout_alignParentStart="true"
android:onClick="sendScreen2"/>
<Button
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Screen 3"
android:id="@+id/toScreen3"
android:layout_alignBottom="@+id/toScreen2"
android:layout_centerHorizontal="true"
android:onClick="sendScreen3"/>
<Button
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="new Salary"
android:id="@+id/calcSalary"
android:layout_alignBottom="@+id/toScreen3"
android:layout_alignParentRight="true"
android:layout_alignParentEnd="true"
android:onClick="calcSalary"/>
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:id="@+id/output"
android:layout_below="@+id/toScreen3"
android:layout_centerHorizontal="true"
android:layout_marginTop="74dp"
android:text="Output" />
</RelativeLayout>
Screen 2
Code:
package marco.classexercisesexteenthsep;
import android.app.Activity;
import android.content.Intent;
import android.os.Bundle;
/**
* Created by Temporary on 9/15/2015.
*/
public class ScreenTwo extends Activity{
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_screen_one);
}
public void sendScreen3(){
Intent toScreen3 = new Intent(this, ScreenThree.class);
startActivity(toScreen3);
}
public void sendHome(){
Intent toHome = new Intent(this, ScreenOne.class);
startActivity(toHome);
}
}
Code:
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent" android:layout_height="match_parent">
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Welcome to Screen 2"
android:id="@+id/welcome2"
android:layout_alignParentTop="true"
android:layout_centerHorizontal="true"
android:layout_marginTop="30dp"
android:textSize="25dp" />
<Button
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Screen 3"
android:id="@+id/toScreen3"
android:layout_above="@+id/toHome"
android:layout_alignLeft="@+id/toHome"
android:layout_alignStart="@+id/toHome"
android:layout_marginBottom="35dp"
android:onClick="sendScreen3"/>
<Button
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Home"
android:id="@+id/toHome"
android:layout_centerVertical="true"
android:layout_centerHorizontal="true"
android:onClick="sendHome"/>
</RelativeLayout>
Screen 3
Code:
package marco.classexercisesexteenthsep;
import android.app.Activity;
import android.content.Intent;
import android.os.Bundle;
import android.widget.TextView;
import java.util.Random;
/**
* Created by Temporary on 9/15/2015.
*/
public class ScreenThree extends Activity {
final TextView output = (TextView) findViewById(R.id.output);
Random rand = new Random();
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_screen_three);
}
public void sendScreen2(){
Intent toScreen2 = new Intent(this, ScreenTwo.class);
startActivity(toScreen2);
}
public void sendHome(){
Intent toHome = new Intent(this, ScreenOne.class);
startActivity(toHome);
}
public void generateNum(){
float num1, num2, num3, num4, numTotal;
String numProp;
num1 = rand.nextFloat();
num2 = rand.nextFloat();
num3 = rand.nextFloat();
num4 = rand.nextFloat();
numTotal = num1 + num2 + num3 + num4;
if(numTotal < 5){
numProp = "lower number";
}
else numProp = "higher number";
output.setText(num1 + "\n" + num2 + "\n" + num3 + "\n" + num4 + "\n" + numTotal + "\n" + numProp);
}
}