在数组列表中存储摘要 类
Storing abstract classes in an arraylist
因此,我正在使用 java 制作一个 android 应用程序并具有以下 classes.
我的问题是,当我在应用程序中创建帐户时,让我们说 new CheckingAccount(111, 111) 和 new SavingsAccount(222, 222),然后尝试在另一个 class 中使用 CreateAccountActivity.getAccountNumberID(0) 得到如下错误
Process: bankapp, PID: 4918
java.lang.RuntimeException: Unable to start activity
ComponentInfo{bankapp/bankapp.EditAccountActivity}:
java.lang.IndexOutOfBoundsException: Index: 0, Size: 0
at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2817)
at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2892)
at android.app.ActivityThread.-wrap11(Unknown Source:0)
at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1593)
at android.os.Handler.dispatchMessage(Handler.java:105)
at android.os.Looper.loop(Looper.java:164)
at android.app.ActivityThread.main(ActivityThread.java:6541)
at java.lang.reflect.Method.invoke(Native Method)
at com.android.internal.os.Zygote$MethodAndArgsCaller.run(Zygote.java:240)
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:767)
Caused by: java.lang.IndexOutOfBoundsException: Index: 0, Size: 0 at java.util.ArrayList.get(ArrayList.java:437)
at bankapp.CreateAccountActivity.getAccountNumberID(CreateAccountActivity.java:131)
at bankapp.EditAccountActivity.onCreate(EditAccountActivity.java:37)
at android.app.Activity.performCreate(Activity.java:6975)
at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1213)
at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2770)
at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2892)
at android.app.ActivityThread.-wrap11(Unknown Source:0)
at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1593)
at android.os.Handler.dispatchMessage(Handler.java:105)
at android.os.Looper.loop(Looper.java:164)
at android.app.ActivityThread.main(ActivityThread.java:6541)
at java.lang.reflect.Method.invoke(Native Method)
at com.android.internal.os.Zygote$MethodAndArgsCaller.run(Zygote.java:240)
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:767)
CreateAccountActivity.java
import android.content.Context;
import android.nfc.Tag;
import android.os.Bundle;
import android.support.v7.app.AppCompatActivity;
import android.util.Log;
import android.view.View;
import android.widget.AdapterView;
import android.widget.ArrayAdapter;
import android.widget.Button;
import android.widget.EditText;
import android.widget.Spinner;
import android.widget.Toast;
import java.util.ArrayList;
import java.util.List;
public class CreateAccountActivity extends AppCompatActivity implements
AdapterView.OnItemSelectedListener{
public static ArrayList<BankAccount> aAccount = new ArrayList<BankAccount>();
EditText accountNumberTV, initialBalanceTV;
String accountType;
Context context = null;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_create);
// Spinner element
Spinner spinner = (Spinner) findViewById(R.id.accountTypeSpinner);
// Spinner click listener
spinner.setOnItemSelectedListener(this);
// Spinner Drop down elements
List<String> categories = new ArrayList<String>();
categories.add("Checking");
categories.add("Savings");
categories.add("Credit");
// Creating adapter for spinner
ArrayAdapter<String> dataAdapter = new ArrayAdapter<String>(this, android.R.layout.simple_spinner_item, categories);
// Drop down layout style - list view with radio button
dataAdapter.setDropDownViewResource(android.R.layout.simple_spinner_dropdown_item);
// attaching data adapter to spinner
spinner.setAdapter(dataAdapter);
final Button SuSe = (Button) findViewById(R.id.createNewAccountButton);
SuSe.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
accountNumberTV = (EditText) findViewById(R.id.accountNumberView);
accountNumberTV.setTransformationMethod(null);
initialBalanceTV = (EditText) findViewById(R.id.initialBalanceView)
}
});
}
@Override
public void onItemSelected(AdapterView<?> parent, View view, int position, long id) {
// On selecting a spinner item
accountType = parent.getItemAtPosition(position).toString();
// Showing selected spinner item
//Toast.makeText(parent.getContext(), "Selected: " + item, Toast.LENGTH_LONG).show();
}
public void onNothingSelected(AdapterView<?> arg0) {
// TODO Auto-generated method stub
}
public void CreateAccountButton(View view) {
accountNumberTV = (EditText) findViewById(R.id.accountNumberView);
accountNumberTV.setTransformationMethod(null);
initialBalanceTV = (EditText) findViewById(R.id.initialBalanceView);
String acctIDString = accountNumberTV.getText().toString();
int acctID=Integer.parseInt(acctIDString);
String initialBalanceString = initialBalanceTV.getText().toString();
double initialBalance=Float.parseFloat(initialBalanceString);
//bd.setUserInfo(1,newName, newAddress, context);
switch(accountType) {
case "Checking":
aAccount.add(new CheckingAccount(acctID, initialBalance));
break;
case "Savings":
aAccount.add(new SavingsAccount(acctID, initialBalance));
break;
case "Credit":
// code block
break;
}
}
static public int getAccountNumberID (int x){
return aAccount.get(x).getAcctID();
}
static public int getArraySize(){
return aAccount.size();
}
public double getAccountBalance(int x){
return aAccount.get(x).getCurrentBalance();
}
}
BankAccount.java
public abstract class BankAccount {
private final int acctID;
double acctBalance = 0;
public BankAccount checking;
public BankAccount savings;
public BankAccount credit;
public BankAccount (int Id, double balance){
this.acctID = Id;
this.acctBalance = balance;
}
public void deposit (double amount){
acctBalance = acctBalance + amount;
//System.out.println(acctBalance);
}
public boolean withdraw (double amount){
if(acctBalance<amount){
//System.out.println("There are insufficient funds.");
return false;
} else
acctBalance = acctBalance - amount;
System.out.println(acctBalance);
return true;
}
double balance (BankAccount acctID) {
return acctBalance;
}
public double getCurrentBalance(){
return acctBalance;
}
public int getAcctID () {
return acctID;
}
public static boolean transfer (double amount, BankAccount fromAccount, BankAccount toAccount) {
if (fromAccount.acctBalance > amount){
fromAccount.acctBalance = fromAccount.acctBalance - amount;
toAccount.acctBalance = toAccount.acctBalance + amount;
//System.out.println(fromAccount.acctBalance);
//System.out.println(toAccount.acctBalance);
return true;
} else
//System.err.println("Insufficient funds");
return false;
}
}
CheckingAccount.java
public class CheckingAccount extends BankAccount {
public BankAccount checking;
public CheckingAccount(int Id, double balance) {
super(Id, balance);
}
public void deposit (double amount)
{
super.deposit(amount);
}
}
SavingsAccount.java
public class SavingsAccount extends BankAccount{
public BankAccount savings;
public SavingsAccount(int Id, double balance) {
super(Id, balance);
}
public void deposit (double amount)
{
super.deposit(amount);
}
}
EditAccount.java
import android.content.Context;
import android.os.Bundle;
import android.support.v7.app.AppCompatActivity;
import android.view.View;
import android.widget.AdapterView;
import android.widget.ArrayAdapter;
import android.widget.Spinner;
import java.util.ArrayList;
import java.util.List;
public class EditAccountActivity extends AppCompatActivity implements AdapterView.OnItemSelectedListener {
//ArrayList<BankAccount> aAccount2=CreateAccountActivity.getList();
Context context = null;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_editaccount);
int arraysize = CreateAccountActivity.getArraySize();
// Spinner element
Spinner spinner1 = (Spinner) findViewById(R.id.selectAccountSpinner);
// Spinner click listener
spinner1.setOnItemSelectedListener(this);
// (2) create a simple static list of strings
List<Integer> spinnerArray = new ArrayList<>();
//spinnerArray.add(1);
//spinnerArray.add(aAccount2.get(0).getAcctID());
//spinnerArray.add(aAccount2.get(1).getAcctID());
spinnerArray.add(CreateAccountActivity.getAccountNumberID(0));
spinnerArray.add(CreateAccountActivity.getAccountNumberID(1));
int i = 0;
/* do{
spinnerArray.add(CreateAccountActivity.getAccountNumberID(i));
i++;
}while (i<2);
*/
// (3) create an adapter from the list
ArrayAdapter<Integer> adapter = new ArrayAdapter<Integer>(
this,
android.R.layout.simple_spinner_item,
spinnerArray
);
//adapter.setDropDownViewResource(android.R.layout.simple_spinner_dropdown_item);
// (4) set the adapter on the spinner
spinner1.setAdapter(adapter);
}
@Override
public void onItemSelected(AdapterView<?> parent, View view, int position, long id) {
}
@Override
public void onNothingSelected(AdapterView<?> parent) {
}
}
将 static 与 activity 的实例变量结合使用是行不通的。您可以使用 ViewModel 解决您的任务。您可以阅读更多关于它们的信息 here.
public void onCreate(Bundle savedInstanceState) {
// Create a ViewModel the first time the system calls an activity's onCreate() method.
// Re-created activities receive the same MyViewModel instance created by the first activity.
MyViewModel model = ViewModelProviders.of(this).get(MyViewModel.class);
model.getAccounts()
}
和视图模型:
public class MyViewModel extends ViewModel {
public List<Account> getAccounts() {
// return sth.
}
}
因此,我正在使用 java 制作一个 android 应用程序并具有以下 classes.
我的问题是,当我在应用程序中创建帐户时,让我们说 new CheckingAccount(111, 111) 和 new SavingsAccount(222, 222),然后尝试在另一个 class 中使用 CreateAccountActivity.getAccountNumberID(0) 得到如下错误
Process: bankapp, PID: 4918
java.lang.RuntimeException: Unable to start activity
ComponentInfo{bankapp/bankapp.EditAccountActivity}:
java.lang.IndexOutOfBoundsException: Index: 0, Size: 0
at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2817)
at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2892)
at android.app.ActivityThread.-wrap11(Unknown Source:0)
at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1593)
at android.os.Handler.dispatchMessage(Handler.java:105)
at android.os.Looper.loop(Looper.java:164)
at android.app.ActivityThread.main(ActivityThread.java:6541)
at java.lang.reflect.Method.invoke(Native Method)
at com.android.internal.os.Zygote$MethodAndArgsCaller.run(Zygote.java:240)
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:767)
Caused by: java.lang.IndexOutOfBoundsException: Index: 0, Size: 0 at java.util.ArrayList.get(ArrayList.java:437)
at bankapp.CreateAccountActivity.getAccountNumberID(CreateAccountActivity.java:131)
at bankapp.EditAccountActivity.onCreate(EditAccountActivity.java:37)
at android.app.Activity.performCreate(Activity.java:6975)
at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1213)
at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2770)
at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2892)
at android.app.ActivityThread.-wrap11(Unknown Source:0)
at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1593)
at android.os.Handler.dispatchMessage(Handler.java:105)
at android.os.Looper.loop(Looper.java:164)
at android.app.ActivityThread.main(ActivityThread.java:6541)
at java.lang.reflect.Method.invoke(Native Method)
at com.android.internal.os.Zygote$MethodAndArgsCaller.run(Zygote.java:240)
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:767)
CreateAccountActivity.java
import android.content.Context;
import android.nfc.Tag;
import android.os.Bundle;
import android.support.v7.app.AppCompatActivity;
import android.util.Log;
import android.view.View;
import android.widget.AdapterView;
import android.widget.ArrayAdapter;
import android.widget.Button;
import android.widget.EditText;
import android.widget.Spinner;
import android.widget.Toast;
import java.util.ArrayList;
import java.util.List;
public class CreateAccountActivity extends AppCompatActivity implements
AdapterView.OnItemSelectedListener{
public static ArrayList<BankAccount> aAccount = new ArrayList<BankAccount>();
EditText accountNumberTV, initialBalanceTV;
String accountType;
Context context = null;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_create);
// Spinner element
Spinner spinner = (Spinner) findViewById(R.id.accountTypeSpinner);
// Spinner click listener
spinner.setOnItemSelectedListener(this);
// Spinner Drop down elements
List<String> categories = new ArrayList<String>();
categories.add("Checking");
categories.add("Savings");
categories.add("Credit");
// Creating adapter for spinner
ArrayAdapter<String> dataAdapter = new ArrayAdapter<String>(this, android.R.layout.simple_spinner_item, categories);
// Drop down layout style - list view with radio button
dataAdapter.setDropDownViewResource(android.R.layout.simple_spinner_dropdown_item);
// attaching data adapter to spinner
spinner.setAdapter(dataAdapter);
final Button SuSe = (Button) findViewById(R.id.createNewAccountButton);
SuSe.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
accountNumberTV = (EditText) findViewById(R.id.accountNumberView);
accountNumberTV.setTransformationMethod(null);
initialBalanceTV = (EditText) findViewById(R.id.initialBalanceView)
}
});
}
@Override
public void onItemSelected(AdapterView<?> parent, View view, int position, long id) {
// On selecting a spinner item
accountType = parent.getItemAtPosition(position).toString();
// Showing selected spinner item
//Toast.makeText(parent.getContext(), "Selected: " + item, Toast.LENGTH_LONG).show();
}
public void onNothingSelected(AdapterView<?> arg0) {
// TODO Auto-generated method stub
}
public void CreateAccountButton(View view) {
accountNumberTV = (EditText) findViewById(R.id.accountNumberView);
accountNumberTV.setTransformationMethod(null);
initialBalanceTV = (EditText) findViewById(R.id.initialBalanceView);
String acctIDString = accountNumberTV.getText().toString();
int acctID=Integer.parseInt(acctIDString);
String initialBalanceString = initialBalanceTV.getText().toString();
double initialBalance=Float.parseFloat(initialBalanceString);
//bd.setUserInfo(1,newName, newAddress, context);
switch(accountType) {
case "Checking":
aAccount.add(new CheckingAccount(acctID, initialBalance));
break;
case "Savings":
aAccount.add(new SavingsAccount(acctID, initialBalance));
break;
case "Credit":
// code block
break;
}
}
static public int getAccountNumberID (int x){
return aAccount.get(x).getAcctID();
}
static public int getArraySize(){
return aAccount.size();
}
public double getAccountBalance(int x){
return aAccount.get(x).getCurrentBalance();
}
}
BankAccount.java
public abstract class BankAccount {
private final int acctID;
double acctBalance = 0;
public BankAccount checking;
public BankAccount savings;
public BankAccount credit;
public BankAccount (int Id, double balance){
this.acctID = Id;
this.acctBalance = balance;
}
public void deposit (double amount){
acctBalance = acctBalance + amount;
//System.out.println(acctBalance);
}
public boolean withdraw (double amount){
if(acctBalance<amount){
//System.out.println("There are insufficient funds.");
return false;
} else
acctBalance = acctBalance - amount;
System.out.println(acctBalance);
return true;
}
double balance (BankAccount acctID) {
return acctBalance;
}
public double getCurrentBalance(){
return acctBalance;
}
public int getAcctID () {
return acctID;
}
public static boolean transfer (double amount, BankAccount fromAccount, BankAccount toAccount) {
if (fromAccount.acctBalance > amount){
fromAccount.acctBalance = fromAccount.acctBalance - amount;
toAccount.acctBalance = toAccount.acctBalance + amount;
//System.out.println(fromAccount.acctBalance);
//System.out.println(toAccount.acctBalance);
return true;
} else
//System.err.println("Insufficient funds");
return false;
}
}
CheckingAccount.java
public class CheckingAccount extends BankAccount {
public BankAccount checking;
public CheckingAccount(int Id, double balance) {
super(Id, balance);
}
public void deposit (double amount)
{
super.deposit(amount);
}
}
SavingsAccount.java
public class SavingsAccount extends BankAccount{
public BankAccount savings;
public SavingsAccount(int Id, double balance) {
super(Id, balance);
}
public void deposit (double amount)
{
super.deposit(amount);
}
}
EditAccount.java
import android.content.Context;
import android.os.Bundle;
import android.support.v7.app.AppCompatActivity;
import android.view.View;
import android.widget.AdapterView;
import android.widget.ArrayAdapter;
import android.widget.Spinner;
import java.util.ArrayList;
import java.util.List;
public class EditAccountActivity extends AppCompatActivity implements AdapterView.OnItemSelectedListener {
//ArrayList<BankAccount> aAccount2=CreateAccountActivity.getList();
Context context = null;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_editaccount);
int arraysize = CreateAccountActivity.getArraySize();
// Spinner element
Spinner spinner1 = (Spinner) findViewById(R.id.selectAccountSpinner);
// Spinner click listener
spinner1.setOnItemSelectedListener(this);
// (2) create a simple static list of strings
List<Integer> spinnerArray = new ArrayList<>();
//spinnerArray.add(1);
//spinnerArray.add(aAccount2.get(0).getAcctID());
//spinnerArray.add(aAccount2.get(1).getAcctID());
spinnerArray.add(CreateAccountActivity.getAccountNumberID(0));
spinnerArray.add(CreateAccountActivity.getAccountNumberID(1));
int i = 0;
/* do{
spinnerArray.add(CreateAccountActivity.getAccountNumberID(i));
i++;
}while (i<2);
*/
// (3) create an adapter from the list
ArrayAdapter<Integer> adapter = new ArrayAdapter<Integer>(
this,
android.R.layout.simple_spinner_item,
spinnerArray
);
//adapter.setDropDownViewResource(android.R.layout.simple_spinner_dropdown_item);
// (4) set the adapter on the spinner
spinner1.setAdapter(adapter);
}
@Override
public void onItemSelected(AdapterView<?> parent, View view, int position, long id) {
}
@Override
public void onNothingSelected(AdapterView<?> parent) {
}
}
将 static 与 activity 的实例变量结合使用是行不通的。您可以使用 ViewModel 解决您的任务。您可以阅读更多关于它们的信息 here.
public void onCreate(Bundle savedInstanceState) {
// Create a ViewModel the first time the system calls an activity's onCreate() method.
// Re-created activities receive the same MyViewModel instance created by the first activity.
MyViewModel model = ViewModelProviders.of(this).get(MyViewModel.class);
model.getAccounts()
}
和视图模型:
public class MyViewModel extends ViewModel {
public List<Account> getAccounts() {
// return sth.
}
}