-2

i did not suppose how to take the icon so that it would fill whole screen in the android.

it also gave the error java.lang.NullPointerException: Attempt to write to null array

my mainActivity file

package com.example.dk.bfit1;

import android.graphics.Color;
import android.os.Build;
import android.support.annotation.RequiresApi;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.util.Log;
import android.widget.Button;

import java.util.concurrent.ThreadLocalRandom;

public class MainActivity extends AppCompatActivity {
Button[] button;


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

    button[0]=(Button)findViewById(R.id.a1);
  button[1]=(Button)findViewById(R.id.a2);
  button[2]=(Button)findViewById(R.id.a3);
  button[3]=(Button)findViewById(R.id.a4);
   button[4]=(Button)findViewById(R.id.a5);
  button[5]=(Button)findViewById(R.id.a6);
  button[6]=(Button)findViewById(R.id.a7);
 button[7]=(Button)findViewById(R.id.a8);
 button[8]=(Button)findViewById(R.id.a9);
  button[9]=(Button)findViewById(R.id.a10);


 run();

  }

void run(){
    int i=0;
    while(i<10){
        i++;
        int n =(int)(Math.random() *9 + 1);
        try{
               for(int j=0;i<10;j++) {

                   button[n].setBackgroundResource(android.R.color.black);
                   Thread.sleep(800);
                   Log.d("Dk", "BUtton icon is"+n);
               }
        }catch(InterruptedException ex){
           Log.d("dk","error at "+ex);
        }
    }
 } 

my main xml file

 <?xml version="1.0" encoding="utf-8"?>
 <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:id="@+id/activity_main"
android:layout_width="match_parent"
android:layout_height="match_parent"
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.dk.bfit1.MainActivity">

<Button
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:id="@+id/a1"/>
 <Button
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
     android:id="@+id/a2"/>
 <Button
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
     android:id="@+id/a3"/>
 <Button
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
     android:id="@+id/a4" />

 <Button
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:id="@+id/a5"/>
 <Button
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
     android:id="@+id/a6" />`enter code here`
 <Button
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"`enter code here`
     android:id="@+id/a7" />
 <Button
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
     android:id="@+id/a8"/>

 <Button
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
     android:id="@+id/a9" />
 <Button
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
     android:id="@+id/a10" />


  </LinearLayout>
  • 6
    Possible duplicate of [What is a NullPointerException, and how do I fix it?](http://stackoverflow.com/questions/218384/what-is-a-nullpointerexception-and-how-do-i-fix-it) – Rick S Feb 02 '17 at 19:43

1 Answers1

0

You need to initialise your Button Array.

Try Button[] button = new Button[10]; where 10 is the length of your array.

For more info about Arrays check: https://docs.oracle.com/javase/tutorial/java/nutsandbolts/arrays.html

Isaac
  • 1,442
  • 17
  • 26
  • Sir , i had done it but there is error occurs Choreographer: Skipped 963 frames! The application may be doing too much work on its main thread. – dhananjay kumar Feb 03 '17 at 03:13
  • See: http://stackoverflow.com/questions/14678593/the-application-may-be-doing-too-much-work-on-its-main-thread – Isaac Feb 03 '17 at 03:14
  • i had seen it earlier bro , acually my aim is to create a disco lilght but only with a sigle light glow at a tiem – dhananjay kumar Feb 03 '17 at 03:20