I have a simple application to multiply two numbers from text boxes and display result in third box. There is no any syntax errors in the code but when I am running an application i get this error: application has stopped unexpectedly.
Here is the java code:
package c.example.rectangle;
import android.os.Bundle;
import android.view.View;
import android.app.Activity;
import android.view.View.OnClickListener;
import android.widget.Button;
import android.widget.EditText;
import android.widget.TextView;
public class MainActivity extends Activity implements OnClickListener{
EditText l = (EditText) findViewById(R.id.length);
EditText w = (EditText) findViewById(R.id.width);
TextView a = (TextView) findViewById(R.id.lblarea);
Button b = (Button) findViewById(R.id.calculate);
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
b.setOnClickListener(this);
}
public void onClick(View v) {
calculateRectangle(l.getText().toString(), w.getText().toString());
}
private void calculateRectangle(String clength, String cwidth){
int area = Integer.parseInt(clength)*Integer.parseInt(cwidth);
b.setText(String.valueOf(area));
}}
And here is my XML file.
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:background="#8B4513"
android:orientation="vertical" >
<TextView
android:id="#+id/label1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_gravity="center_horizontal"
android:layout_marginTop="20dp"
android:background="#2F4F4F"
android:gravity="center"
android:text="#string/rect"
android:textAppearance="?android:attr/textAppearanceMedium" />
<LinearLayout
xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:background="#8B4513"
android:orientation="horizontal" >
<TextView
android:id="#+id/label2"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginLeft="30dp"
android:layout_marginTop="50dp"
android:background="#2F4F4F"
android:gravity="center"
android:text="#string/cm"
android:textAppearance="?android:attr/textAppearanceMedium" />
<EditText
android:id="#+id/length"
android:layout_width="110dp"
android:layout_height="21dp"
android:layout_marginLeft="40dp"
android:layout_marginTop="50dp"
android:background="#2F4F4F"
android:ems="10"
android:gravity="center"
android:inputType="number" />
</LinearLayout>
<LinearLayout
xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:background="#8B4513"
android:orientation="horizontal" >
<TextView
android:id="#+id/label3"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:background="#2F4F4F"
android:layout_marginLeft="30dp"
android:layout_marginTop="20dp"
android:text="#string/breadth"
android:textAppearance="?android:attr/textAppearanceMedium" />
<EditText
android:id="#+id/width"
android:layout_width="110dp"
android:layout_height="21dp"
android:layout_marginLeft="33dp"
android:layout_marginTop="20dp"
android:background="#2F4F4F"
android:inputType="number"
android:ems="10"
android:gravity="center"
>
<requestFocus />
</EditText>
</LinearLayout>
<Button
android:id="#+id/calculate"
android:layout_width="fill_parent"
android:layout_marginLeft="100dip"
android:layout_marginRight="100dip"
android:layout_height="wrap_content"
android:text="#string/calculate"
android:layout_marginTop="20dp" />
<LinearLayout
xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:background="#8B4513"
android:orientation="horizontal" >
<TextView
android:id="#+id/label4"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginLeft="30dp"
android:layout_marginTop="20dp"
android:background="#2F4F4F"
android:text="#string/area"
android:textAppearance="?android:attr/textAppearanceMedium" />
<TextView
android:id="#+id/lblarea"
android:layout_width="110dp"
android:layout_height="21dp"
android:layout_marginLeft="60dp"
android:layout_marginTop="20dp"
android:background="#2F4F4F"
android:gravity="center"/>
</LinearLayout>
</LinearLayout>
Please help.
I know, that you got correct answer, but I just want to explain you why you need to write code as #Mr.Me says.
You describe your View elements in start of class, and trying to initialize them there. It is not correct. Because you have not attached layout file to activity at the moment when constructor will run your initialization of Views objects. As you can see, you are using findViewById() method, but before use it you should call setContentView().
For better understanding, read Activity Lifecycle, pay attention to rendering proccess.
Rearrange your code to look like this:
EditText l;
EditText w;
TextView a;
Button b;
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
l = (EditText) findViewById(R.id.length);
w = (EditText) findViewById(R.id.width);
a = (TextView) findViewById(R.id.lblarea);
b = (Button) findViewById(R.id.calculate);
Related
I am really new to java and I came across the following 2 errors error: illegal start of expression public void registerClick(View v) and error: class, interface, or enum expected }
I don't understand what I am doing wrong. I have watched extensive tutorials on Youtube and gone to the Android Studio docs but I can not find out what is wrong with my program. I am trying to go from a signup screen to a register screen if a certain text view is clicked. My code is down below.
Main Activity.java:
package com.example.gooddeed;
import androidx.appcompat.app.ActionBar;
import androidx.appcompat.app.AppCompatActivity;
import android.os.Bundle;
import android.view.View;
import android.view.WindowManager;
import android.graphics.Color;
import android.os.Bundle;
import android.view.Menu;
import android.view.MenuItem;
import android.widget.TextView;
import android.view.View;
public class MainActivity extends AppCompatActivity {
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
//Config Stuff
ActionBar actionBar = getSupportActionBar();
actionBar.hide();
//end of config stuff
public void registerClick(View v)
{
TextView tv= (TextView) findViewById(R.id.register);
}
}
}
Activity Main.Xml
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
tools:context=".MainActivity">
<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:orientation="vertical">
<FrameLayout
android:layout_width="match_parent"
android:layout_height="235dp"
android:background="#drawable/blue_bg" />
<RelativeLayout
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginLeft="62dp"
android:layout_marginTop="36.7dp"
android:layout_marginRight="62dp"
android:background="#drawable/blue_border_rounded_cornwe">
<!-- INPUT -->
<EditText
android:id="#+id/email_input"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_toRightOf="#+id/username_icon"
android:background="#android:color/transparent"
android:fontFamily="#font/poppins_regular"
android:hint="Email"
android:inputType="textEmailAddress"
android:maxLines="1"
android:paddingLeft="17dp"
android:paddingTop="15dp"
android:paddingBottom="15dp"
android:textCursorDrawable="#drawable/cursor_color"
android:textSize="12sp" />
<!-- ICON -->
<ImageView
android:id="#+id/username_icon"
android:layout_width="14.7dp"
android:layout_height="10.3dp"
android:layout_centerVertical="true"
android:layout_marginLeft="17dp"
android:src="#drawable/email" />
</RelativeLayout>
<!-- Spacer Button -->
<!-- Spacer Button -->
<TextView
android:id="#+id/textView"
android:layout_width="match_parent"
android:layout_height="14dp" />
<RelativeLayout
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginLeft="62dp"
android:layout_marginTop="12.7dp"
android:layout_marginRight="62dp"
android:background="#drawable/blue_border_rounded_cornwe">
<!-- INPUT -->
<EditText
android:id="#+id/password_input"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_toRightOf="#+id/icon"
android:background="#android:color/transparent"
android:fontFamily="#font/poppins_regular"
android:hint="Password"
android:inputType="textPassword"
android:maxLength="10"
android:maxLines="1"
android:paddingLeft="17dp"
android:paddingTop="15dp"
android:paddingBottom="15dp"
android:textCursorDrawable="#drawable/cursor_color"
android:textSize="12sp" />
<!-- ICON -->
<ImageView
android:id="#+id/icon"
android:layout_width="14.7dp"
android:layout_height="10.3dp"
android:layout_centerVertical="true"
android:layout_marginLeft="17dp"
android:src="#drawable/password" />
</RelativeLayout>
<TextView
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_gravity="center"
android:layout_marginLeft="62dp"
android:layout_marginTop="18.7dp"
android:layout_marginRight="62dp"
android:background="#drawable/blue_fill__rounded_color"
android:fontFamily="#font/poppins_medium"
android:gravity="center"
android:paddingTop="14dp"
android:paddingBottom="14dp"
android:text="Login"
android:textColor="#ffffff"
android:textSize="14sp"
android:textStyle="bold" />
<TextView
android:id="#+id/textView3"
android:layout_width="match_parent"
android:layout_height="wrap_content" />
<TextView
android:id="#+id/forgotpassword"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_gravity="center"
android:layout_marginTop="12dp"
android:fontFamily="#font/poppins_medium"
android:text="FORGOT PASSWORD?"
android:textColor="#1566e0"
android:textSize="10.5sp" />
<TextView
android:id="#+id/register"
android:layout_width="wrap_content"
android:layout_height="18dp"
android:layout_gravity="center"
android:layout_marginTop="12dp"
android:fontFamily="#font/poppins_medium"
android:text="Don't have an account? Make one for free!"
android:textColor="#1566e0"
android:textSize="13sp"
android:clickable="true"
android:onClick="registerClick"
/>
</LinearLayout>
<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_alignParentBottom="true"
android:orientation="vertical">
<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_marginBottom="13.7dp"
android:gravity="center"
android:orientation="horizontal"/>
</LinearLayout>
</RelativeLayout>
Thank you to everyone that helps!
You should have registerClick as a separate method inside the activity because Java does not support directly nested methods
public class MainActivity extends AppCompatActivity {
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
//Config Stuff
ActionBar actionBar = getSupportActionBar();
actionBar.hide();
//end of config stuff
}
public void registerClick(View v) {
TextView tv= (TextView) findViewById(R.id.register);
}
}
I am try to save my roll no and name in main activity but when my screen is rotated the activity data has been lose ,
finally i fixed it , below i adding source code
consider any layout , here i am using this layout structure
In activity_xml :
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical"
android:padding="20dp"
tools:context=".MainActivity">
<LinearLayout
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:orientation="horizontal">
<TextView
android:id="#+id/rollnoID"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginLeft="15dp"
android:layout_marginTop="10dp"
android:text="Rollno : "
android:textSize="25dp" />
<EditText
android:hint="Roll Number"
android:id="#+id/rollnoID_edit"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginLeft="15dp"
android:layout_marginTop="10dp"
android:ems="8"
android:inputType="number"
android:text=""
android:textSize="25dp" />
</LinearLayout>
<LinearLayout
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:orientation="horizontal">
<TextView
android:id="#+id/nameID"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginLeft="15dp"
android:layout_marginTop="10dp"
android:text="Name : "
android:textSize="25dp" />
<EditText
android:hint="Student Name"
android:id="#+id/nameID_edit"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginLeft="15dp"
android:layout_marginTop="10dp"
android:ems="8"
android:inputType="textCapWords"
android:text=""
android:textSize="25dp" />
</LinearLayout>
<Button
android:onClick="saveData"
android:id="#+id/saveButton"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:text="save this Data"
android:textSize="20sp" />
<LinearLayout
android:layout_width="match_parent"
android:layout_height="match_parent"
android:layout_marginTop="20dp"
android:padding="20dp"
android:orientation="vertical">
<TextView
android:gravity="center"
android:hint="~RollNo~"
android:textColor="#color/colorPrimary"
android:id="#+id/display_rollno"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:textSize="30sp" />
<TextView
android:layout_marginTop="20dp"
android:id="#+id/display_name"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:gravity="center"
android:hint="~Name~"
android:textColor="#color/colorPrimary"
android:textSize="30sp" />
</LinearLayout>
</LinearLayout>
In MainActivity :
import androidx.appcompat.app.AppCompatActivity;
import android.os.Bundle;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;
import android.widget.TextView;
import android.widget.Toast;
public class MainActivity extends AppCompatActivity {
private static final String KEY_ROLL = "rollno_key";
private static final String KEY_NAME = "name_key";
int rollno;
String name;
EditText roll_et , name_et;
TextView roll_tv , name_tv;
Button saveButton;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
roll_et = (EditText) findViewById(R.id.rollnoID_edit);
name_et = (EditText) findViewById(R.id.nameID_edit);
roll_tv = (TextView) findViewById(R.id.display_rollno);
name_tv = (TextView) findViewById(R.id.display_name);
if (savedInstanceState != null){
String save_RollNO = savedInstanceState.getString(KEY_ROLL);
roll_tv.setText(save_RollNO);
String save_Name = savedInstanceState.getString(KEY_NAME);
name_tv.setText(save_Name);
} else {
Toast.makeText(getApplicationContext(),"Entry your data",Toast.LENGTH_SHORT).show();
}
}
#Override
public void onSaveInstanceState(Bundle saveData) {
saveData.putString(KEY_ROLL , roll_et.getText().toString());
saveData.putString(KEY_NAME , name_et.getText().toString());
super.onSaveInstanceState(saveData);
}
public void saveData(View v){
roll_tv.setText(roll_et.getText().toString().trim());
name_tv.setText(name_et.getText().toString().trim());
}
}
This question already has answers here:
What is a NullPointerException, and how do I fix it?
(12 answers)
Closed 6 years ago.
I may have some troubles finding where the problem is ... Here is my activity_main.xml :
<?xml version="1.0" encoding="utf-8"?>
<FrameLayout 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: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"
tools:context="com.grasland.test.MainActivity">
<RelativeLayout
android:layout_width="match_parent"
android:layout_height="match_parent">
<TextView
android:id="#+id/poids"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_centerHorizontal="true"
android:text="#string/poids"
android:textColor="#FF0000"
android:textStyle="bold"
android:textSize="20sp" />
<EditText
android:id="#+id/editPoids"
android:layout_width="match_parent"
android:layout_height="50sp"
android:layout_below="#id/poids"
android:hint="#string/editPoids"
android:inputType="number"/>
<TextView
android:id="#+id/taille"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_below="#id/editPoids"
android:layout_centerHorizontal="true"
android:text="#string/taille"
android:textColor="#FF0000"
android:textStyle="bold"
android:textSize="20sp" />
<EditText
android:id="#+id/editTaille"
android:layout_width="match_parent"
android:layout_height="50sp"
android:layout_below="#id/taille"
android:hint="#string/editTaille"
android:inputType="number"/>
<RadioGroup
android:id="#+id/radioGroup1"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_centerHorizontal="true"
android:orientation="horizontal"
android:layout_below="#id/editTaille">
<RadioButton
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="#string/radioButtonCm"
android:checked="true" />
<RadioButton
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="#string/radioButtonM"/>
</RadioGroup>
<Button
android:id="#+id/button"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_centerHorizontal="true"
android:layout_below="#id/radioGroup1"
android:text="#string/button"/>
<TextView
android:id="#+id/resultTitle"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_below="#id/button"
android:paddingTop="40dp"
android:layout_centerHorizontal="true"
android:text="#string/resultTitle"
android:textSize="20sp" />
<TextView
android:id="#+id/result"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_below="#id/resultTitle"
android:paddingTop="20dp"
android:layout_centerHorizontal="true"
android:textSize="80sp" />
</RelativeLayout>
</FrameLayout>
and there is my java file :
import android.os.Bundle;
import android.support.v7.app.AppCompatActivity;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;
import android.widget.TextView;
public class MainActivity extends AppCompatActivity {
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
Button calculImc = (Button) findViewById(R.id.button);
final EditText poidsView = (EditText) findViewById(R.id.editPoids);
final EditText tailleView = (EditText) findViewById(R.id.editTaille);
final TextView resultat = (TextView) findViewById(R.id.result);
calculImc.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
int iPoids = Integer.parseInt(poidsView.getText().toString());
int iTaille = Integer.parseInt(tailleView.getText().toString());
double dResult = iPoids / ( ( iTaille * iTaille ) / 1000 );
resultat.setText(Double.toString(dResult));
}
});
}
}
I can't see the reason for this NullPointerException I'm getting (I may be a bit tired ...).
Thanks in advance !
Sylvain.
I don't see you placing the setContentView function below onCreate
You should consider doing:
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
...
}
I've been searching about this problem a lot before posting this question but still couldnt get any help,kindly help me in solving this error as im a newbie to this field..!!
Main Java Code
package com.example.firstapp;
import com.example.firstapp.R;
import android.app.Activity;
import android.content.Intent;
import android.os.Bundle;
import android.view.Menu;
import android.view.MenuItem;
import android.view.View;
import android.widget.Button;
public class MainActivity extends Activity {
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
final Button button = (Button) findViewById(R.id.button_id);
button.setOnClickListener(new View.OnClickListener() {
public void onClick(View v) {
Intent i = new Intent(getApplicationContext(),Second.class);
startActivity(i);
}
});
}
activity_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: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.firstapp.MainActivity" >
<EditText
android:id="#+id/editText1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignLeft="#+id/imageView1"
android:layout_alignParentBottom="true"
android:layout_marginBottom="26dp"
android:ems="10"
android:inputType="numberPassword" />
<EditText
android:id="#+id/editText5"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_above="#+id/editText1"
android:layout_alignLeft="#+id/editText1"
android:ems="10"
android:inputType="textMultiLine"
android:text="Enter your Password"
tools:ignore="HardcodedText" />
<EditText
android:id="#+id/editText3"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_above="#+id/editText5"
android:layout_alignLeft="#+id/editText5"
android:layout_marginBottom="20dp"
android:ems="10"
android:inputType="textEmailAddress" >
<requestFocus android:layout_width="wrap_content" />
</EditText>
<EditText
android:id="#+id/editText4"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_above="#+id/editText3"
android:layout_alignLeft="#+id/editText3"
android:ems="10"
android:inputType="textMultiLine"
android:text="Enter your E-mail id"
tools:ignore="HardcodedText" />
<ImageView
android:id="#+id/imageView1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_above="#+id/editText4"
android:layout_centerHorizontal="true"
android:layout_marginBottom="36dp"
android:src="#drawable/robo"
android:contentDescription="#null"/>
<EditText
android:id="#+id/editText2"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentTop="true"
android:layout_centerHorizontal="true"
android:layout_marginTop="38dp"
android:ems="10"
android:inputType="textMultiLine"
android:text="Welcome To Foodparkk"
tools:ignore="HardcodedText" />
<Button
android:id="#+id/button_id"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentBottom="true"
android:layout_alignParentRight="true"
android:text="Login" />
</RelativeLayout>
activity_second.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: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.firstapp.Second" >
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="#string/hello_world" />
</RelativeLayout>
As i mentioned in my comments earlier, There is no button with id button_id..
instead you have android:id="#+id/button1" in your xml file..
so, please change
final Button button = (Button) findViewById(R.id.button_id);
to
final Button button = (Button) findViewById(R.id.button1);
in MainActivity.java
OR
change
android:id="#+id/button1"
to
android:id="#+id/button_id"
in your activity_main.xml
The button id that you are trying to reference in your Java code (using R.id.button_id) doesn't match the one you declared in your xml file (android:id="#+id/button1"). Try changing it to button1 and the error should go away.
replace R.id.button_id with R.id.button1 since this is what is present in your xml
I am fairly new to android and am writing an app that will solve physics problems. The math is going well thanks to some help from these forums, but when I try to start my activity from a list it comes up with a nullpointerexception in the onCreate Method of the new activity. This doesn't seem to make much sense though, because all thats there is a submit button that executes the math from some EditText views. Here is my code.
package android.physicsengine;
import android.app.Activity;
import android.os.Bundle;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
import android.widget.EditText;
import java.lang.Math;
public class ProjectileMotion extends Activity {
private EditText acceleration;
private EditText finalVelocity;
private EditText initialVelocity;
private EditText time;
private EditText deltay;
private EditText velocity;
private EditText deltax;
private Button submitButton;
#Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.projectile_motion_layout);
submitButton.setOnClickListener(new OnClickListener() {
#Override
public void onClick(View v) {
acceleration = (EditText)findViewById(R.id.acceleration);
double a = Doublify(acceleration);
finalVelocity = (EditText)findViewById(R.id.finalVelocity);
double vf = Doublify(finalVelocity);
initialVelocity = (EditText)findViewById(R.id.intitialVelocity);
double vi = Doublify(initialVelocity);
time = (EditText)findViewById(R.id.time);
double t = Doublify(time);
deltay = (EditText)findViewById(R.id.deltay);
double y = Doublify(deltay);
velocity = (EditText)findViewById(R.id.velocity);
double vx = Doublify(velocity);
deltax = (EditText)findViewById(R.id.deltax);
double x = Doublify(deltax);
//Y Axis
if(time.getText()==null && deltay.getText()==null){
time.setText(Double.toString((vf-vi)/a));
deltay.setText(Double.toString(((vf-vi)/a)+(a*Math.pow(((vf-vi)/a),2))));
}
if(acceleration.getText()==null && deltay.getText()==null){
acceleration.setText(Double.toString((vf-vi)/t));
deltay.setText(Double.toString((vi*t+.5*((vf-vi)/t))*Math.pow(t,2)));
}
if(acceleration.getText()==null && time.getText()==null){
acceleration.setText(Double.toString(((Math.pow(vf,2)-Math.pow(vi,2)))/2*y));
time.setText(Double.toString(2*y*(vf-vi)/(Math.pow(vf,2)-vi)));
}
if(initialVelocity.getText()==null && deltay.getText()==null){
initialVelocity.setText(Double.toString(vf-a*t));
deltay.setText(Double.toString((vf-a*t)*t+.5*a*Math.pow(t,2)));
}
if(initialVelocity.getText()==null && time.getText()==null){
initialVelocity.setText(Double.toString(Math.sqrt(Math.pow(vf,2)-2*a*y)));
time.setText(Double.toString((vf-Math.sqrt(Math.pow(vf,2)-2*a*y))/2));
}
if(initialVelocity.getText()==null && acceleration.getText()==null){
initialVelocity.setText(Double.toString(vf-2*(vf-y/t)));
acceleration.setText(Double.toString((2/t)*(vf-y/t)));
}
if(finalVelocity.getText()==null && deltay.getText()==null){
finalVelocity.setText(Double.toString(vi+a*t));
deltay.setText(Double.toString(vi*t+.5*a*Math.pow(t,2)));
}
if(finalVelocity.getText()==null && time.getText()==null){
finalVelocity.setText(Double.toString(Math.sqrt(Math.pow(vi,2)+2*a*y)));
time.setText(Double.toString(((Math.sqrt(Math.pow(vi,2)+2*a*y)-vi))/a));
}
if(finalVelocity.getText()==null && acceleration.getText()==null){
acceleration.setText(Double.toString(2*(y-vi*t)/Math.pow(t,2)));
finalVelocity.setText(Double.toString(vi+(2*(y-vi*t)/t)));
}
if(finalVelocity.getText()==null && initialVelocity.getText()==null){
initialVelocity.setText(Double.toString((y-.5*a*Math.pow(t,2))/t));
finalVelocity.setText(Double.toString((y-.5*a*Math.pow(t,2))/t)+a*t);
}
}
});
}
private double Doublify(EditText editText){
if(editText.getText()!= null){
return Double.parseDouble(editText.getText().toString());
}
return 0;
}
}
and the XML
<?xml version="1.0" encoding="utf-8"?>
<TableLayout
xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:orientation="vertical">
<TableRow>
<TextView android:text="Projectile Motion Engine"
android:textSize="25dip"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:layout_span="4"
android:layout_gravity="center" />
</TableRow>
<TableRow>
<TextView android:text="X axis"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:layout_span="2"
android:textStyle="bold"/>
<TextView android:text="Y axis"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:layout_span="2"
android:textStyle="bold"/>
</TableRow>
<TableRow>
<TextView android:text="accel(m/s^2)="
android:layout_width="fill_parent"
android:layout_height="wrap_content" />
<EditText android:id="#+id/acceleration"
android:text="9.8"
android:inputType="numberDecimal"
android:layout_width="fill_parent"
android:layout_height="wrap_content" />
<TextView android:text="deltax(m)="
android:layout_width="fill_parent"
android:layout_height="wrap_content" />
<EditText android:id="#+id/deltax"
android:inputType="numberDecimal"
android:layout_width="fill_parent"
android:layout_height="wrap_content" />
</TableRow>
<TableRow>
<TextView android:text="init v(m/s)="
android:layout_width="fill_parent"
android:layout_height="wrap_content" />
<EditText android:id="#+id/intitialVelocity"
android:inputType="numberDecimal"
android:layout_width="fill_parent"
android:layout_height="wrap_content" />
<TextView android:text="v ="
android:layout_width="fill_parent"
android:layout_height="wrap_content" />
<EditText android:id="#+id/velocity"
android:inputType="numberDecimal"
android:layout_width="fill_parent"
android:layout_height="wrap_content" />
</TableRow>
<TableRow>
<TextView android:text="final v(m/s)="
android:layout_width="fill_parent"
android:layout_height="wrap_content" />
<EditText android:id="#+id/finalVelocity"
android:inputType="numberDecimal"
android:layout_width="fill_parent"
android:layout_height="wrap_content" />
<TextView android:text="time(s)="
android:layout_width="fill_parent"
android:layout_height="wrap_content" />
<EditText android:id="#+id/time"
android:inputType="numberDecimal"
android:layout_width="fill_parent"
android:layout_height="wrap_content" />
</TableRow>
<TableRow>
<TextView android:text="deltay(m)="
android:layout_width="fill_parent"
android:layout_height="wrap_content" />
<EditText android:id="#+id/deltay"
android:inputType="numberDecimal"
android:layout_width="fill_parent"
android:layout_height="wrap_content" />
<Button android:id="#+id/submitButton"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:layout_span="2"
android:text="Launch" />
</TableRow>
</TableLayout>
super.onCreate(savedInstanceState);
setContentView(R.layout.projectile_motion_layout);
submitButton.setOnClickListener(new OnClickListener() {
You forgot to assign your submitButton (with findViewById)
super.onCreate(savedInstanceState);
setContentView(R.layout.projectile_motion_layout);
submitButton = (Button) findViewById((R.id.submitButton); // new line
submitButton.setOnClickListener(new OnClickListener() {
Based on the code you've posted, it appears that you call
submitButton.setOnClickListener(new OnClickListener() {
before instantiating submitButton with a value. This would cause a NullPointer
projectile_motion_layout may not be in your android manifest. I would check that first.
You can find the Application Nodes under the Application tab in the manifest.