如何使用回收者视图在片段中传递正确的上下文?

时间:2019-04-10 14:01:11

标签: android kotlin

我正在Kotlin制作一个android应用程序,应该可以代表我的简历。我做到这一点时会考虑碎片和回收者的观点。 tabLayout中的第一个选项卡应该具有我的传记,第二个应该具有学历和第三次工作经验。同样,第二个和第三个选项卡应带有“回收者视图”。问题在于,其中一个回收者视图在上下文所在的位置返回null。教育和工作经验的工作方式相同。

我尝试了不同的上下文类型,但没有找到任何有效的方法。还尝试将layoutManager和DefaultItemAnimator移至MainActivity,但这没有帮助(当我将其移至MainActivity时,使用“ this”作为上下文)。

Github link: https://github.com/CookieTheMonsta/MyCV
class Education: Fragment() {

    companion object {
        fun newInstance(): Education {
            return Education()
        }
    }
    override fun onCreateView(inflater: LayoutInflater, container: ViewGroup?, savedInstanceState: Bundle?): View? {
        val view = inflater.inflate(R.layout.education,container,false)
        educationDisplay.layoutManager = LinearLayoutManager(container?.context,RecyclerView.VERTICAL,false)
        educationDisplay.itemAnimator = DefaultItemAnimator()
        educationDisplay.addItemDecoration(DividerItemDecoration(container?.context, RecyclerView.VERTICAL))
        return view
    }
}
class MainActivity : AppCompatActivity() {

    override fun onCreate(savedInstanceState: Bundle?) {
        super.onCreate(savedInstanceState)
        setContentView(R.layout.activity_main)

        setUpUi()
    }

    private fun setUpUi() {
        viewPager.adapter = FragmentAdapter(supportFragmentManager)
        tabLayout.setupWithViewPager(viewPager)


        displayData()

    }

    fun displayData() {

        /*educationDisplay.adapter = EducationAdapter(EducationRepository.educations)
        workexpDisplay.adapter = WorkExperienceAdapter(WorkRepository.works)*/
    }
}

Logcat的错误列表

2019-04-10 15:57:44.260 9004-9004/hr.ferit.sumigaborna.mycv E/AndroidRuntime: FATAL EXCEPTION: main
    Process: hr.ferit.sumigaborna.mycv, PID: 9004
    java.lang.RuntimeException: Unable to start activity ComponentInfo{hr.ferit.sumigaborna.mycv/hr.ferit.sumigaborna.mycv.MainActivity}: java.lang.IllegalStateException: educationDisplay must not be null
        at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2913)
        at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:3048)
        at android.app.servertransaction.LaunchActivityItem.execute(LaunchActivityItem.java:78)
        at android.app.servertransaction.TransactionExecutor.executeCallbacks(TransactionExecutor.java:108)
        at android.app.servertransaction.TransactionExecutor.execute(TransactionExecutor.java:68)
        at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1808)
        at android.os.Handler.dispatchMessage(Handler.java:106)
        at android.os.Looper.loop(Looper.java:193)
        at android.app.ActivityThread.main(ActivityThread.java:6669)
        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:858)
     Caused by: java.lang.IllegalStateException: educationDisplay must not be null
        at hr.ferit.sumigaborna.mycv.MainActivity.displayData(MainActivity.kt:39)
        at hr.ferit.sumigaborna.mycv.MainActivity.setUpUi(MainActivity.kt:33)
        at hr.ferit.sumigaborna.mycv.MainActivity.onCreate(MainActivity.kt:25)
        at android.app.Activity.performCreate(Activity.java:7136)
        at android.app.Activity.performCreate(Activity.java:7127)
        at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1271)
        at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2893)
        at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:3048) 
        at android.app.servertransaction.LaunchActivityItem.execute(LaunchActivityItem.java:78) 
        at android.app.servertransaction.TransactionExecutor.executeCallbacks(TransactionExecutor.java:108) 
        at android.app.servertransaction.TransactionExecutor.execute(TransactionExecutor.java:68) 
        at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1808) 
        at android.os.Handler.dispatchMessage(Handler.java:106) 
        at android.os.Looper.loop(Looper.java:193) 
        at android.app.ActivityThread.main(ActivityThread.java:6669) 
        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:858) 

应用程序应具有3个标签,首先是传记,您可以忽略它,因为它不会带来问题。 “教育和工作经验”标签应在其中包含我的信息的情况下为回收商提供视图。但是他们不是因为

1 个答案:

答案 0 :(得分:1)

您正在处理活动中的片段UI,您不应该这样做,这就是为什么要使用NPE的原因。

让我们一步一步走。定义活动并设置ViewPager及其选项卡的行为,这是您的活动类将拥有的唯一UI逻辑

class MainActivity : AppCompatActivity() {

    override fun onCreate(savedInstanceState: Bundle?) {
        super.onCreate(savedInstanceState)
        setContentView(R.layout.activity_main)

        setUpUi()
    }

    private fun setUpUi() {
        viewPager.adapter = FragmentAdapter(supportFragmentManager)
        tabLayout.setupWithViewPager(viewPager)

        // set tabLayout's behaviour with the corresponding fragments
    }
}

现在已设置好ViewPager,并添加了片段,请处理片段视图,例如:

class Education: Fragment() {

    companion object {
        fun newInstance(): Education {
            return Education()
        }
    }
    override fun onViewCreated(view: View?, savedInstanceState: Bundle?) {
        super.onViewCreated(view, savedInstanceState)
        displayEducationData()
    }

    private fun displayEducationData() {
        educationDisplay.layoutManager = LinearLayoutManager(activity,RecyclerView.VERTICAL,false)
        educationDisplay.itemAnimator = DefaultItemAnimator()
   educationDisplay.addItemDecoration(DividerItemDecoration(activity, RecyclerView.VERTICAL))
        educationDisplay.adapter = EducationAdapter(EducationRepository.educations)
    } 
}

现在对WorkExperience片段执行相同的操作。

PS:我假设您正在使用Kotlin视图扩展,如果没有,请不要忘记在findViewById内使用displayEducationData()来获得对EducationDisplay的引用