Tengo un proyector en Kotlin con la plantilla por defecto de MenuDrawer (Navigation Drawer Activity).
En la pantalla inicial, (Home_fragment) implemente un Slider con esta librería https://github.com/sayyam/carouselview
Al iniciar la aplicación esta funciona correctamente y se puede "navegar" por los diferentes fragment que tiene la app. pero al momento de volver al fragment inicial (Home_fragment) la aplicación se cierra.
Al ver el Logcat me sale que el error se produce al volver al Home_Fragment en la lógica del Slider.
Eh tratado de buscar la solución al problema, mantener el estado del fragment, ver librerías, videos, pero no he podido solucionar el problema.
Dejo el código del Home_Fragment.kt
import android.os.Bundle
import android.view.LayoutInflater
import android.view.View
import android.view.ViewGroup
import android.widget.ImageView
import android.widget.TextView
import androidx.fragment.app.Fragment
import androidx.lifecycle.Observer
import androidx.lifecycle.ViewModelProviders
import androidx.navigation.fragment.findNavController
import com.example.mec.R
import kotlinx.android.synthetic.main.fragment_home.*
class HomeFragment : Fragment() {
var sampleImages = intArrayOf(
R.drawable.slider1,
R.drawable.slider2,
R.drawable.slider3,
R.drawable.slider4,
R.drawable.slider5,
R.drawable.slider6,
R.drawable.slider7,
R.drawable.slider8,
R.drawable.slider9
)
var cities = arrayOf(
"Aris",
"Grand",
"Habimec",
"Genimec",
"Aris",
"Grand",
"Habimec",
"Genimec",
"Genimec"
)
private lateinit var homeViewModel: HomeViewModel
override fun onCreateView(
inflater: LayoutInflater,
container: ViewGroup?,
savedInstanceState: Bundle?
): View? {
homeViewModel =
ViewModelProviders.of(this).get(HomeViewModel::class.java)
val root = inflater.inflate(R.layout.fragment_home, container, false)
val textView: TextView = root.findViewById(R.id.text_home)
homeViewModel.text.observe(viewLifecycleOwner, Observer {
textView.text = it
})
return root
}
override fun onViewCreated(view: View, savedInstanceState: Bundle?) {
super.onViewCreated(view, savedInstanceState)
btn_ver_todo.setOnClickListener {
findNavController().navigate(R.id.action_nav_home_to_nav_gallery)
}
btn_familia.setOnClickListener {
findNavController().navigate(R.id.action_nav_home_to_familiasFragment)
}
btn_hidrobox.setOnClickListener {
findNavController().navigate(R.id.action_nav_home_to_hidroboxFragment)
}
btn_temporizador.setOnClickListener {
findNavController().navigate(R.id.action_nav_home_to_temporizadoFragment)
}
btn_control.setOnClickListener {
findNavController().navigate(R.id.action_nav_home_to_elementosControlFragment)
}
btn_industrial.setOnClickListener {
findNavController().navigate(R.id.action_nav_home_to_tomasIndustrialesFragment)
}
btn_conexion.setOnClickListener {
findNavController().navigate(R.id.action_nav_home_to_elementosConexionFragment)
}
btn_emt.setOnClickListener {
findNavController().navigate(R.id.action_nav_home_to_EMTFragment)
}
btn_tablero.setOnClickListener {
findNavController().navigate(R.id.action_nav_home_to_CTGFragment)
}
btn_sujecion.setOnClickListener {
findNavController().navigate(R.id.action_nav_home_to_accesoriosSujecionFragment)
}
btn_accesorios.setOnClickListener {
findNavController().navigate(R.id.action_nav_home_to_accesoriosIntaladoresFragment)
}
// Slider
carouselView.pageCount = cities.size
carouselView.setImageListener { position, imageView ->
imageView.setImageResource(sampleImages[position])
imageView.setScaleType(ImageView.ScaleType.FIT_XY)
}
}
}
El Logcat que marca error en la linea 117 (HomeFragment.kt:117)
2020-10-02 00:35:52.011 21804-21804/com.example.mec E/MessageQueue-JNI: Exception in MessageQueue callback: handleReceiveCallback
2020-10-02 00:35:52.022 21804-21804/com.example.mec E/MessageQueue-JNI: java.lang.RuntimeException: View must set ImageListener or ViewListener.
at com.synnapps.carouselview.CarouselView$CarouselPagerAdapter.instantiateItem(CarouselView.java:346)
at androidx.viewpager.widget.ViewPager.addNewItem(ViewPager.java:1010)
at androidx.viewpager.widget.ViewPager.populate(ViewPager.java:1158)
at androidx.viewpager.widget.ViewPager.populate(ViewPager.java:1092)
at androidx.viewpager.widget.ViewPager.setOffscreenPageLimit(ViewPager.java:856)
at com.synnapps.carouselview.CarouselView.setData(CarouselView.java:255)
at com.synnapps.carouselview.CarouselView.setPageCount(CarouselView.java:437)
at com.example.mec.ui.home.HomeFragment.onViewCreated(HomeFragment.kt:117)
at androidx.fragment.app.FragmentManagerImpl.moveToState(FragmentManager.java:1471)
at androidx.fragment.app.FragmentManagerImpl.addAddedFragments(FragmentManager.java:2646)
at androidx.fragment.app.FragmentManagerImpl.executeOpsTogether(FragmentManager.java:2416)
at androidx.fragment.app.FragmentManagerImpl.removeRedundantOperationsAndExecute(FragmentManager.java:2372)
at androidx.fragment.app.FragmentManagerImpl.popBackStackImmediate(FragmentManager.java:884)
at androidx.fragment.app.FragmentManagerImpl.popBackStackImmediate(FragmentManager.java:827)
at androidx.fragment.app.FragmentManagerImpl.popBackStackImmediate(FragmentManager.java:874)
at androidx.fragment.app.FragmentManagerImpl.popBackStackImmediate(FragmentManager.java:827)
at androidx.fragment.app.FragmentActivity.onBackPressed(FragmentActivity.java:190)
at android.app.Activity.onKeyUp(Activity.java:2826)
at android.view.KeyEvent.dispatch(KeyEvent.java:2766)
at androidx.core.view.KeyEventDispatcher.activitySuperDispatchKeyEventPre28(KeyEventDispatcher.java:137)
at androidx.core.view.KeyEventDispatcher.dispatchKeyEvent(KeyEventDispatcher.java:87)
at androidx.core.app.ComponentActivity.dispatchKeyEvent(ComponentActivity.java:126)
at androidx.appcompat.app.AppCompatActivity.dispatchKeyEvent(AppCompatActivity.java:535)
at androidx.appcompat.view.WindowCallbackWrapper.dispatchKeyEvent(WindowCallbackWrapper.java:59)
at androidx.appcompat.app.AppCompatDelegateImpl$AppCompatWindowCallback.dispatchKeyEvent(AppCompatDelegateImpl.java:2533)
at androidx.appcompat.view.WindowCallbackWrapper.dispatchKeyEvent(WindowCallbackWrapper.java:59)
at com.android.internal.policy.DecorView.dispatchKeyEvent(DecorView.java:353)
at android.view.ViewRootImpl$ViewPostImeInputStage.processKeyEvent(ViewRootImpl.java:4748)
at android.view.ViewRootImpl$ViewPostImeInputStage.onProcess(ViewRootImpl.java:4719)
at android.view.ViewRootImpl$InputStage.deliver(ViewRootImpl.java:4255)
at android.view.ViewRootImpl$InputStage.onDeliverToNext(ViewRootImpl.java:4308)
at android.view.ViewRootImpl$InputStage.forward(ViewRootImpl.java:4274)
at android.view.ViewRootImpl$AsyncInputStage.forward(ViewRootImpl.java:4401)
at android.view.ViewRootImpl$InputStage.apply(ViewRootImpl.java:4282)
at android.view.ViewRootImpl$AsyncInputStage.apply(ViewRootImpl.java:4458)
at android.view.ViewRootImpl$InputStage.deliver(ViewRootImpl.java:4255)
at android.view.ViewRootImpl$InputStage.onDeliverToNext(ViewRootImpl.java:4308)
at android.view.ViewRootImpl$InputStage.forward(ViewRootImpl.java:4274)
at android.view.ViewRootImpl$InputStage.apply(ViewRootImpl.java:4282)
at android.view.ViewRootImpl$InputStage.deliver(ViewRootImpl.java:4255)
at android.view.ViewRootImpl$InputStage.onDeliverToNext(ViewRootImpl.java:4308)
at android.view.ViewRootImpl$InputStage.forward(ViewRootImpl.java:4274)
at android.view.ViewRootImpl$AsyncInputStage.forward(ViewRootImpl.java:4434)
at android.view.ViewRootImpl$ImeInputStage.onFinishedInputEvent(ViewRootImpl.java:4595)
at android.view.inputmethod.InputMethodManager$PendingEvent.run(InputMethodManager.java:2512)
at android.view.inputmethod.InputMethodManager.invokeFinishedInputEventCallback(InputMethodManager.java:2106)
at android.view.inputmethod.InputMethodManager.finishedInputEvent(InputMethodManager.java:2097)
at android.vi
2020-10-02 00:35:52.023 21804-21804/com.example.mec E/AndroidRuntime: FATAL EXCEPTION: main
Process: com.example.mec, PID: 21804
java.lang.RuntimeException: View must set ImageListener or ViewListener.
at com.synnapps.carouselview.CarouselView$CarouselPagerAdapter.instantiateItem(CarouselView.java:346)
at androidx.viewpager.widget.ViewPager.addNewItem(ViewPager.java:1010)
at androidx.viewpager.widget.ViewPager.populate(ViewPager.java:1158)
at androidx.viewpager.widget.ViewPager.populate(ViewPager.java:1092)
at androidx.viewpager.widget.ViewPager.setOffscreenPageLimit(ViewPager.java:856)
at com.synnapps.carouselview.CarouselView.setData(CarouselView.java:255)
at com.synnapps.carouselview.CarouselView.setPageCount(CarouselView.java:437)
at com.example.mec.ui.home.HomeFragment.onViewCreated(HomeFragment.kt:117)
at androidx.fragment.app.FragmentManagerImpl.moveToState(FragmentManager.java:1471)
at androidx.fragment.app.FragmentManagerImpl.addAddedFragments(FragmentManager.java:2646)
at androidx.fragment.app.FragmentManagerImpl.executeOpsTogether(FragmentManager.java:2416)
at androidx.fragment.app.FragmentManagerImpl.removeRedundantOperationsAndExecute(FragmentManager.java:2372)
at androidx.fragment.app.FragmentManagerImpl.popBackStackImmediate(FragmentManager.java:884)
at androidx.fragment.app.FragmentManagerImpl.popBackStackImmediate(FragmentManager.java:827)
at androidx.fragment.app.FragmentManagerImpl.popBackStackImmediate(FragmentManager.java:874)
at androidx.fragment.app.FragmentManagerImpl.popBackStackImmediate(FragmentManager.java:827)
at androidx.fragment.app.FragmentActivity.onBackPressed(FragmentActivity.java:190)
at android.app.Activity.onKeyUp(Activity.java:2826)
at android.view.KeyEvent.dispatch(KeyEvent.java:2766)
at androidx.core.view.KeyEventDispatcher.activitySuperDispatchKeyEventPre28(KeyEventDispatcher.java:137)
at androidx.core.view.KeyEventDispatcher.dispatchKeyEvent(KeyEventDispatcher.java:87)
at androidx.core.app.ComponentActivity.dispatchKeyEvent(ComponentActivity.java:126)
at androidx.appcompat.app.AppCompatActivity.dispatchKeyEvent(AppCompatActivity.java:535)
at androidx.appcompat.view.WindowCallbackWrapper.dispatchKeyEvent(WindowCallbackWrapper.java:59)
at androidx.appcompat.app.AppCompatDelegateImpl$AppCompatWindowCallback.dispatchKeyEvent(AppCompatDelegateImpl.java:2533)
at androidx.appcompat.view.WindowCallbackWrapper.dispatchKeyEvent(WindowCallbackWrapper.java:59)
at com.android.internal.policy.DecorView.dispatchKeyEvent(DecorView.java:353)
at android.view.ViewRootImpl$ViewPostImeInputStage.processKeyEvent(ViewRootImpl.java:4748)
at android.view.ViewRootImpl$ViewPostImeInputStage.onProcess(ViewRootImpl.java:4719)
at android.view.ViewRootImpl$InputStage.deliver(ViewRootImpl.java:4255)
at android.view.ViewRootImpl$InputStage.onDeliverToNext(ViewRootImpl.java:4308)
at android.view.ViewRootImpl$InputStage.forward(ViewRootImpl.java:4274)
at android.view.ViewRootImpl$AsyncInputStage.forward(ViewRootImpl.java:4401)
at android.view.ViewRootImpl$InputStage.apply(ViewRootImpl.java:4282)
at android.view.ViewRootImpl$AsyncInputStage.apply(ViewRootImpl.java:4458)
at android.view.ViewRootImpl$InputStage.deliver(ViewRootImpl.java:4255)
at android.view.ViewRootImpl$InputStage.onDeliverToNext(ViewRootImpl.java:4308)
at android.view.ViewRootImpl$InputStage.forward(ViewRootImpl.java:4274)
at android.view.ViewRootImpl$InputStage.apply(ViewRootImpl.java:4282)
at android.view.ViewRootImpl$InputStage.deliver(ViewRootImpl.java:4255)
at android.view.ViewRootImpl$InputStage.onDeliverToNext(ViewRootImpl.java:4308)
at android.view.ViewRootImpl$InputStage.forward(ViewRootImpl.java:4274)
at android.view.ViewRootImpl$AsyncInputStage.forward(ViewRootImpl.java:4434)
at android.view.ViewRootImpl$ImeInputStage.onFinishedInputEvent(ViewRootImpl.java:4595)
at android.view.inputmethod.InputMethodManager$PendingEvent.run(InputMethodManager.java:2512)
at android.view.inputmethod.InputMethodManager.invokeFinishedInputEventCallback(InputMethodManager.java:2106)
2020-10-02 00:35:52.024 21804-21804/com.example.mec E/AndroidRuntime: at android.view.inputmethod.InputMethodManager.finishedInputEvent(InputMethodManager.java:2097)
at android.view.inputmethod.InputMethodManager$ImeInputEventSender.onInputEventFinished(InputMethodManager.java:2487)
at android.view.InputEventSender.dispatchInputEventFinished(InputEventSender.java:141)
at android.os.MessageQueue.nativePollOnce(Native Method)
at android.os.MessageQueue.next(MessageQueue.java:356)
at android.os.Looper.loop(Looper.java:138)
at android.app.ActivityThread.main(ActivityThread.java:6523)
at java.lang.reflect.Method.invoke(Native Method)
at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:942)
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:832)
Ojala alguien me pueda ayudar.
de antemano Muchas Gracias.