kotlin Android-студия список Посмотреть во фрагменте - PullRequest
0 голосов
/ 14 мая 2018

Представление списка правильно отображается в первом фрагменте, это первый экран моего приложения.

, но если я добавлю представление списка в другой фрагмент, который не является первым экраном приложения.Приложение закроется, когда я нажму на другие фрагменты.Пожалуйста, помогите решить эту проблему.

здесь с моим кодом FourFragment:

class FourFragment : Fragment() {

    override fun onCreateView(inflater: LayoutInflater, container: ViewGroup?,
                              savedInstanceState: Bundle?): View? {
        // Inflate the layout for this fragment
        return inflater.inflate(R.layout.fragment_four, container, false)

    }

    var adapter:TutorialAdapter? = null
    var listOfTutorial = TutorialList().list


    override fun onViewCreated(view: View, savedInstanceState: Bundle?) {
        super.onViewCreated(view, savedInstanceState)


        adapter = TutorialAdapter(context!!, mylist = listOfTutorial)
        tutorialListView.adapter = adapter
    }


    fun openYoutubeLink(youtubeID: String) {
        val intentApp = Intent(Intent.ACTION_VIEW, Uri.parse("vnd.youtube:" + youtubeID))
        val intentBrowser = Intent(Intent.ACTION_VIEW, Uri.parse("http://www.youtube.com/watch?v=" + youtubeID))
        try {
            this.startActivity(intentApp)
        } catch (ex: ActivityNotFoundException) {
            this.startActivity(intentBrowser)
        }
    }


    inner class TutorialAdapter : BaseAdapter {

        var mylist=ArrayList<Tutorial>()
        var context: Context?=null

        constructor(context: Context, mylist:ArrayList<Tutorial>):super() {

            this.context=context
            this.mylist=mylist
        }

        override fun getView(position: Int, convertView: View?, parent: ViewGroup?): View? {
            val tutorialget = mylist[position]
            var inflater = context!!.getSystemService(Context.LAYOUT_INFLATER_SERVICE) as LayoutInflater
            var myView = inflater.inflate(R.layout.tutorial_layout,null)
            myView.title.text = tutorialget.name
            myView.youtubeIcon.setImageResource(tutorialget.image!!)
            myView.youtubeIcon.setOnClickListener {
                openYoutubeLink(youtubeID = tutorialget.youtubeLink!!)
            }
            return myView
        }

        override fun getItem(position: Int): Any {
            return mylist[position]
        }

        override fun getItemId(position: Int): Long {
            return position.toLong()
        }

        override fun getCount(): Int {
            return mylist.size
        }
    }



}

Здесь с моим кодом MainActivity:

class MainActivity : AppCompatActivity() {

    private var tabLayout: TabLayout? = null
    var viewPager: ViewPager? = null

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


        //Action Bar
        val actionBar = supportActionBar
        actionBar!!.setDisplayShowHomeEnabled(true)
        actionBar.setBackgroundDrawable(ColorDrawable(Color.parseColor("#00FFFFFF")))
        actionBar.setIcon(R.drawable.title)
        actionBar.setDisplayShowTitleEnabled(false)




        viewPager = findViewById(R.id.viewpager) as ViewPager
        setupViewPager(viewPager!!)

        tabLayout = findViewById(R.id.tabs) as TabLayout
        tabLayout!!.setupWithViewPager(viewPager)

        val headerView = (getSystemService(Context.LAYOUT_INFLATER_SERVICE) as LayoutInflater)
                .inflate(R.layout.custom_tab, null, false)

        val linearLayoutOne = headerView.findViewById(R.id.ll) as LinearLayout
        val linearLayout2 = headerView.findViewById(R.id.ll2) as LinearLayout
        val linearLayout3 = headerView.findViewById(R.id.ll3) as LinearLayout
        val linearLayout4 = headerView.findViewById(R.id.ll4) as LinearLayout

        tabLayout!!.getTabAt(0)!!.setCustomView(linearLayoutOne)
        tabLayout!!.getTabAt(1)!!.setCustomView(linearLayout2)
        tabLayout!!.getTabAt(2)!!.setCustomView(linearLayout3)
        tabLayout!!.getTabAt(3)!!.setCustomView(linearLayout4)

    }

    private fun setupViewPager(viewPager: ViewPager) {
        val adapter = ViewPagerAdapter(supportFragmentManager)
        adapter.addFragment(OneFragment(), "ONE")
        adapter.addFragment(TwoFragment(), "TWO")
        adapter.addFragment(ThreeFragment(), "THREE")
        adapter.addFragment(FourFragment(), "FOUR")
        viewPager.adapter = adapter
    }

    internal inner class ViewPagerAdapter(manager: FragmentManager) : FragmentPagerAdapter(manager) {
        private val mFragmentList = ArrayList<Fragment>()
        private val mFragmentTitleList = ArrayList<String>()

        override fun getItem(position: Int): Fragment {
            return mFragmentList[position]
        }

        override fun getCount(): Int {
            return mFragmentList.size
        }

        fun addFragment(fragment: Fragment, title: String) {
            mFragmentList.add(fragment)
            mFragmentTitleList.add(title)
        }

        override fun getPageTitle(position: Int): CharSequence {
            return mFragmentTitleList[position]
        }
    }
}

enter image description here

enter image description here

1 Ответ

0 голосов
/ 14 мая 2018

Ваш ListView экземпляр является нулевым.Вы должны создать экземпляр tutorialListView внутри onCreateView

override fun onCreateView(inflater: LayoutInflater, container: ViewGroup?,
                          savedInstanceState: Bundle?): View? {
    // Inflate the layout for this fragment
    val view: View = inflater.inflate(R.layout.fragment_four, container, false)
    tutorialListView = view.findViewById<ListView>(R.id.recycler_view)

    return view
}
...