Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • Home
  • SEARCH
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 8276901
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 8, 20262026-06-08T08:31:25+00:00 2026-06-08T08:31:25+00:00

I am making a simple calculator and am adding buttons dynamically through java at

  • 0

I am making a simple calculator and am adding buttons dynamically through java at run time

my Java file is

package com.example.calculator;

import android.os.Bundle;
import android.app.Activity;
import android.view.*;
import android.widget.*;
import android.support.v4.app.NavUtils;

public class MainActivity extends Activity {

    Button [] button = new Button[17];
    TextView input_field = new TextView(null);

    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        this.setlayout();        
    }

    @Override
    public boolean onCreateOptionsMenu(Menu menu) {
        getMenuInflater().inflate(R.menu.activity_main, menu);
        return true;
    }

    private void setlayout(){

        Display display = getWindowManager().getDefaultDisplay(); 
        int width = display.getWidth();
        int height = display.getHeight();
        int x = 4;
        height -= x ;
        input_field.setHeight(x);
        input_field.setWidth((width-x));
        height /= 4;
        width /= 4;
        for(int i=0;i<16;i++)
            {   
                button [i] = new Button(null);
                button[i].setHeight(height);
                button[i].setWidth(width);
            }
        button [16] = new Button(null); 
        button[16].setHeight(x);
        button[16].setWidth(x);

        LinearLayout top = new LinearLayout(null);
        top.addView(input_field);
        top.addView(button[16]);

        LinearLayout row1 = new LinearLayout(null);
        row1.addView(button[8]);
        row1.addView(button[9]);
        row1.addView(button[10]);
        row1.addView(button[12]);

        LinearLayout row2 = new LinearLayout(null);
        row2.addView(button[7]);
        row2.addView(button[6]);
        row2.addView(button[5]);
        row2.addView(button[13]);

        LinearLayout row3 = new LinearLayout(null);
        row3.addView(button[4]);
        row3.addView(button[3]);
        row3.addView(button[2]);
        row3.addView(button[14]);

        LinearLayout row4 = new LinearLayout(null);
        row4.addView(button[0]);
        row4.addView(button[1]);
        row4.addView(button[11]);
        row4.addView(button[15]);

        LinearLayout main = (LinearLayout) findViewById(R.id.main_layout);

        main.setVisibility(1);
        main.setOrientation(1);
        main.addView(top);
        main.addView(row1);
        main.addView(row2);
        main.addView(row3);
        main.addView(row4);

        main.setVisibility(0);
    }
}

here is my XML file

<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:tools="http://schemas.android.com/tools"
    android:id="@+id/main_layout"
    android:layout_width="match_parent"
    android:layout_height="match_parent" >


</LinearLayout>

here is my logCat error

07-22 12:58:42.725: E/AndroidRuntime(29430): FATAL EXCEPTION: main
07-22 12:58:42.725: E/AndroidRuntime(29430): java.lang.RuntimeException: Unable to instantiate activity ComponentInfo{com.example.calculator/com.example.calculator.MainActivity}: java.lang.NullPointerException
07-22 12:58:42.725: E/AndroidRuntime(29430):    at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2675)
07-22 12:58:42.725: E/AndroidRuntime(29430):    at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2769)
07-22 12:58:42.725: E/AndroidRuntime(29430):    at android.app.ActivityThread.access$2500(ActivityThread.java:129)
07-22 12:58:42.725: E/AndroidRuntime(29430):    at android.app.ActivityThread$H.handleMessage(ActivityThread.java:2117)
07-22 12:58:42.725: E/AndroidRuntime(29430):    at android.os.Handler.dispatchMessage(Handler.java:99)
07-22 12:58:42.725: E/AndroidRuntime(29430):    at android.os.Looper.loop(Looper.java:143)
07-22 12:58:42.725: E/AndroidRuntime(29430):    at android.app.ActivityThread.main(ActivityThread.java:4717)
07-22 12:58:42.725: E/AndroidRuntime(29430):    at java.lang.reflect.Method.invokeNative(Native Method)
07-22 12:58:42.725: E/AndroidRuntime(29430):    at java.lang.reflect.Method.invoke(Method.java:521)
07-22 12:58:42.725: E/AndroidRuntime(29430):    at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:860)
07-22 12:58:42.725: E/AndroidRuntime(29430):    at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:618)
07-22 12:58:42.725: E/AndroidRuntime(29430):    at dalvik.system.NativeStart.main(Native Method)
07-22 12:58:42.725: E/AndroidRuntime(29430): Caused by: java.lang.NullPointerException
07-22 12:58:42.725: E/AndroidRuntime(29430):    at android.view.ViewConfiguration.get(ViewConfiguration.java:216)
07-22 12:58:42.725: E/AndroidRuntime(29430):    at android.view.View.<init>(View.java:1826)
07-22 12:58:42.725: E/AndroidRuntime(29430):    at android.view.View.<init>(View.java:1873)
07-22 12:58:42.725: E/AndroidRuntime(29430):    at android.widget.TextView.<init>(TextView.java:432)
07-22 12:58:42.725: E/AndroidRuntime(29430):    at android.widget.TextView.<init>(TextView.java:426)
07-22 12:58:42.725: E/AndroidRuntime(29430):    at android.widget.TextView.<init>(TextView.java:421)
07-22 12:58:42.725: E/AndroidRuntime(29430):    at com.example.calculator.MainActivity.<init>(MainActivity.java:12)
07-22 12:58:42.725: E/AndroidRuntime(29430):    at java.lang.Class.newInstanceImpl(Native Method)
07-22 12:58:42.725: E/AndroidRuntime(29430):    at java.lang.Class.newInstance(Class.java:1429)
07-22 12:58:42.725: E/AndroidRuntime(29430):    at android.app.Instrumentation.newActivity(Instrumentation.java:1021)
07-22 12:58:42.725: E/AndroidRuntime(29430):    at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2667)
07-22 12:58:42.725: E/AndroidRuntime(29430):    ... 11 more
  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-08T08:31:27+00:00Added an answer on June 8, 2026 at 8:31 am

    Why are you passing null to LinearLayout? It requires that you pass it the context of your activity (in your case it would be this). Same is true for Buttons and any other View you choose to create dynamically in code.

    Also, look at this answer cause I suspect you’ll run into another problem as soon as you replace all your nulls with this, and that is the problem that your views will not know how wide or tall to be…
    Set margins in a LinearLayout programmatically

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I am currently making a simple calculator parser in Java, to deal only with
I'm making a simple calculator for this homework, and Java is returning Infinity when
I'm making a webpage ( http://example.com/calculation/ ). This site will do simple calculations. The
I am making a little calculator in C, and i want to pass simple
I'm making a simple RTS game. I want it to run very fast because
I have a simple calculator script for making calculations of facebook insights. There is
I'm making a simple tower defense game in Swing and I've run into a
I am making simple grid like game ( grid is matrix 128x128) and matrix
I am making simple connection between two table - first one called users has
Making a simple application, so when the user logs out of Windows, it of

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.