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)};
}
}