Как начать действие с фрагмента с помощью Page Viewer? - PullRequest
0 голосов
/ 27 мая 2019

Запуск операции с фрагмента, который находится внутри Просмотрщика страниц?

Я пробовал

startActivity(activity!!.(Intent(activity!!,UserVerification::class.java)))

activity!!.startActivity(activity!!.(Intent(activity!!,UserVerification::class.java)))

startActivity(Intent(view!!.context,UserVerification::class.java))

view!!.context.startActivity(Intent(view!!.context,UserVerification::class.java))

Приведенный выше код не работает.MainActivity работает отлично.

MainActivity.kt

class MainActivity : AppCompatActivity() {

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

        val pagerAdapter = ScreenSlidePagerAdapter(supportFragmentManager)
        pagerAdapter.addFragment(LoginFragment())
        pagerAdapter.addFragment(RegisterFragment())
        viewPager.adapter = pagerAdapter
    }

    override fun onBackPressed() {
        if (viewPager.currentItem == 0) {
            super.onBackPressed()
        } else {
            viewPager.currentItem = viewPager.currentItem - 1
        }
    }

    internal inner class ScreenSlidePagerAdapter(fm: FragmentManager) : FragmentPagerAdapter(fm) {
        private val fragmentList = ArrayList<Fragment>()

        override fun getItem(i: Int): Fragment{
            return fragmentList[i]
        }

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

        fun addFragment(fragment: Fragment) {
            fragmentList.add(fragment)
        }
    }
}

Я не знаю, как запустить действие из класса RegisterFragment, который находится в средстве просмотра страниц

RegisterFragment.kt

class RegisterFragment : Fragment() {

    private lateinit var registerBtn : Button

    override fun onCreateView(inflater: LayoutInflater, container: ViewGroup?,
                              savedInstanceState: Bundle?): View? {

        var view =  inflater.inflate(R.layout.fragment_register, container, false)
        registerBtn = view.findViewById(R.id.btn_register)

        return view
    }

    override fun onResume() {
        super.onResume()

        registerBtn.setOnClickListener {
             view!!.context.startActivity(Intent(view!!.context,UserVerification::class.java))
            }
        }

}

UserVerification.kt

class UserVerification : AppCompatActivity() {

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

        val pagerAdapter = ScreenSlidePagerAdapter(supportFragmentManager)
        pagerAdapter.addFragment(UserPhoneNumberFragment())
        pagerAdapter.addFragment(UserOtpFragment())
        viewPager.adapter = pagerAdapter
    }

    internal inner class ScreenSlidePagerAdapter(fm: FragmentManager) : FragmentPagerAdapter(fm) {
        private val fragmentList = ArrayList<Fragment>()

        override fun getItem(i: Int): Fragment {
            return fragmentList[i]
        }

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

        fun addFragment(fragment: Fragment) {
            fragmentList.add(fragment)
        }
    }
}

Есть ли проблемы с AndroidManifest

AndroidManifest.xml

<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
          package="com.robin.android.cpbusiness">

    <application
            android:allowBackup="true"
            android:icon="@mipmap/ic_launcher"
            android:label="@string/app_name"
            android:roundIcon="@mipmap/ic_launcher_round"
            android:supportsRtl="true"
            android:theme="@style/AppTheme">
        <activity android:name=".UserVerification">
        </activity>
        <activity
                android:name=".MainActivity"
                android:theme="@style/AppTheme.NoActionBar">
            <intent-filter>
                <action android:name="android.intent.action.MAIN"/>

                <category android:name="android.intent.category.LAUNCHER"/>
            </intent-filter>
        </activity>
    </application>

</manifest>

Пожалуйста, взгляните на эту LogCat Logcat

2019-05-27 11:06:14.954 26815-26815/com.robin.android.cpbusiness E/AndroidRuntime: FATAL EXCEPTION: main
    Process: com.robin.android.cpbusiness, PID: 26815
    java.lang.RuntimeException: Unable to start activity ComponentInfo{com.robin.android.cpbusiness/com.robin.android.cpbusiness.UserVerification}: java.lang.IllegalStateException: viewPager must not be null
        at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2723)
        at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2784)
        at android.app.ActivityThread.-wrap12(ActivityThread.java)
        at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1523)
        at android.os.Handler.dispatchMessage(Handler.java:102)
        at android.os.Looper.loop(Looper.java:163)
        at android.app.ActivityThread.main(ActivityThread.java:6238)
        at java.lang.reflect.Method.invoke(Native Method)
        at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:933)
        at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:823)
     Caused by: java.lang.IllegalStateException: viewPager must not be null
        at com.robin.android.cpbusiness.UserVerification.onCreate(UserVerification.kt:19)
        at android.app.Activity.performCreate(Activity.java:6857)
        at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1119)
        at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2676)
        at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2784) 
        at android.app.ActivityThread.-wrap12(ActivityThread.java) 
        at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1523) 
        at android.os.Handler.dispatchMessage(Handler.java:102) 
        at android.os.Looper.loop(Looper.java:163) 
        at android.app.ActivityThread.main(ActivityThread.java:6238) 
        at java.lang.reflect.Method.invoke(Native Method) 
        at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:933) 
        at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:823) 
Добро пожаловать на сайт PullRequest, где вы можете задавать вопросы и получать ответы от других членов сообщества.
...