I'm studying Android and I want to make a simple parameter pass for another Activity but the application is terminated in the emulator and the logCat shows a NullPointerException, can anyone help me?
Class:
public class MainActivity extends Activity {
EditText num1 = (EditText) findViewById(R.id.txtNumber1);
EditText num2 = (EditText) findViewById(R.id.txtNumber2);
String n1= num1.getText().toString();
int numero1 = Integer.parseInt(n1);
String n2= num2.getText().toString();
int numero2 = Integer.parseInt(n1);
int soma=numero1+numero2;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
Button bt = (Button) findViewById(R.id.bt1);
final Intent it = new Intent(this, Cadastros.class);
bt.setOnClickListener(new Button.OnClickListener() {
@Override
public void onClick(View v) {
it.putExtra("Total", soma);
startActivity(it);
}
});
}
XML:
<?xml version="1.0" encoding="utf-8"?>
<AbsoluteLayout
android:id="@+id/widget0"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
xmlns:android="http://schemas.android.com/apk/res/android">
<Button
android:id="@+id/bt1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Click"
android:gravity="center"
android:layout_x="134dp"
android:layout_y="102dp" />
<EditText
android:id="@+id/txtNumber1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_x="56dp"
android:layout_y="179dp"
android:ems="10"
android:inputType="number" >
<requestFocus />
</EditText>
<EditText
android:id="@+id/txtNumber2"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_x="69dp"
android:layout_y="222dp"
android:ems="10"
android:inputType="number" />
</AbsoluteLayout>
Log
06-16 08:09:48.240: D/AndroidRuntime(948): Shutting down VM
06-16 08:09:48.270: W/dalvikvm(948): threadid=1: thread exiting with uncaught exception (group=0x41465730)
06-16 08:09:48.330: E/AndroidRuntime(948): FATAL EXCEPTION: main
06-16 08:09:48.330: E/AndroidRuntime(948): java.lang.RuntimeException: Unable to instantiate activity ComponentInfo{br.com.passagemparam/br.com.passagemparam.MainActivity}: java.lang.NullPointerException
06-16 08:09:48.330: E/AndroidRuntime(948): at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2137)
06-16 08:09:48.330: E/AndroidRuntime(948): at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2261)
06-16 08:09:48.330: E/AndroidRuntime(948): at android.app.ActivityThread.access$600(ActivityThread.java:141)
06-16 08:09:48.330: E/AndroidRuntime(948): at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1256)
06-16 08:09:48.330: E/AndroidRuntime(948): at android.os.Handler.dispatchMessage(Handler.java:99)
06-16 08:09:48.330: E/AndroidRuntime(948): at android.os.Looper.loop(Looper.java:137)
06-16 08:09:48.330: E/AndroidRuntime(948): at android.app.ActivityThread.main(ActivityThread.java:5103)
06-16 08:09:48.330: E/AndroidRuntime(948): at java.lang.reflect.Method.invokeNative(Native Method)
06-16 08:09:48.330: E/AndroidRuntime(948): at java.lang.reflect.Method.invoke(Method.java:525)
06-16 08:09:48.330: E/AndroidRuntime(948): at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:737)
06-16 08:09:48.330: E/AndroidRuntime(948): at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:553)
06-16 08:09:48.330: E/AndroidRuntime(948): at dalvik.system.NativeStart.main(Native Method)
06-16 08:09:48.330: E/AndroidRuntime(948): Caused by: java.lang.NullPointerException
06-16 08:09:48.330: E/AndroidRuntime(948): at android.app.Activity.findViewById(Activity.java:1853)
06-16 08:09:48.330: E/AndroidRuntime(948): at br.com.passagemparam.MainActivity.<init>(MainActivity.java:14)
06-16 08:09:48.330: E/AndroidRuntime(948): at java.lang.Class.newInstanceImpl(Native Method)
06-16 08:09:48.330: E/AndroidRuntime(948): at java.lang.Class.newInstance(Class.java:1130)
06-16 08:09:48.330: E/AndroidRuntime(948): at android.app.Instrumentation.newActivity(Instrumentation.java:1061)
06-16 08:09:48.330: E/AndroidRuntime(948): at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2128)
06-16 08:09:48.330: E/AndroidRuntime(948): ... 11 more