Thursday, 20 August 2015

XML : System service is not available to activities before onCreate()

I'm new to android development and I'm here to seek for help. I've coded a wallet program and when i press the debit button , it stopped working and crashes. Is it because of the final context context declaration ?

My logcat error starts from here : java.lang.IllegalStateException: System services not available to Activities before onCreate()

Main class activity :

  private double total; //total amount  EditText et;  private double cinput; //credit input  private double dinput; //debit input  Button CreditButton;  Button DebitButton;  final Context context = this;    @Override  protected void onCreate(Bundle savedInstanceState) {      super.onCreate(savedInstanceState);      setContentView(R.layout.activity_main);      //loadAmt();      cinput = 0;      dinput = 0;      CreditButton = (Button) findViewById(R.id.CreditButton);      DebitButton = (Button) findViewById(R.id.DebitButton);      CreditButton.setOnClickListener(new ButtonActionListener());      DebitButton.setOnClickListener(new ButtonActionListener());      et = (EditText) findViewById(R.id.AmountText);      et.setText(String.valueOf(total));  }  public void loadAmt()  {      }    @Override  public boolean onCreateOptionsMenu(Menu menu) {      // Inflate the menu; this adds items to the action bar if it is present.      getMenuInflater().inflate(R.menu.menu_main, menu);      return true;  }    @Override  public boolean onOptionsItemSelected(MenuItem item) {      // Handle action bar item clicks here. The action bar will      // automatically handle clicks on the Home/Up button, so long      // as you specify a parent activity in AndroidManifest.xml.      int id = item.getItemId();        //noinspection SimplifiableIfStatement      if (id == R.id.action_settings) {          return true;      }        return super.onOptionsItemSelected(item);  }    public class ButtonActionListener extends MainActivity implements View.OnClickListener  {      @Override      public void onClick(View v){          if (v.getId() == R.id.DebitButton){              try{              // get prompts.xml view              LayoutInflater li = LayoutInflater.from(context);              View promptsView = li.inflate(R.layout.prompts, null);                AlertDialog.Builder alertDialogBuilder = new AlertDialog.Builder(                      context);                // set prompts.xml to alertdialog builder              alertDialogBuilder.setView(promptsView);                final EditText userInput = (EditText) promptsView                      .findViewById(R.id.editTextDialogUserInput);                // set dialog message              alertDialogBuilder                      .setCancelable(false)                      .setPositiveButton("OK",                              new DialogInterface.OnClickListener() {                                  public void onClick(DialogInterface dialog,int id) {                                      // get user input and set it to result                                      // edit text                                      String inputString = userInput.getText().toString();                                      dinput = Double.parseDouble(inputString);                                      total = total + dinput;                                      et.setText(String.valueOf(total));                                      }                              })                      .setNegativeButton("Cancel",                              new DialogInterface.OnClickListener() {                                  public void onClick(DialogInterface dialog,int id) {                                      dialog.cancel();                                  }                              });                // create alert dialog              AlertDialog alertDialog = alertDialogBuilder.create();                // show it              alertDialog.show();}              catch (InputMismatchException e){                  Toast.makeText(getApplicationContext(),"Only Numbers Are Allowed",Toast.LENGTH_SHORT).show();          }}            else if (v.getId() == R.id.CreditButton){              try {                  // get prompts.xml view                  LayoutInflater li = LayoutInflater.from(context);                  View promptsView = li.inflate(R.layout.prompts, null);                    AlertDialog.Builder alertDialogBuilder = new AlertDialog.Builder(                          context);                    // set prompts.xml to alertdialog builder                  alertDialogBuilder.setView(promptsView);                    final EditText userInput = (EditText) promptsView                          .findViewById(R.id.editTextDialogUserInput);                    // set dialog message                  alertDialogBuilder                          .setCancelable(false)                          .setPositiveButton("OK",                                  new DialogInterface.OnClickListener() {                                      public void onClick(DialogInterface dialog, int id) {                                          // get user input and set it to result                                          // edit text                                          String inputString = userInput.getText().toString();                                          cinput = Double.parseDouble(inputString);                                          total = total - cinput;                                          et.setText(String.valueOf(total));                                        }                                  })                          .setNegativeButton("Cancel",                                  new DialogInterface.OnClickListener() {                                      public void onClick(DialogInterface dialog, int id) {                                          dialog.cancel();                                      }                                  });                    // create alert dialog                  AlertDialog alertDialog = alertDialogBuilder.create();                    // show it                  alertDialog.show();              }              catch (InputMismatchException e) {                  Toast.makeText(getApplicationContext(), "Only Numbers Are Allowed", Toast.LENGTH_SHORT).show();              }          }            //save(total);          public void save(double tamt) {            SharedPreferences spamount = getSharedPreferences("TotalAmount" , MODE_PRIVATE);          SharedPreferences.Editor editor = spamount.edit();          editor.putString("LastAmount",String.valueOf(tamt));          editor.apply();        }      }  }    

No comments:

Post a Comment