I have already posted here yesterday about this app, this activity is very like the one i posted, but still i have an error, and i don't know how to solve it. The problem is that when i open the app and choose the Velocity from the menu, it's forced close.
Velocity.java:
package com.bben.physics;
import android.os.Bundle;
import android.app.Activity;
import android.view.Menu;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;
import android.widget.TextView;
public class Velocity extends Activity {
EditText X0, X1, T0, T1;
Button VeloB;
TextView VeloResult;
double dX, dT, velo;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_acceleration);
X0 = (EditText) findViewById(R.id.X0);
X1 = (EditText) findViewById(R.id.X1);
T0 = (EditText) findViewById(R.id.T0);
T1 = (EditText) findViewById(R.id.T1);
VeloB = (Button) findViewById(R.id.VeloB);
VeloResult = (TextView) findViewById(R.id.VeloResult);
if (VeloB == null) {
System.out.println("VeloB = null");
} else {
VeloB.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
dX = (Double.parseDouble(X0.getText().toString()))
- (Double.parseDouble(X1.getText().toString()));
dT = (Double.parseDouble(T0.getText().toString()))
- (Double.parseDouble(T1.getText().toString()));
velo = dX / dT;
VeloResult.setText(Double.toString(velo));
}
});
}
}
@Override
public boolean onCreateOptionsMenu(Menu menu) {
// Inflate the menu; this adds items to the action bar if it is present.
getMenuInflater().inflate(R.menu.acceleration, menu);
return true;
}
}
activity_velocity.xml:
<LinearLayout 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"
tools:context=".Acceleration" >
<TextView
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:gravity="center"
android:paddingTop="10dp"
android:singleLine="true"
android:text="@string/velocity"
android:textSize="30dp" />
<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:gravity="center"
android:orientation="horizontal"
android:paddingTop="40dp"
android:weightSum="100" >
<EditText
android:id="@+id/X0"
android:layout_width="15dp"
android:layout_height="wrap_content"
android:layout_weight="30"
android:digits=".-0123456789"
android:ems="10"
android:hint="x0"
android:inputType="numberSigned"
android:singleLine="true" />
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="-"
android:textSize="30dp" />
<EditText
android:id="@+id/X1"
android:layout_width="15dp"
android:layout_height="wrap_content"
android:layout_weight="30"
android:digits=".-0123456789"
android:ems="10"
android:hint="x1"
android:inputType="numberSigned"
android:singleLine="true" />
</LinearLayout>
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_gravity="center"
android:text="_______________________________" />
<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:gravity="center"
android:orientation="horizontal"
android:paddingBottom="20dp"
android:paddingTop="10dp"
android:weightSum="100" >
<EditText
android:id="@+id/T0"
android:layout_width="15dp"
android:layout_height="wrap_content"
android:layout_weight="30"
android:digits=".-0123456789"
android:ems="10"
android:hint="t0"
android:inputType="numberSigned"
android:singleLine="true" />
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="-"
android:textSize="30dp" />
<EditText
android:id="@+id/T1"
android:layout_width="15dp"
android:layout_height="wrap_content"
android:layout_weight="30"
android:digits=".-0123456789"
android:ems="10"
android:hint="t1"
android:inputType="numberSigned"
android:singleLine="true" />
</LinearLayout>
<Button
android:id="@+id/VeloCalcB"
android:layout_width="wrap_content"
android:layout_height="45dp"
android:layout_gravity="center"
android:text="חשב"
android:textSize="20dp" />
<LinearLayout
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:gravity="center"
android:orientation="horizontal"
android:paddingTop="30dp"
android:weightSum="100" >
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_weight="50"
android:paddingLeft="20dp"
android:text="Result: "
android:textSize="20dp" />
<TextView
android:id="@+id/tvVeloResult"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_weight="50"
android:paddingLeft="20dp"
android:text=""
android:textSize="20dp" />
</LinearLayout>
</LinearLayout>
The Logcat:
09-19 02:41:50.022: D/AndroidRuntime(26165): Shutting down VM
09-19 02:41:50.022: W/dalvikvm(26165): threadid=1: thread exiting with uncaught exception (group=0x2aac8578)
09-19 02:41:50.022: E/AndroidRuntime(26165): FATAL EXCEPTION: main
09-19 02:41:50.022: E/AndroidRuntime(26165): java.lang.RuntimeException: Unable to start activity ComponentInfo{com.bben.physics/com.bben.physics.Velocity}: java.lang.NullPointerException
09-19 02:41:50.022: E/AndroidRuntime(26165): at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:1659)
09-19 02:41:50.022: E/AndroidRuntime(26165): at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:1675)
09-19 02:41:50.022: E/AndroidRuntime(26165): at android.app.ActivityThread.access$1500(ActivityThread.java:121)
09-19 02:41:50.022: E/AndroidRuntime(26165): at android.app.ActivityThread$H.handleMessage(ActivityThread.java:943)
09-19 02:41:50.022: E/AndroidRuntime(26165): at android.os.Handler.dispatchMessage(Handler.java:99)
09-19 02:41:50.022: E/AndroidRuntime(26165): at android.os.Looper.loop(Looper.java:130)
09-19 02:41:50.022: E/AndroidRuntime(26165): at android.app.ActivityThread.main(ActivityThread.java:3701)
09-19 02:41:50.022: E/AndroidRuntime(26165): at java.lang.reflect.Method.invokeNative(Native Method)
09-19 02:41:50.022: E/AndroidRuntime(26165): at java.lang.reflect.Method.invoke(Method.java:507)
09-19 02:41:50.022: E/AndroidRuntime(26165): at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:866)
09-19 02:41:50.022: E/AndroidRuntime(26165): at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:624)
09-19 02:41:50.022: E/AndroidRuntime(26165): at dalvik.system.NativeStart.main(Native Method)
09-19 02:41:50.022: E/AndroidRuntime(26165): Caused by: java.lang.NullPointerException
09-19 02:41:50.022: E/AndroidRuntime(26165): at com.bben.physics.Velocity.onCreate(Velocity.java:25)
09-19 02:41:50.022: E/AndroidRuntime(26165): at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1047)
09-19 02:41:50.022: E/AndroidRuntime(26165): at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:1623)
09-19 02:41:50.022: E/AndroidRuntime(26165): ... 11 more
Indeed, as Greg has said in his answer, findViewById returns null. This might have happend because the XML you posted here is not the one you're referring to in your code:
setContentView(R.layout.activity_acceleration);
You should probably replace activity_acceleration
with activity_velocity
.