-1

I have created my first calculator app but to reduce the size and to simplify the code i want to create an EditText as global variable and latter a Button array also as global variable. For now when I define and initialize the EditText array outside of the method the app crashes on the start.

But when I define the array outside the method and initialize it inside of the method I get error of array initialization not allowed here. what to do? sample code will be appreciated please.

Case-1 (app crashes)

public class MainActivity extends AppCompatActivity
        implements NavigationView.OnNavigationItemSelectedListener {
    EditText[] editList = {(EditText) findViewById(R.id.decValue), (EditText) findViewById(R.id.octValue), (EditText) findViewById(R.id.hexValue), (EditText) findViewById(R.id.hexValue),(EditText) findViewById(R.id.binValue)};
    ..
    @Override
    protected void onCreate(Bundle savedInstanceState) {
    ..
    }
}

Case-2 (error of cannot initialize array here)

public class MainActivity extends AppCompatActivity
        implements NavigationView.OnNavigationItemSelectedListener {
    EditText[] editList;
    ..
    @Override
    protected void onCreate(Bundle savedInstanceState) {
    ..
    editList = {(EditText) findViewById(R.id.decValue), (EditText) findViewById(R.id.octValue), (EditText) findViewById(R.id.hexValue), (EditText) findViewById(R.id.hexValue),(EditText) findViewById(R.id.binValue)};
    }
}
  • It makes no sense to make any view "global". Any view only has relevance inside the parent that holds it (eg. `Activity` or `Fragment`). Using `findViewById()` will only find one resource they are not interchangeable. – Barns Jun 15 '18 at 22:56
  • @Barns How do I define and initialize a 'EditText' array variable that is accessible by all the methods inside the 'Activity' lets say 'MainActivity.java' using an array. It's is required as I have created methods for performing different task e.g. disabling/enabling 'Buttons' and 'EditText', performing calculations, changing colours etc. – user5653890 Jun 15 '18 at 23:04
  • You mean class variable instead of global variable! Class variables are accessible throughout the class. But, global variables are accessible throughout the entire app... which would lead to quite a few errors if they were to reference a `TextView` or similar view. – Barns Jun 15 '18 at 23:07
  • @Barns yes you are correct, I mean the class variable. – user5653890 Jun 15 '18 at 23:11

3 Answers3

0

Replace

editList = {(EditText) findViewById(R.id.decValue), ...};

with :

editList = new EditText[]{(EditText) findViewById(R.id.decValue), ...};

In your first example the initialization of the array was correct, but there are no EditText till onCreate method is called. In the second case, you were almost correct, but you were doing the array initialization bad.

ollaw
  • 2,086
  • 1
  • 20
  • 33
0

Case 1

You're using an instance initializer so your list is initialized before MainActivity's constructor is called, and before onCreate() -> setContentView(), which means there is no UI at that point.

Case 2

You can only use shorthand array initialization syntax when declaring the member variable.

Replace it with new EditText[]{...}.

Advice

I would use ButterKnife library. It makes view binding easier and you can also group multiple views into a list which is what you're trying to achieve.

Add dependency

dependencies {
  implementation 'com.jakewharton:butterknife:8.8.1'
  annotationProcessor 'com.jakewharton:butterknife-compiler:8.8.1'
}

In your activity:

@BindViews({R.id.first_name, R.id.middle_name, R.id.last_name})
List<EditText> nameViews;

@Override
public void onCreate(Bundle savedInstanceState) {
    //...
    ButterKnife.bind(this);
}

That's it. Looks much more readable.

Apply an action to all views in a list:

ButterKnife.apply(list, (view, index) -> {
    //your action for view
});

Take a look at the documentation for details.

0

Define your class variables inside the class (but not inside any method!)

Then you assign the variables in your onCreate() method (for example).

public class MainActivity extends AppCompatActivity
        implements NavigationView.OnNavigationItemSelectedListener {

    EditText evDecValue;
    EditText evOctValue;
    EditText evHexValue;
    ...
    ... 
    ...
    EditText evBinValue;


    @Override
    protected void onCreate(Bundle savedInstanceState) {
        evDecValue = (EditText) findViewById(R.id.decValue);
        evOctValue = (EditText) findViewById(R.id.octValue);
        evHexValue = (EditText) findViewById(R.id.hexValue);
        ...
        ...
        ...
        evHexValue = (EditText) findViewById(R.id.binValue);
    }
}

Please note that "..." is for you to fill in with the other EditViews. I was to lazy to type them all.

I believe you will be more satisfied assigning each EditView to its own variable. It is cleaner syntax and you will know exactly which EditView you are accessing. Otherwise you would need to know which EditView was assigned to editList[1].

Barns
  • 4,850
  • 3
  • 17
  • 31