Error while creating a calculator

0 Abhishek Chatterjee · April 7, 2015
Hey Guys ,

I was trying to create this calculator using grid layout .Although the design turned out pretty good , i am getting the following error message at runtime after gradle build completes , there are no syntax errors :- 

04-06 00:15:12.407    1815-1815/com.example.abhishek.davidcopperfield E/AndroidRuntime? FATAL EXCEPTION: main
    Process: com.example.abhishek.davidcopperfield, PID: 1815
    java.lang.RuntimeException: Unable to start activity ComponentInfo{com.example.abhishek.davidcopperfield/com.example.abhishek.davidcopperfield.MainActivity}: java.lang.NullPointerException: Attempt to invoke virtual method 'void android.widget.Button.setOnClickListener(android.view.View$OnClickListener)' on a null object reference
            at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2325)
            at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2390)
            at android.app.ActivityThread.access$800(ActivityThread.java:151)
            at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1303)
            at android.os.Handler.dispatchMessage(Handler.java:102)
            at android.os.Looper.loop(Looper.java:135)
            at android.app.ActivityThread.main(ActivityThread.java:5257)
            at java.lang.reflect.Method.invoke(Native Method)
            at java.lang.reflect.Method.invoke(Method.java:372)
            at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:903)
            at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:698)
     Caused by: java.lang.NullPointerException: Attempt to invoke virtual method 'void android.widget.Button.setOnClickListener(android.view.View$OnClickListener)' on a null object reference
            at com.example.abhishek.davidcopperfield.MainActivity.onCreate(MainActivity.java:22)
            at android.app.Activity.performCreate(Activity.java:5990)
            at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1106)
            at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2278)
            at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2390)
            at android.app.ActivityThread.access$800(ActivityThread.java:151)
            at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1303)
            at android.os.Handler.dispatchMessage(Handler.java:102)
            at android.os.Looper.loop(Looper.java:135)
            at android.app.ActivityThread.main(ActivityThread.java:5257)
            at java.lang.reflect.Method.invoke(Native Method)
            at java.lang.reflect.Method.invoke(Method.java:372)
            at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:903)
            at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:698)


activity_main.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: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="com.example.abhishek.davidcopperfield.MainActivity">

    <TextView
        android:layout_width="340dp"
        android:layout_height="170dp"
        android:textAppearance="?android:attr/textAppearanceLarge"
        android:text="@string/Message_Text"
        android:id="@+id/CalText"
        android:layout_above="@+id/gridLayout4"
        android:layout_alignParentLeft="true"
        android:layout_alignParentStart="true"
        android:layout_alignParentTop="true"
        android:layout_alignParentRight="true"
        android:layout_alignParentEnd="true"
        android:autoText="true" />

    <GridLayout
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_alignParentBottom="true"
        android:layout_alignParentLeft="true"
        android:layout_alignParentStart="true"
        android:id="@+id/gridLayout">

        <Button
            android:layout_width="170dp"
            android:layout_height="85dp"
            android:text="@string/Button_Text1"
            android:id="@+id/button11"
            android:layout_row="2"
            android:layout_column="0"
            android:layout_gravity="fill_horizontal" />

        <Button
            android:layout_width="85dp"
            android:layout_height="85dp"
            android:text="@string/Button_Text2"
            android:id="@+id/button12"
            android:layout_row="2"
            android:layout_column="1" />

        <Button
            android:layout_width="85dp"
            android:layout_height="85dp"
            android:text="@string/Button_Text3"
            android:id="@+id/button13"
            android:layout_row="2"
            android:layout_column="2" />

    </GridLayout>

    <GridLayout
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_above="@+id/gridLayout"
        android:layout_alignParentLeft="true"
        android:layout_alignParentStart="true"
        android:id="@+id/gridLayout1">

        <Button
            android:layout_width="85dp"
            android:layout_height="85dp"
            android:text="@string/Button_Text4"
            android:id="@+id/button21"
            android:layout_row="0"
            android:layout_column="0" />

        <Button
            android:layout_width="85dp"
            android:layout_height="85dp"
            android:text="@string/Button_Text5"
            android:id="@+id/button22"
            android:layout_row="0"
            android:layout_column="1" />

        <Button
            android:layout_width="85dp"
            android:layout_height="85dp"
            android:text="@string/Button_Text6"
            android:id="@+id/button23"
            android:layout_row="0"
            android:layout_column="2" />

        <Button
            android:layout_width="85dp"
            android:layout_height="85dp"
            android:text="@string/Button_Text7"
            android:id="@+id/button24"
            android:layout_row="0"
            android:layout_column="3" />
    </GridLayout>

    <GridLayout
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_above="@+id/gridLayout1"
        android:layout_alignParentLeft="true"
        android:layout_alignParentStart="true"
        android:id="@+id/gridLayout2">

        <Button
            android:layout_width="85dp"
            android:layout_height="85dp"
            android:text="@string/Button_Text8"
            android:id="@+id/button31"
            android:layout_row="0"
            android:layout_column="0" />

        <Button
            android:layout_width="85dp"
            android:layout_height="85dp"
            android:text="@string/Button_Text9"
            android:id="@+id/button32"
            android:layout_row="0"
            android:layout_column="1" />

        <Button
            android:layout_width="85dp"
            android:layout_height="85dp"
            android:text="@string/Button_Text10"
            android:id="@+id/button33"
            android:layout_row="0"
            android:layout_column="2" />

        <Button
            android:layout_width="85dp"
            android:layout_height="85dp"
            android:text="@string/Button_Text11"
            android:id="@+id/button34"
            android:layout_row="0"
            android:layout_column="3" />
    </GridLayout>


    <GridLayout
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_above="@+id/gridLayout2"
        android:layout_alignParentLeft="true"
        android:layout_alignParentStart="true"
        android:id="@+id/gridLayout3">

        <Button
            android:layout_width="85dp"
            android:layout_height="85dp"
            android:text="@string/Button_Text12"
            android:id="@+id/button41"
            android:layout_row="0"
            android:layout_column="0" />

        <Button
            android:layout_width="85dp"
            android:layout_height="85dp"
            android:text="@string/Button_Text13"
            android:id="@+id/button42"
            android:layout_row="0"
            android:layout_column="1" />

        <Button
            android:layout_width="85dp"
            android:layout_height="85dp"
            android:text="@string/Button_Text14"
            android:id="@+id/button43"
            android:layout_row="0"
            android:layout_column="2" />

        <Button
            android:layout_width="85dp"
            android:layout_height="85dp"
            android:text="@string/Button_Text15"
            android:id="@+id/button44"
            android:layout_row="0"
            android:layout_column="3" />
    </GridLayout>

    <GridLayout
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_above="@+id/gridLayout3"
        android:layout_alignParentLeft="true"
        android:layout_alignParentStart="true"
        android:id="@+id/gridLayout4">

        <Button
            android:layout_width="85dp"
            android:layout_height="85dp"
            android:text="@string/Button_Text16"
            android:id="@+id/button51"
            android:layout_row="0"
            android:layout_column="0" />

        <Button
            android:layout_width="85dp"
            android:layout_height="85dp"
            android:text="@string/Button_Text17"
            android:id="@+id/button52"
            android:layout_row="0"
            android:layout_column="1" />

        <Button
            android:layout_width="85dp"
            android:layout_height="85dp"
            android:text="@string/Button_Text18"
            android:id="@+id/button53"
            android:layout_row="0"
            android:layout_column="2" />

        <Button
            android:layout_width="85dp"
            android:layout_height="85dp"
            android:text="@string/Button_Text19"
            android:id="@+id/button54"
            android:layout_row="0"
            android:layout_column="3" />
    </GridLayout>


</RelativeLayout>

MainActivity.java 
package com.example.abhishek.davidcopperfield;

import android.support.v7.app.ActionBarActivity;
import android.os.Bundle;
import android.util.Log;
import android.view.Menu;
import android.view.MenuItem;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;
import android.widget.GridView;
import android.widget.TextView;


public class MainActivity extends ActionBarActivity {

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);

        Button button11 = (Button)findViewById((R.id.button11));

        button11.setOnClickListener(
                new Button.OnClickListener(){
                    public void onClick(View v){
                        TextView CalText = (TextView)findViewById(R.id.CalText);
                        CalText.setText("0");
                    }
                }
        );
}

    @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_main, 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);
    }

}
 Strings.xml
<resources>
    <string name="app_name">David Copperfield</string>
    <string name="title_activity_main">MainActivity</string>

    <string name="hello_world">Hello world!</string>
    <string name="action_settings">Settings</string>
    <string name="Message_Text">0</string>
    <string name="Button_Text1">0</string>
    <string name="Button_Text2">.</string>
    <string name="Button_Text3">=</string>
    <string name="Button_Text4">1</string>
    <string name="Button_Text5">2</string>
    <string name="Button_Text6">3</string>
    <string name="Button_Text7">+</string>
    <string name="Button_Text8">4</string>
    <string name="Button_Text9">5</string>
    <string name="Button_Text10">6</string>
    <string name="Button_Text11">-</string>
    <string name="Button_Text12">7</string>
    <string name="Button_Text13">8</string>
    <string name="Button_Text14">9</string>
    <string name="Button_Text15">X</string>
    <string name="Button_Text16">AC</string>
    <string name="Button_Text17">+/-</string>
    <string name="Button_Text18">%</string>
    <string name="Button_Text19">/</string>

</resources>

Post a Reply

Replies

Oldest  Newest  Rating
0 Tim Herreijgers · April 7, 2015
I looked at your error message and it said this:

Caused by: java.lang.NullPointerException: Attempt to invoke virtual method 'void android.widget.Button.setOnClickListener(android.view.View$OnClickListener)' on a null object reference
at com.example.abhishek.davidcopperfield.MainActivity.onCreate(MainActivity.java:22)

It says that something goes wrong at line 22 (showed in red). After that i reviewed your code and i found this at line 22:

super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);


You are doing the same thing twice, I think if you remove it once you should be good to go. I hope this helped you.
  • 1

Java / Android Development

107,205 followers
About

Very popular language used to create desktop applications, website applets, and Android apps.

Links
Moderators
Bucky Roberts Administrator