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
Rajesh
  • 15,724
  • 7
  • 46
  • 95
Akshit
  • 19
  • 4

4 Answers4

2

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

Community
  • 1
  • 1
Yevgeny Simkin
  • 27,946
  • 39
  • 137
  • 236
1

Try changing

TextView input_field = new TextView(null);

to

private TextView input_field = new TextView(MainActivity.this);
Jeshurun
  • 22,940
  • 6
  • 79
  • 92
0

setContextView should be called in the end. Try doing setLayout first and call SetContextView on the "main" object of LinearLayout type.

Also, can you post what line is it that throws the error?

SurenNihalani
  • 1,398
  • 2
  • 15
  • 28
0

here you have array of buttons from button[0] till button[16]

then change the the for loop from i<16 to i<=16

for(int i=0;i<=16;i++)

Hossam Alaa
  • 663
  • 5
  • 9