货币Util Java错误代码'int java.lang.Object.hashCode()'

时间:2018-10-29 15:37:16

标签: java android hash hashcode

我正在尝试从Java Util Currency显示我的货币对话框,并且我在这里的简单试用项目中始终遇到此错误,将为我的对话框如何解决该问题以启动任何帮助,将不胜感激。 这是GitHub项目,我将非常感谢您的帮助。

货币帮助程序类

class ViewController: UIViewController {

    @IBOutlet weak var textField: UITextField!

    var toolbar = KeyboardToolbar?
    override func viewDidLoad() {
        super.viewDidLoad()


        addButtons(for: textField, setLeftButtons: [.back, .forward], andRightButtons: [.done])
    }


    private func addButtons(for textField: UITextField, setLeftButtons leftButtons: [KeyboardToolbarButton] = [], andRightButtons rightButtons: [KeyboardToolbarButton] = []) {
        let toolbar = KeyboardToolbar(textField: textField)
        toolbar.toolBarDelegate = self
        toolbar.setup(leftButtons: leftButtons, rightButtons: rightButtons)
        self.toolbar = toolbar
    }

}

代码:

class CurrencyHelper {

    private static final String[] MAIN_CURRENCIES = {"USD",
            "EUR", "GBP", "IRN", "AUD", "CAD", "SGD", "CHF", "MYR", "JPY", "CNY", "NZD","KSH"};
    private static final String CURRENCY_ISO    = "currency_iso";

    private static final DecimalFormat decimalFormatter = new DecimalFormat("#.00");


    @NonNull
    public static List<Currency> getMainAvailableCurrencies()
    {
        List<Currency> mainCurrencies = new ArrayList<>(MAIN_CURRENCIES.length);

        for(String currencyCode : MAIN_CURRENCIES)
        {
            try
            {
                Currency currency = Currency.getInstance(currencyCode);
                if( currency != null )
                {
                    mainCurrencies.add(currency);
                }
            }
            catch (Exception e)
            {
                Logger.getLogger("Unable to find currency with code: "+currencyCode);
            }
        }

        return mainCurrencies;
    }


    public static List<Currency> getOtherAvailableCurrencies()
    {
        List<Currency> mainCurrencies = getMainAvailableCurrencies();

        if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.KITKAT)
        {
            List<Currency> currencies = new ArrayList<>(Currency.getAvailableCurrencies());

            // Exclude main currencies
            Iterator<Currency> currencyIterator = currencies.iterator();
            while (currencyIterator.hasNext())
            {
                Currency currency = currencyIterator.next();

                if( mainCurrencies.contains(currency) )
                {
                    currencyIterator.remove();
                }
            }

            return currencies;
        }
        else
        {
            Set<Currency> currencySet = new HashSet<>();

            Locale[] locales = Locale.getAvailableLocales();
            for(Locale locale : locales)
            {
                try
                {
                    Currency currency = Currency.getInstance(locale);

                    if( mainCurrencies.contains(currency) )
                    {
                        continue; // Exclude main currencies
                    }

                    currencySet.add(currency);
                }
                catch(Exception ignored)
                {
                    // Locale not found
                }
            }

            List<Currency> currencies = new ArrayList<>(currencySet);
            Collections.sort(currencies, new Comparator<Currency>()
            {
                @Override
                public int compare(Currency lhs, Currency rhs)
                {
                    return lhs.getCurrencyCode().compareTo(rhs.getCurrencyCode());
                }
            });

            return currencies;
        }
    }

我的异步任务

    public class Constants {

        private final static String SHARED_PREFERENCES_FILE_NAME = "currency_trial";

        private final SharedPreferences preferences;


        private Constants(@NonNull Context context) {
            preferences = context.getApplicationContext().getSharedPreferences(SHARED_PREFERENCES_FILE_NAME, Context.MODE_PRIVATE);
        }


        public void putInt(@NonNull String key, int value)
   {
            preferences.edit().putInt(key, value).apply();
        }

        public void putLong(@NonNull String key, long value)
        {
            preferences.edit().putLong(key, value).apply();
        }


        public void putString(@NonNull String key, @NonNull String value)
        {
            preferences.edit().putString(key, value).apply();
        }


        public void putBoolean(String key, boolean value)
        {
            preferences.edit().putBoolean(key, value).apply();
        }


        public int getInt(@NonNull String key, int defaultValue)
        {
            return preferences.getInt(key, defaultValue);
        }


        public long getLong(@NonNull String key, long defaultValue)
        {
            return preferences.getLong(key, defaultValue);
        }


        public boolean getBoolean(@NonNull String key, boolean defaultValue) {
            return preferences.getBoolean(key, defaultValue);
        }


        @Nullable
        public String getString(String key) {
            return preferences.getString(key, null);
        }


        private static Constants ourInstance;


        public static synchronized Constants getInstance(Context context) {
            if (ourInstance == null) {
                ourInstance = new Constants(context);
            }

            return ourInstance;
        }
    }


   [1]: https://github.com/annMusenya/Trial-Project

完整错误代码:

  @SuppressLint("StaticFieldLeak")
    private void setupRecyclerView(final View v) {
        final RecyclerView recyclerView = v.findViewById(R.id.select_currency_recycler_view);
        recyclerView.setLayoutManager(new LinearLayoutManager(v.getContext()));


        new AsyncTask<Void, Void, Pair<SelectCurrencyAdapter, Integer>>()
        {
            @RequiresApi(api = Build.VERSION_CODES.KITKAT)
            @Override
            protected Pair<SelectCurrencyAdapter, Integer> doInBackground(Void... voids) {
                SelectCurrencyAdapter adapter = new SelectCurrencyAdapter(CurrencyHelper.getMainAvailableCurrencies(),
                        CurrencyHelper.getOtherAvailableCurrencies());
                return Pair.create(adapter, adapter.getSelectedCurrencyPosition(Objects.requireNonNull(getContext())));
            }

            @Override
            protected void onPostExecute(Pair<SelectCurrencyAdapter, Integer> data)
            {
                recyclerView.setAdapter(data.first);

                if( data.second > 1 )
                {
                    recyclerView.scrollToPosition(data.second-1);
                }
            }
        }.execute();
    }

1 个答案:

答案 0 :(得分:1)

该错误消息有时可能会有些混乱。实际上,错误不在方法hashCode()中。实际上,它尝试在null对象上调用此方法。

在您的情况下,这是通过以下方法发生的:

static Currency getUserCurrency( Context context) {

    return Currency.getInstance(Constants.getInstance(context).getString(CURRENCY_ISO));
}

Constants.getInstance(context).getString(CURRENCY_ISO)为空。