onCreate()之前的活动无法使用系统服务

时间:2015-08-21 05:25:47

标签: android xml button service

我是Android开发的新手,我在这里寻求帮助。

我编写了一个钱包程序,当我按下借记按钮时,它停止工作并崩溃。

是否因为: java.lang.IllegalStateException: System services not available to Activities before onCreate() 声明?

我的logcat错误从这里开始

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

    }
}
}

主要课程活动:

{{1}}

1 个答案:

答案 0 :(得分:1)

您只能在Activity生命周期方法中获取活动的上下文。所以你应该在onCreate方法中初始化它。

private Context context;

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