I wrote a simple code for adding two numbers. But whenever I use the button OnClickListener() function the app is crashing.
import android.support.v7.app.ActionBarActivity;
import android.widget.Button;
import android.widget.TextView;
import android.support.v4.app.Fragment;
import android.os.Bundle;
import android.view.LayoutInflater;
import android.view.Menu;
import android.view.MenuItem;
import android.view.View;
import android.view.ViewGroup;
import android.widget.EditText;
public class MainActivity extends ActionBarActivity {
Button button1;
EditText txtbox1,txtbox2;
TextView tv;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
txtbox1 = (EditText) findViewById(R.id.editText1);
txtbox2 = (EditText) findViewById(R.id.editText2);
button1 = (Button) findViewById(R.id.button1);
tv = (TextView) findViewById(R.id.textView1);
if (savedInstanceState == null) {
getSupportFragmentManager().beginTransaction()
.add(R.id.container, new PlaceholderFragment()).commit();
}
button1.setOnClickListener(new View.OnClickListener() {
public void onClick(View v) {
String a,b;
Integer vis;
a = txtbox1.getText().toString();
b = txtbox2.getText().toString();
vis = Integer.parseInt(a)+Integer.parseInt(b);
tv.setText(vis.toString());
}
});
}
So to check what was wrong I changed the code in button1.setOnClickListener to a simple just changing the textview to some random text. Still the app is crashing. So I guess the problem is in my button function only.
Here is my xml file as well.
<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:background="#FFFFFF"
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.chawla.MainActivity$PlaceholderFragment" >
<EditText
android:id="@+id/editText1"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:hint="@string/edit_text1"
/>
<Button
android:id="@+id/button1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignLeft="@+id/editText1"
android:layout_below="@+id/editText1"
android:layout_marginTop="81dp"
android:text="@string/button_text"
android:clickable="true"
/>
<EditText
android:id="@+id/editText2"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:layout_alignLeft="@+id/editText1"
android:layout_below="@+id/editText1"
android:layout_marginTop="18dp"
android:ems="10"
android:hint="@string/edit_text2" />
<TextView
android:id="@+id/textView1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_below="@+id/button1"
android:layout_centerHorizontal="true"
android:layout_marginTop="98dp"
android:text="hi" />
Here is the stacktrace
06-04 18:24:35.150: E/AndroidRuntime(12289): FATAL EXCEPTION: main
06-04 18:24:35.150: E/AndroidRuntime(12289): java.lang.RuntimeException: Unable to start activity ComponentInfo{com.example.chawla/com.example.chawla.MainActivity}: java.lang.NullPointerException
06-04 18:24:35.150: E/AndroidRuntime(12289): at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2304)
06-04 18:24:35.150: E/AndroidRuntime(12289): at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2354)
06-04 18:24:35.150: E/AndroidRuntime(12289): at android.app.ActivityThread.access$600(ActivityThread.java:153)
06-04 18:24:35.150: E/AndroidRuntime(12289): at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1247)
06-04 18:24:35.150: E/AndroidRuntime(12289): at android.os.Handler.dispatchMessage(Handler.java:99)
06-04 18:24:35.150: E/AndroidRuntime(12289): at android.os.Looper.loop(Looper.java:137)
06-04 18:24:35.150: E/AndroidRuntime(12289): at android.app.ActivityThread.main(ActivityThread.java:5228)
06-04 18:24:35.150: E/AndroidRuntime(12289): at java.lang.reflect.Method.invokeNative(Native Method)
06-04 18:24:35.150: E/AndroidRuntime(12289): at java.lang.reflect.Method.invoke(Method.java:511)
06-04 18:24:35.150: E/AndroidRuntime(12289): at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:794)
06-04 18:24:35.150: E/AndroidRuntime(12289): at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:561)
06-04 18:24:35.150: E/AndroidRuntime(12289): at dalvik.system.NativeStart.main(Native Method)
06-04 18:24:35.150: E/AndroidRuntime(12289): Caused by: java.lang.NullPointerException
06-04 18:24:35.150: E/AndroidRuntime(12289): at com.example.chawla.MainActivity.onCreate(MainActivity.java:35)
06-04 18:24:35.150: E/AndroidRuntime(12289): at android.app.Activity.performCreate(Activity.java:5213)
06-04 18:24:35.150: E/AndroidRuntime(12289): at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1080)
06-04 18:24:35.150: E/AndroidRuntime(12289): at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2258)
06-04 18:24:35.150: E/AndroidRuntime(12289): ... 11 more
Thanks in Advance