在类似条件下进行转换时,textView Nullpointer异常

时间:2018-04-02 02:24:04

标签: android android-fragments kotlin android-tablayout

背景
我正在动态填充textview和imageview以获得标准化的片段 我没有创建6个片段布局和活动,而是创建了3个,我根据需要设置了文本/图像。这样我就可以节省时间和内存,而不是每个活动创建一个片段,而唯一改变的是textView和imageView中的文本。
问题
当它到达片段3时,当实现与FragmenFirst,FragmentSecond相同时,我得到一个nullpointer,而我不知道为什么。
代码
填充tablayout的页面适配器

class FragmentFirst : Fragment() {

    override fun onCreateView(inflater: LayoutInflater?, container: ViewGroup?,
                              savedInstanceState: Bundle?): View? {
        // Inflate the layout for this fragment "

        return inflater!!.inflate(R.layout.fragment_fragment_first, container, false)

    }

碎片是标准的,没有任何改变

class FragmentSecond : Fragment() {

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


}

第二

class FragmentThird : Fragment() {

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


}

第三

import android.os.Bundle import android.support.design.widget.NavigationView import android.support.design.widget.TabLayout import android.support.v4.view.GravityCompat import android.support.v7.app.ActionBarDrawerToggle import android.support.v7.app.AppCompatActivity import android.view.Menu import android.view.MenuItem import com.company.app.fragments.* import kotlinx.android.synthetic.main.activity_main.* import kotlinx.android.synthetic.main.app_bar_main.* import kotlinx.android.synthetic.main.content_appbar_main.* import kotlinx.android.synthetic.main.fragment_fragment_first.* import kotlinx.android.synthetic.main.fragment_fragment_second.* import kotlinx.android.synthetic.main.fragment_fragment_third.*

class MainActivity : AppCompatActivity(), NavigationView.OnNavigationItemSelectedListener {
    var objPageAdapter:CustomPageAdapter?=null

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

override fun onNavigationItemSelected(item: MenuItem): Boolean {
        // Handle navigation view item clicks here.
        objPageAdapter = CustomPageAdapter(supportFragmentManager)

        when (item.itemId) {
            R.id.nav_fruits -> {
                objPageAdapter!!.removeFragments()
                objPageAdapter!!.addFragments(FragmentFirst(),getString(R.string.txt_tab_fruits_Element_1))
                objPageAdapter!!.addFragments(FragmentSecond(),getString(R.string.txt_tab_fruits_Element_2))
                objPageAdapter!!.addFragments(FragmentThird(),getString(R.string.txt_tab_fruits_Element_3))
                ViewPagerMain.adapter = objPageAdapter
                TabLayoutMain.setupWithViewPager(ViewPagerMain)
                this.textViewFragment_1.text = getString(R.string.txt_txtView_fruits_Element_1)
                this.textViewFragment_2.text = getString(R.string.txt_txtView_fruits_Element_2)
                this.textViewFragment_3.text = getString(R.string.txt_txtView_fruits_Element_3)

...

主要活动更改每个

的textViews的部分
<?xml version="1.0" encoding="utf-8"?> <RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:tools="http://schemas.android.com/tools"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    tools:context=".fragments.FragmentSecond">


    <ImageView
        android:id="@+id/imageViewFragment_2"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_alignParentTop="true"
        android:layout_centerHorizontal="true"
        android:layout_gravity="center_horizontal"
        android:src="@drawable/ic_banana" />

    <TextView
        android:id="@+id/textViewFragment_2"
        android:layout_width="match_parent"
        android:layout_height="261dp"
        android:layout_alignParentBottom="true"
        android:layout_alignParentStart="true" />

</RelativeLayout>

错误

DEMO

Xml片段2和3作为样本

片段2

<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:tools="http://schemas.android.com/tools"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    tools:context=".fragments.FragmentThird">



    <ImageView
        android:id="@+id/imageViewFragment_3"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_alignParentTop="true"
        android:layout_centerHorizontal="true"
        android:layout_gravity="center_horizontal"
        android:src="@drawable/ic_banana" />

    <TextView
        android:id="@+id/textViewFragment_3"
        android:layout_width="match_parent"
        android:layout_height="261dp"
        android:layout_alignParentBottom="true"
        android:layout_alignParentStart="true" />

</RelativeLayout>

片段3

E/AndroidRuntime: FATAL EXCEPTION: main
                  Process: com.company.app, PID: 6790
                  java.lang.IllegalStateException: this.textViewFragment_3 must not be null
                      at com.company.app.MainActivity.onNavigationItemSelected(MainActivity.kt:111)
                      at android.support.design.widget.NavigationView$1.onMenuItemSelected(NavigationView.java:154)
                      at android.support.v7.view.menu.MenuBuilder.dispatchMenuItemSelected(MenuBuilder.java:822)
                      at android.support.v7.view.menu.MenuItemImpl.invoke(MenuItemImpl.java:171)
                      at android.support.v7.view.menu.MenuBuilder.performItemAction(MenuBuilder.java:973)
                      at android.support.design.internal.NavigationMenuPresenter$1.onClick(NavigationMenuPresenter.java:342)
                      at android.view.View.performClick(View.java:6294)
                      at android.view.View$PerformClick.run(View.java:24770)
                      at android.os.Handler.handleCallback(Handler.java:790)
                      at android.os.Handler.dispatchMessage(Handler.java:99)
                      at android.os.Looper.loop(Looper.java:164)
                      at android.app.ActivityThread.main(ActivityThread.java:6494)
                      at java.lang.reflect.Method.invoke(Native Method)
                      at com.android.internal.os.RuntimeInit$MethodAndArgsCaller.run(RuntimeInit.java:438)
                      at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:807)


使用的方法
1.清除/重建项目
2.关闭/重启PC

编辑:根据请求,我粘贴了我正在获取的堆栈跟踪

...
ViewPagerMain.addOnPageChangeListener(object : ViewPager.OnPageChangeListener {
                override fun onPageScrolled(position: Int, positionOffset: Float, positionOffsetPixels: Int) {}
                private lateinit var fr: Fragment
                private lateinit var viewPager: ViewPager
                private lateinit var textView1:TextView
                private lateinit var textView2:TextView
                private lateinit var textView3:TextView
                override fun onPageSelected(position: Int) {
                    when (position) {
                        0 ->{
                            fr = getSupportFragmentManager().findFragmentByTag("android:switcher:" + R.id.ViewPagerMain + ":" + ViewPagerMain.getCurrentItem())
                            textView1 = fr.textViewFragment_1
                            textView1.text= "test1"
                        }
                        1 ->{
                            fr = getSupportFragmentManager().findFragmentByTag("android:switcher:" + R.id.ViewPagerMain + ":" + ViewPagerMain.getCurrentItem())
                            textView2 = fr.textViewFragment_2
                            textView2.text= "test2"
                        }
                        2 ->{
                            fr = getSupportFragmentManager().findFragmentByTag("android:switcher:" + R.id.ViewPagerMain + ":" + ViewPagerMain.getCurrentItem())
                            textView3.text= "test3"
                        }

Edit2:致电Rahul回答
主要活动中的嵌入代码

Library/WebKit/LocalStorage/

1 个答案:

答案 0 :(得分:4)

不使用onCreate访问textView,而是使用onPageChangeListener事件

 private lateinit var fr: Fragment;
private lateinit var viewPager: ViewPager
private lateinit var textView1:TextView
private lateinit var textView2:TextView
private lateinit var textView3:TextView
override fun onPageSelected(position: Int) {
    when (position) {
        0 ->{
            fr = getSupportFragmentManager().findFragmentByTag("android:switcher:" + R.id.viewPager + ":" + viewPager.getCurrentItem())

            textView1 = fr.textViewFragment_1
        }



        1-> {
            fr = getSupportFragmentManager().findFragmentByTag("android:switcher:" + R.id.viewPager + ":" + viewPager.getCurrentItem())

            textView2 = fr.textViewFragment_2

            textView2.text = "rr"
        }



        2-> {
            fr = getSupportFragmentManager().findFragmentByTag("android:switcher:" + R.id.viewPager + ":" + viewPager.getCurrentItem())

            textView3= fr.textViewFragment_3

            textView3.text = "tt"
        }

    }
}

override fun onPageScrollStateChanged(state: Int) {
    TODO("not implemented") //To change body of created functions use File | Settings | File Templates.
}


override fun onCreate(savedInstanceState: Bundle?, persistentState: PersistableBundle?) {
    super.onCreate(savedInstanceState, persistentState)
    setContentView(R.layout.test_layout)


}

在onPageselected中使用这些行来获取当前可见片段的实例

Fragment fr = getSupportFragmentManager().findFragmentByTag(“android:switcher:” + R.id.pager + “:” + mViewPager.getCurrentItem());

现在您可以访问textview但是检查fr instancOf Fragment1 textView1 = fr.findViewById(textview1Id)等os ..........