简体   繁体   English

回收站视图中的弹出窗口

[英]Popup inside a recycler View

I need help because I face to an issue since a long time and I can't resolve it... I have this error message and my app crash when I click on a button:我需要帮助,因为我长期面临一个问题并且我无法解决它......我有这个错误消息并且当我点击一个按钮时我的应用程序崩溃:

 java.lang.NullPointerException: Attempt to invoke virtual method 'android.app.ActivityThread$ApplicationThread android.app.ActivityThread.getApplicationThread()' on a null object reference
        at android.app.Activity.startActivityForResult(Activity.java:5251)
        at androidx.fragment.app.FragmentActivity.startActivityForResult(FragmentActivity.java:675)
        at android.app.Activity.startActivityForResult(Activity.java:5208)
        at androidx.fragment.app.FragmentActivity.startActivityForResult(FragmentActivity.java:662)
        at android.app.Activity.startActivity(Activity.java:5579)
        at androidx.core.content.ContextCompat.startActivity(ContextCompat.java:251)
        at fr.amseu.mystretching.adapter.ChooserAdapter$onBindViewHolder$1.onClick(ChooserAdapter.kt:38)
        at android.view.View.performClick(View.java:7870)
        at android.view.View.performClickInternal(View.java:7839)
        at android.view.View.access$3600(View.java:886)
        at android.view.View$PerformClick.run(View.java:29363)
        at android.os.Handler.handleCallback(Handler.java:883)
        at android.os.Handler.dispatchMessage(Handler.java:100)
        at android.os.Looper.loop(Looper.java:237)
        at android.app.ActivityThread.main(ActivityThread.java:7948)
        at java.lang.reflect.Method.invoke(Native Method)
        at com.android.internal.os.RuntimeInit$MethodAndArgsCaller.run(RuntimeInit.java:493)
        at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:1075)

What happened is that I have a Recycler View of item in a Navigation bar.发生的事情是我在导航栏中有一个项目的回收站视图。 I would that when we click on an item, it open a popup.我希望当我们单击一个项目时,它会打开一个弹出窗口。 But with my code, when I click it happen the upper code.但是使用我的代码,当我单击它时,会出现上面的代码。 Here is my popup Activity ( that I have simplified to find the problem ):这是我的弹出活动(我已经简化以找到问题):

class MusclePopup(
        private val adapter: ChooserAdapter,
        private val currentItem: ChoosersModel
) : Dialog(adapter.context) {

    override fun onCreate(savedInstanceState: Bundle?) {
        super.onCreate(savedInstanceState)
        requestWindowFeature(Window.FEATURE_NO_TITLE)
        setContentView(R.layout.fragment_popup)
        setupComponents()
    }

    private fun setupComponents() {
        findViewById<TextView>(R.id.input_chooser).text = currentItem.description
    }
}

and this is my adapter Activity, the same that is used for my recycler view of the clicked items:这是我的适配器活动,与我的点击项目的回收者视图相同:

class ChooserAdapter(
        val context: ChooserActivity,
        private val ChooseList: ArrayList<ChoosersModel>)
    :RecyclerView.Adapter<ChooserAdapter.ViewHolder>(){

    class ViewHolder(view: View) : RecyclerView.ViewHolder(view) {
        var chooserNumber: TextView = view.findViewById(R.id.item_number)
        var chooserDescription: TextView = view.findViewById(R.id.item_description)
    }

    override fun onCreateViewHolder(parent: ViewGroup, viewType: Int): ViewHolder {
        val view = LayoutInflater.from(parent.context)
                .inflate(R.layout.item_chooser, parent, false)

        return ViewHolder(view)
    }
    override fun onBindViewHolder(holder: ViewHolder, position: Int) {
        val currentItem = ChooseList[position]

        holder.chooserNumber.text = currentItem.number
        holder.chooserDescription.text = currentItem.description
        holder.itemView.setOnClickListener {
            MusclePopup(this, currentItem).show()
        }
    }

    override fun getItemCount(): Int = ChooseList.size
}

With my tests, I think that the error comes from this line:通过我的测试,我认为错误来自这一行:

 holder.itemView.setOnClickListener {
            MusclePopup(this, currentItem).show()
        }

If you need more information, I can give it to you, thanks for any help !如果您需要更多信息,我可以给您,感谢您的帮助!

Use <YourActivity>.this wherever you need context.在需要上下文的任何地方使用<YourActivity>.this I am suspecting that context is null when initializing the Adapter.我怀疑初始化适配器时上下文是 null 。

I am not exactly sure as to why you are passing the whole adapter to your dialog, to inflate the dialog you just need the context which can be provided without providing the whole Adapter.我不确定为什么要将整个适配器传递给对话框,以使对话框膨胀,您只需要可以在不提供整个适配器的情况下提供的上下文。

Secondly, I feel it would be better if you separate out the code of inflating the dialog to your activity as that is where it should belong for which you can use an interface.其次,我觉得如果您将扩展对话框的代码分离到您的活动中会更好,因为它应该属于您可以使用界面的地方。

Here are the changes I will recommend.以下是我将推荐的更改。

ChooserAdapter.kt will be as follows now ChooserAdapter.kt现在如下

class ChooserAdapter(
        private val chooserAdapterCallback: ChooserAdapterCallback,
        private val ChooseList: ArrayList<ChoosersModel>)
    :RecyclerView.Adapter<ChooserAdapter.ViewHolder>(){

    class ViewHolder(view: View) : RecyclerView.ViewHolder(view) {
        var chooserNumber: TextView = view.findViewById(R.id.item_number)
        var chooserDescription: TextView = view.findViewById(R.id.item_description)
    }

    override fun onCreateViewHolder(parent: ViewGroup, viewType: Int): ViewHolder {
        val view = LayoutInflater.from(parent.context)
                .inflate(R.layout.item_chooser, parent, false)

        return ViewHolder(view)
    }
    override fun onBindViewHolder(holder: ViewHolder, position: Int) {
        val currentItem = ChooseList[position]

        holder.chooserNumber.text = currentItem.number
        holder.chooserDescription.text = currentItem.description
        holder.itemView.setOnClickListener {
            chooserAdapterCallback.onChooseAdapterClick(currentItem)
        }
    }

    override fun getItemCount(): Int = ChooseList.size
    
    interface ChooserAdapterCallback{
        fun onChooseAdapterClick(currentItem: ChoosersModel)
    }
}

Next, inside your ChooserActivity , you will have to implement, the interface, which will be roughly around as follows.接下来,在您的ChooserActivity中,您必须实现接口,大致如下。 Also you will need to modify your Adapter initialization and pass this there so roughly as ChooserAdapter(this, chooseList)此外,您将需要修改您的适配器初始化并将其大致传递给ChooserAdapter(this, chooseList)

class ChooserActivity : AppCompatActivity(), ChooserAdapter.ChooserAdapterCallback {
override fun onChooseAdapterClick(currentItem: ChoosersModel) {
        MusclePopup(this, currentItem).show()
    }
}

Next you will have to modify your MusclePopup class to as follows:接下来,您必须将 MusclePopup class 修改为如下:

class MusclePopup(
        private val context: Context,
        private val currentItem: ChoosersModel
) : Dialog(context)

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM