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

问题描述 投票:1回答:1
我是android开发的新手,我在这里寻求帮助。

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

是因为final Context context声明吗?

我的logcat错误从这里开始

: java.lang.IllegalStateException: System services not available to Activities before onCreate()

主课堂活动:

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

我是android开发的新手,我在这里寻求帮助。我已经编写了一个钱包程序,当我按下借记按钮时,它停止工作并崩溃。是因为最终的上下文...
android xml button service
1个回答
2
投票
您只会在“活动”生命周期方法中获得活动的上下文。因此,您应该在onCreate方法中对其进行初始化。
© www.soinside.com 2019 - 2024. All rights reserved.