有 Java 编程相关的问题?

你可以在下面搜索框中键入要查询的问题!

java如何在从活动中删除回收器适配器的项目后刷新它

“我的回收器”视图中的每个钱包都有一个设置活动,我可以使用下面的代码从其设置活动中删除回收器项目

我试图在执行delete函数后更新recycler视图,但我一直从walletfragment中获取空指针异常

设置。kt

 class WalletSettings : AppCompatActivity() {
    override fun onCreate(savedInstanceState: Bundle?) {
        super.onCreate(savedInstanceState)
        setContentView(R.layout.settings_activity)
        supportFragmentManager
                .beginTransaction()
                .replace(R.id.settings, SettingsFragment())
                .commit()
        supportActionBar?.setDisplayHomeAsUpEnabled(true)

        val txtName = findViewById<View>(R.id.title) as TextView
        txtName.text = "Settings"

        val subTitle = findViewById<View>(R.id.sub_title) as TextView
        subTitle.visibility = View.VISIBLE
        subTitle.text = intent.extras!!.getString("walletname")

        val leftImageView = findViewById<View>(R.id.go_back) as ImageView
        leftImageView.setImageResource(R.drawable.ic_action_navigation_arrow_back)
        leftImageView.setColorFilter(ContextCompat.getColor(applicationContext, R.color.colorPrimary))
        leftImageView.setOnClickListener { finish() }

        val rightImageView = findViewById<View>(R.id.notification) as ImageView
        rightImageView.visibility = View.INVISIBLE
    }

    class SettingsFragment : PreferenceFragmentCompat() {
        var walletsFragment: WalletsFragment? = null

        override fun onCreate(savedInstanceState: Bundle?) {
            super.onCreate(savedInstanceState)
            setHasOptionsMenu(true)
            walletsFragment = WalletsFragment()
            Log.i(TAG, "----------oncreate fragment $walletsFragment")
        }

        override fun onCreatePreferences(savedInstanceState: Bundle?, rootKey: String?) {
            setPreferencesFromResource(R.xml.root_preferences, rootKey)

            val deleteWallet: Preference? = findPreference(getString(R.string.delete))
            deleteWallet!!.setOnPreferenceClickListener { //code for what you want it to do
                Toast.makeText(context, "Delete wallet clicked", Toast.LENGTH_SHORT).show();
                deleteWalletUser();
                true
            }

            val changePIN: Preference? = findPreference(getString(R.string.change_pin))
            changePIN!!.setOnPreferenceClickListener { //code for what you want it to do
                Toast.makeText(context, "Change PIN clicked", Toast.LENGTH_SHORT).show();
                true
            }

            val useFingerprint: Preference? = findPreference(getString(R.string.fingerprint))
            useFingerprint!!.setOnPreferenceClickListener { //code for what you want it to do
                Toast.makeText(context, "Use fingerprint clicked", Toast.LENGTH_SHORT).show();
                false
            }

        }


        private fun deleteWalletUser() {
            val walletID = requireActivity().intent.extras!!.getInt("walletid")
            val stringRequest: StringRequest = object : StringRequest(Method.DELETE, URLs.URL_USER_DELETE_WALLET + walletID,
                    Response.Listener { response ->
                        //                        progressBar.setVisibility(View.GONE);
                        try {
                            //converting response to json object
                            val obj = JSONObject(response)

                            //if no error in response
                            if (obj.getBoolean("success")) {
                                Toast.makeText(context, obj.getString("message"), Toast.LENGTH_SHORT).show()
                                requireActivity().finish()
                                Log.i(TAG, "----------ondelete $walletsFragment")

                                walletsFragment!!.updateWalletFragment()
                            } else {
                                Toast.makeText(context, obj.getString("message"), Toast.LENGTH_SHORT).show()
                            }
                        } catch (e: JSONException) {
                            e.printStackTrace()
                        }
                    },
                    Response.ErrorListener { error -> Toast.makeText(context, error.message, Toast.LENGTH_SHORT).show() }) {
                @Throws(AuthFailureError::class)
                override fun getParams(): Map<String, String> {
                    val params: MutableMap<String, String> = HashMap()
//                    params["wallet_name"] = walletID
                    return params
                }
            }
            VolleySingleton.getInstance(context).addToRequestQueue(stringRequest)
        }
    }

}

钱包碎片。java

      public class WalletsFragment extends Fragment implements SwipeRefreshLayout.OnRefreshListener {
    private RecyclerView mRecyclerView;
    private RecyclerView.Adapter mAdapter;
    private RecyclerView.LayoutManager mLayoutManager;

    private static String TAG = HomeFragment.class.getSimpleName();
    private TextView name;
    private TextView score;
    private SwipeRefreshLayout swipeRefreshLayout;

    private ArrayList<Wallet> userWalletList;

    private ProgressDialog progressDialog;

    boolean mIsRequest = false;

    private WalletsViewModel walletsViewModel;
    private ShimmerFrameLayout mShimmerViewContainer;

    public View onCreateView(@NonNull LayoutInflater inflater,
                             ViewGroup container, Bundle savedInstanceState) {
        walletsViewModel =
                ViewModelProviders.of(this).get(WalletsViewModel.class);
        View root = inflater.inflate(R.layout.fragment_wallets, container, false);
        TextView txtName = (TextView) root.findViewById(R.id.title);
        txtName.setText("Wallets");
        ImageView rightImageView = (ImageView) root.findViewById(R.id.notification);
        rightImageView.setImageResource(R.drawable.ic_action_account_balance_wallet);
        rightImageView.setColorFilter(ContextCompat.getColor(getContext(), R.color.colorPrimary));
        rightImageView.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                    CreateWalletDialog createWalletFragment = new CreateWalletDialog();
                createWalletFragment.show(getChildFragmentManager(), createWalletFragment.getTag());

//                CustomAlertDialog cdd=new CustomAlertDialog(getActivity());
//                cdd.show();
//                Toast.makeText(getContext(),"Create a new wallet", Toast.LENGTH_SHORT).show();
            }
        });

        mShimmerViewContainer = root.findViewById(R.id.shimmer_view_container);
        mRecyclerView = (RecyclerView) root.findViewById(R.id.recycler_view);
        swipeRefreshLayout = (SwipeRefreshLayout) root.findViewById(R.id.swipe_refresh_layout);
        swipeRefreshLayout.setOnRefreshListener(this);
        swipeRefreshLayout.setColorSchemeColors(getResources().getColor(安卓.R.color.holo_green_dark),
                getResources().getColor(安卓.R.color.holo_red_dark),
                getResources().getColor(安卓.R.color.holo_blue_dark),
                getResources().getColor(安卓.R.color.holo_orange_dark));
        mRecyclerView.setHasFixedSize(true);
        SnapHelper helper = new LinearSnapHelper();
        helper.attachToRecyclerView(mRecyclerView);
        mLayoutManager = new LinearLayoutManager(getContext(), LinearLayoutManager.VERTICAL, false);
        mRecyclerView.setLayoutManager(mLayoutManager);

        //initializing the walletlist
        userWalletList = new ArrayList<>();
        loadUserWallets();

        return root;
    }


    private void loadUserWallets() {
//        swipeRefreshLayout.setRefreshing(true);
//        progressDialog = ProgressDialog.show(getActivity(), "Fetching All Time Earners","Please Wait....", true);
        /*
         * Creating a String Request
         * The request type is GET defined by first parameter
         * The URL is defined in the second parameter
         * Then we have a Response Listener and a Error Listener
         * In response listener we will get the JSON response as a String
         * */

        if(mIsRequest)
            return;
        mIsRequest= true;
        userWalletList.clear();

        User user = SharedPrefManager.getInstance(getContext()).getUser();
        StringRequest stringRequest = new StringRequest(Request.Method.GET, URLs.URL_USER_WALLET_LIST + user.getId(),
                new Response.Listener<String>() {
                    @Override
                    public void onResponse(String response) {
                        mIsRequest = false;
                        try {
                            //converting the string to json object
                            JSONObject object=new JSONObject(response);
                            //getting data array from json response object
                            JSONArray array=object.getJSONArray("data");

                            for(int i=0;i<array.length();i++) {

                                //getting wallet object from json array
                                JSONObject userWallets=array.getJSONObject(i);

                                //adding the wallet to wallet list
                                userWalletList.add(new Wallet(
                                        userWallets.getInt("id"),
                                        userWallets.getInt("user_id"),
                                        userWallets.getString("wallet_name"),
                                        userWallets.getInt("wallet_id"),
                                        userWallets.getInt("wallet_type"),
                                        userWallets.getDouble("balance")
                                ));
                            }

                            //creating adapter object and setting it to recyclerview
                            WalletAdapter adapter = new WalletAdapter(getActivity(),getChildFragmentManager(), userWalletList);
                            mRecyclerView.setAdapter(adapter);
                            swipeRefreshLayout.setRefreshing(false);
                            // stop animating Shimmer and hide the layout
                            mShimmerViewContainer.stopShimmerAnimation();
                            mShimmerViewContainer.setVisibility(View.GONE);
//                            progressDialog.dismiss();
                            adapter.notifyDataSetChanged();
                        } catch (JSONException e) {
                            e.printStackTrace();
                        }
                    }
                },
                new Response.ErrorListener() {
                    @Override
                    public void onErrorResponse(VolleyError error) {
                        mIsRequest = false;
//                        progressDialog.dismiss();
//                        swipeRefreshLayout.setRefreshing(false);
                        Toast.makeText(getContext(),"No Internet Connection", Toast.LENGTH_SHORT).show();
                    }
                });

        //adding our stringrequest to queue
        Volley.newRequestQueue(getContext()).add(stringRequest);
    }

    @Override
    public void onRefresh() {
        swipeRefreshLayout.setRefreshing(true);
        User user = SharedPrefManager.getInstance(getContext()).getUser();
        VolleySingleton.getInstance(getContext()).getRequestQueue().getCache().invalidate(URLs.URL_USER_WALLET_LIST + user.getId(), true);
        loadUserWallets();
    }

    public void updateWalletFragment(){
        loadUserWallets();
    }

    @Override
    public void onResume() {
        super.onResume();
        mShimmerViewContainer.startShimmerAnimation();
    }

    @Override
    public void onPause() {
        mShimmerViewContainer.stopShimmerAnimation();
        super.onPause();
    }

}

我得到java.lang.NullPointerException: Attempt to invoke virtual method 'void java.util.ArrayList.clear()' on a null object reference at com.dreacot.dreacotwallet.ui.wallets.WalletsFragment.loadUserWallets(WalletsFragment.java:123)' on a null object reference

如果删除导致空指针的代码,就会得到其他空指针

我认为问题是我正在创建一个新的wallet fragment实例,而不是重用旧实例,如果这是真的,我如何重用旧实例呢

可能还需要注意,这是我如何从钱包适配器打开活动的

WalletAdapter。java

 private void showSettingsActivity(RecyclerView.ViewHolder dataObjectHolder){
        Intent intent = new Intent(mCtx , WalletSettings.class);
        Bundle bundle = new Bundle();
        String walletName = userWalletList.get(dataObjectHolder.getAdapterPosition()).getWalletName();
        int walletID = userWalletList.get(dataObjectHolder.getAdapterPosition()).getId();
        bundle.putString("walletname", walletName);
        bundle.putInt("walletid", walletID);
        intent.putExtras(bundle);
        mCtx.startActivity(intent);
    }

以及碎片的代码

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

        navView = findViewById(R.id.nav_view);
        addBadgeView();
        AppBarConfiguration appBarConfiguration = new AppBarConfiguration.Builder(
                R.id.navigation_home, R.id.navigation_transactions, R.id.navigation_wallets, R.id.navigation_account)
                .build();
        NavController navController = Navigation.findNavController(this, R.id.nav_host_fragment);
//        NavigationUI.setupActionBarWithNavController(this, navController, appBarConfiguration);
        NavigationUI.setupWithNavController(navView, navController);
    }

共 (1) 个答案

  1. # 1 楼答案

    当你在另一个Activity中时,你没有办法得到WalletFragment。对于您的情况,简单的解决方案是您应该使用方法startActivityForResult

    1. WalletAdapter向它传递walletFragment中,更改它的构造函数
    private Fragment mFragment;
    WalletAdapter(FragmentManager manager, ArrayList<Wallet> list, Fragment fragment) {
        // your init
        mFragment = fragment;
    }
    
    
    1. 更改方法导航到SettingActivity
     private void showSettingsActivity(RecyclerView.ViewHolder dataObjectHolder){
            Intent intent = new Intent(mCtx , WalletSettings.class);
            Bundle bundle = new Bundle();
            String walletName = userWalletList.get(dataObjectHolder.getAdapterPosition()).getWalletName();
            int walletID = userWalletList.get(dataObjectHolder.getAdapterPosition()).getId();
            bundle.putString("walletname", walletName);
            bundle.putInt("walletid", walletID);
            intent.putExtras(bundle);
            mFragment.startActivityForResult(intent, 123); // you can define request code
        }
    
    1. WalletFragment覆盖onActivityResult
        @Override
        protected void onActivityResult(int requestCode, int resultCode, Intent data) {
            super.onActivityResult(requestCode, resultCode, data);
            if (resultCode == Activity.RESULT_OK && requestCode == 123) {
                updateWalletFragment();
            }
        }
    
    1. SettingsFragment中,删除所有与walletFragment相关的代码,并将其更改为
    if (obj.getBoolean("success")) {
                                    Toast.makeText(context, obj.getString("message"), Toast.LENGTH_SHORT).show()
                                    requireActivity().setResult(Activity.RESULT_OK)
                                    requireActivity().finish()
                                }