I wrote bellow code in fragment that extends 'VideoSupportFragment'
val playerGlue = PlaybackTransportControlGlue(
activity,
MediaPlayerAdapter(activity))
playerGlue.setHost(VideoSupportFragmentGlueHost(this))
playerGlue.addPlayerCallback(object : PlaybackGlue.PlayerCallback() {
override fun onPreparedStateChanged(glue: PlaybackGlue) {
if (glue.isPrepared()) {
//playerGlue.seekProvider = MySeekProvider()
playerGlue.play()
}
}
})
playerGlue.playerAdapter?.setDataSource(Uri.parse("http://techslides.com/demos/sample-videos/small.mp4"))
When setHost is called app crashes and bellow log appears in logcat.
If i comment setHost, just audio get play.
2020-03-15 02:32:15.578 1779-1779/tv.debug E/AndroidRuntime: FATAL EXCEPTION: main
Process: tv.debug, PID: 1779
android.view.InflateException: Binary XML file line #33: Binary XML file line #33: Error inflating class <unknown>
Caused by: android.view.InflateException: Binary XML file line #33: Error inflating class <unknown>
Caused by: java.lang.reflect.InvocationTargetException
at java.lang.reflect.Constructor.newInstance0(Native Method)
at java.lang.reflect.Constructor.newInstance(Constructor.java:430)
at android.view.LayoutInflater.createView(LayoutInflater.java:645)
at com.android.internal.policy.PhoneLayoutInflater.onCreateView(PhoneLayoutInflater.java:58)
at android.view.LayoutInflater.onCreateView(LayoutInflater.java:717)
at android.view.LayoutInflater.createViewFromTag(LayoutInflater.java:785)
at android.view.LayoutInflater.createViewFromTag(LayoutInflater.java:727)
at android.view.LayoutInflater.rInflate(LayoutInflater.java:858)
at android.view.LayoutInflater.rInflateChildren(LayoutInflater.java:821)
at android.view.LayoutInflater.rInflate(LayoutInflater.java:861)
at android.view.LayoutInflater.rInflateChildren(LayoutInflater.java:821)
at android.view.LayoutInflater.inflate(LayoutInflater.java:518)
at android.view.LayoutInflater.inflate(LayoutInflater.java:426)
at androidx.leanback.widget.PlaybackTransportRowPresenter.createRowViewHolder(PlaybackTransportRowPresenter.java:684)
at androidx.leanback.widget.RowPresenter.onCreateViewHolder(RowPresenter.java:326)
at androidx.leanback.widget.ItemBridgeAdapter.onCreateViewHolder(ItemBridgeAdapter.java:352)
at androidx.recyclerview.widget.RecyclerView$Adapter.createViewHolder(RecyclerView.java:7078)
at androidx.recyclerview.widget.RecyclerView$Recycler.tryGetViewHolderForPositionByDeadline(RecyclerView.java:6235)
at androidx.recyclerview.widget.RecyclerView$Recycler.getViewForPosition(RecyclerView.java:6118)
at androidx.recyclerview.widget.RecyclerView$Recycler.getViewForPosition(RecyclerView.java:6114)
at androidx.leanback.widget.GridLayoutManager.getViewForPosition(GridLayoutManager.java:1085)
at androidx.leanback.widget.GridLayoutManager$2.createItem(GridLayoutManager.java:1613)
at androidx.leanback.widget.SingleRow.appendVisibleItems(SingleRow.java:113)
at androidx.leanback.widget.Grid.appendOneColumnVisibleItems(Grid.java:389)
at androidx.leanback.widget.GridLayoutManager.appendOneColumnVisibleItems(GridLayoutManager.java:1839)
at androidx.leanback.widget.GridLayoutManager.onLayoutChildren(GridLayoutManager.java:2276)
at androidx.recyclerview.widget.RecyclerView.dispatchLayoutStep2(RecyclerView.java:4134)
at androidx.recyclerview.widget.RecyclerView.dispatchLayout(RecyclerView.java:3851)
at androidx.recyclerview.widget.RecyclerView.consumePendingUpdateOperations(RecyclerView.java:1897)
at androidx.recyclerview.widget.RecyclerView$1.run(RecyclerView.java:414)
at android.view.Choreographer$CallbackRecord.run(Choreographer.java:874)
at android.view.Choreographer.doCallbacks(Choreographer.java:686)
at android.view.Choreographer.doFrame(Choreographer.java:618)
at android.view.Choreographer$FrameDisplayEventReceiver.run(Choreographer.java:860)
at android.os.Handler.handleCallback(Handler.java:751)
at android.os.Handler.dispatchMessage(Handler.java:95)
at android.os.Looper.loop(Looper.java:154)
at android.app.ActivityThread.main(ActivityThread.java:6119)
at java.lang.reflect.Method.invoke(Native Method)
at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:886)
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:776)
Caused by: java.lang.UnsupportedOperationException: Failed to resolve attribute at index 68: TypedValue{t=0x2/d=0x7f04005d a=-1}
at android.content.res.TypedArray.getDimensionPixelSize(TypedArray.java:716)
at android.view.View.<init>(View.java:4228)
at android.view.ViewGroup.<init>(ViewGroup.java:579)
at android.widget.LinearLayout.<init>(LinearLayout.java:211)
at android.widget.LinearLayout.<init>(LinearLayout.java:207)
2020-03-15 02:32:15.578 1779-1779/com.shatelland.namava.tv.debug E/AndroidRuntime: at android.widget.LinearLayout.<init>(LinearLayout.java:203)
I don't know why the class is unknown.
i can't find which layout has error because of this.
After long time searching i find the reason.
The Activity that contains the Fragment should set theme to Leanback
override fun onCreate(savedInstanceState: Bundle?) {
super.onCreate(savedInstanceState)
setTheme(R.style.Theme_Leanback)
}
Related
My app is perfectly running on Huawei Y9 prime which has android 10 and it crashes on Redmi Note 4 which has android 6. I made the project with Android 5...While I am installing the app on Redmi note 4 it isn't starting...A white screen comes for a very short time and the floats down...No error is shown in logcat, but this is shown in "Run" and that too when I click the app icon 3-4 times -
E/AndroidRuntime: FATAL EXCEPTION: main
Process: com.js11.a8minutesworkout, PID: 29896
java.lang.RuntimeException: Unable to start activity ComponentInfo{com.js11.a8minutesworkout/com.js11.a8minutesworkout.MainActivity}: android.view.InflateException: Binary XML file line #25: Binary XML file line #25: Error inflating class <unknown>
at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2423)
at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2483)
at android.app.ActivityThread.access$900(ActivityThread.java:153)
at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1349)
at android.os.Handler.dispatchMessage(Handler.java:102)
at android.os.Looper.loop(Looper.java:148)
at android.app.ActivityThread.main(ActivityThread.java:5438)
at java.lang.reflect.Method.invoke(Native Method)
at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:738)
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:628)
Caused by: android.view.InflateException: Binary XML file line #25: Binary XML file line #25: Error inflating class <unknown>
at android.view.LayoutInflater.inflate(LayoutInflater.java:543)
at android.view.LayoutInflater.inflate(LayoutInflater.java:427)
at android.view.LayoutInflater.inflate(LayoutInflater.java:374)
at androidx.appcompat.app.AppCompatDelegateImpl.setContentView(AppCompatDelegateImpl.java:696)
at androidx.appcompat.app.AppCompatActivity.setContentView(AppCompatActivity.java:170)
at com.js11.a8minutesworkout.MainActivity.onCreate(MainActivity.kt:15)
at android.app.Activity.performCreate(Activity.java:6303)
at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1108)
at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2376)
at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2483)
at android.app.ActivityThread.access$900(ActivityThread.java:153)
at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1349)
at android.os.Handler.dispatchMessage(Handler.java:102)
at android.os.Looper.loop(Looper.java:148)
at android.app.ActivityThread.main(ActivityThread.java:5438)
at java.lang.reflect.Method.invoke(Native Method)
at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:738)
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:628)
Caused by: android.view.InflateException: Binary XML file line #25: Error inflating class <unknown>
at android.view.LayoutInflater.createView(LayoutInflater.java:649)
at com.android.internal.policy.PhoneLayoutInflater.onCreateView(PhoneLayoutInflater.java:58)
at android.view.LayoutInflater.onCreateView(LayoutInflater.java:698)
at android.view.LayoutInflater.createViewFromTag(LayoutInflater.java:766)
at android.view.LayoutInflater.createViewFromTag(LayoutInflater.java:708)
at android.view.LayoutInflater.rInflate(LayoutInflater.java:839)
at android.view.LayoutInflater.rInflateChildren(LayoutInflater.java:802)
at android.view.LayoutInflater.rInflate(LayoutInflater.java:842)
at android.view.LayoutInflater.rInflateChildren(LayoutInflater.java:802)
at android.view.LayoutInflater.inflate(LayoutInflater.java:519)
at android.view.LayoutInflater.inflate(LayoutInflater.java:427)
at android.view.LayoutInflater.inflate(LayoutInflater.java:374)
at androidx.appcompat.app.AppCompatDelegateImpl.setContentView(AppCompatDelegateImpl.java:696)
at androidx.appcompat.app.AppCompatActivity.setContentView(AppCompatActivity.java:170)
at com.js11.a8minutesworkout.MainActivity.onCreate(MainActivity.kt:15)
at android.app.Activity.performCreate(Activity.java:6303)
at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1108)
at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2376)
at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2483)
at android.app.ActivityThread.access$900(ActivityThread.java:153)
at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1349)
at android.os.Handler.dispatchMessage(Handler.java:102)
at android.os.Looper.loop(Looper.java:148)
at android.app.ActivityThread.main(ActivityThread.java:5438)
at java.lang.reflect.Method.invoke(Native Method)
at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:738)
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:628)
Caused by: java.lang.reflect.InvocationTargetException
at java.lang.reflect.Constructor.newInstance(Native Method)
at android.view.LayoutInflater.createView(LayoutInflater.java:623)
at com.android.internal.policy.PhoneLayoutInflater.onCreateView(PhoneLayoutInflater.java:58)
at android.view.LayoutInflater.onCreateView(LayoutInflater.java:698)
at android.view.LayoutInflater.createViewFromTag(LayoutInflater.java:766)
at android.view.LayoutInflater.createViewFromTag(LayoutInflater.java:708)
at android.view.LayoutInflater.rInflate(LayoutInflater.java:839)
at android.view.LayoutInflater.rInflateChildren(LayoutInflater.java:802)
at android.view.LayoutInflater.rInflate(LayoutInflater.java:842)
at android.view.LayoutInflater.rInflateChildren(LayoutInflater.java:802)
at android.view.LayoutInflater.inflate(LayoutInflater.java:519)
at android.view.LayoutInflater.inflate(LayoutInflater.java:427)
at android.view.LayoutInflater.inflate(LayoutInflater.java:374)
at androidx.appcompat.app.AppCompatDelegateImpl.setContentView(AppCompatDelegateImpl.java:696)
at androidx.appcompat.app.AppCompatActivity.setContentView(AppCompatActivity.java:170)
at com.js11.a8minutesworkout.MainActivity.onCreate(MainActivity.kt:15)
at android.app.Activity.performCreate(Activity.java:6303)
at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1108)
at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2376)
at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2483)
at android.app.ActivityThread.access$900(ActivityThread.java:153)
at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1349)
at android.os.Handler.dispatchMessage(Handler.java:102)
at android.os.Looper.loop(Looper.java:148)
at android.app.ActivityThread.main(ActivityThread.java:5438)
at java.lang.reflect.Method.invoke(Native Method)
at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:738)
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:628)
Caused by: android.content.res.Resources$NotFoundException: Resource "com.js11.a8minutesworkout:drawable/item_color_accent_border_ripple_background" (7f07008c) is not a Drawable (color or path): TypedValue{t=0x1/d=0x7f07008c a=-1 r=0x7f07008c}
at android.content.res.Resources.loadDrawableForCookie(Resources.java:2646)
at android.content.res.Resources.loadDrawable(Resources.java:2580)
at android.content.res.MiuiResources.loadDrawable(MiuiResources.java:387)
at android.content.res.TypedArray.getDrawable(TypedArray.java:872)
at android.view.View.<init>(View.java:3955)
at android.view.ViewGroup.<init>(ViewGroup.java:574)
at android.widget.LinearLayout.<init>(LinearLayout.java:203)
at android.widget.LinearLayout.<init>(LinearLayout.java:199)
at android.widget.LinearLayout.<init>(LinearLayout.java:195)
at java.lang.reflect.Constructor.newInstance(Native Method)
at android.view.LayoutInflater.createView(LayoutInflater.java:623)
at com.android.internal.policy.PhoneLayoutInflater.onCreateView(PhoneLayoutInflater.java:58)
at android.view.LayoutInflater.onCreateView(LayoutInflater.java:698)
at android.view.LayoutInflater.createViewFromTag(LayoutInflater.java:766)
at android.view.LayoutInflater.createViewFromTag(LayoutInflater.java:708)
at android.view.LayoutInflater.rInflate(LayoutInflater.java:839)
at android.view.LayoutInflater.rInflateChildren(LayoutInflater.java:802)
at android.view.LayoutInflater.rInflate(LayoutInflater.java:842)
at android.view.LayoutInflater.rInflateChildren(LayoutInflater.java:802)
at android.view.LayoutInflater.inflate(LayoutInflater.java:519)
at android.view.LayoutInflater.inflate(LayoutInflater.java:427)
at android.view.LayoutInflater.inflate(LayoutInflater.java:374)
at androidx.appcompat.app.AppCompatDelegateImpl.setContentView(AppCompatDelegateImpl.java:696)
at androidx.appcompat.app.AppCompatActivity.setContentView(AppCompatActivity.java:170)
at com.js11.a8minutesworkout.MainActivity.onCreate(MainActivity.kt:15)
at android.app.Activity.performCreate(Activity.java:6303)
at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1108)
at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2376)
at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2483)
at android.app.ActivityThread.access$900(ActivityThread.java:153)
at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1349)
at android.os.Handler.dispatchMessage(Handler.java:102)
at android.os.Looper.loop(Looper.java:148)
at android.app.ActivityThread.main(ActivityThread.java:5438)
at java.lang.reflect.Method.invoke(Native Method)
at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:738)
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:628)
MainActivity code -
class MainActivity : AppCompatActivity() {
#RequiresApi(Build.VERSION_CODES.LOLLIPOP)
override fun onCreate(savedInstanceState: Bundle?) {
super.onCreate(savedInstanceState)
setContentView(R.layout.activity_main)
window.setFlags(WindowManager.LayoutParams.FLAG_FULLSCREEN,
WindowManager.LayoutParams.FLAG_FULLSCREEN)
window.navigationBarColor = resources.getColor(R.color.backgroundColor)
llStart.setOnClickListener {
val intent = Intent(this, ExerciseActivity::class.java)
startActivity(intent)
}
llBMI.setOnClickListener{
val intent = Intent(this, BMIActivity::class.java)
startActivity(intent)
}
llHistory.setOnClickListener {
val intent = Intent(this, HistoryActivity::class.java)
startActivity(intent)
}
}
}
drawable/item_color_accent_border_ripple_background -
<?xml version="1.0" encoding="utf-8"?>
<item android:id="#android:id/mask">
<shape android:shape="oval">
<solid android:color="#000000" /><!--ye color jahe jo bhi rkho isse frk nh pdne wala-->
</shape>
</item>
<item android:drawable="#drawable/item_circular_color_accent_border" />
I am new to android developing and I am very frustrated with this problem..
PLEASE HELP
maybe you can change android sdk version.
in androidmanifest.xml file.
targetsdkversion and minsdkversion first check.
llStart.setOnClickListener {
val intent = Intent(MainActivity.this, ExerciseActivity::class.java)
startActivity(intent)
}
llBMI.setOnClickListener{
val intent = Intent(MainActivity.this, BMIActivity::class.java)
startActivity(intent)
}
llHistory.setOnClickListener {
val intent = Intent(MainActivity.this, HistoryActivity::class.java)
startActivity(intent)
}
only this will point to the parent function that is onclicklistener, so you want to use activity's context then you should use className.this
I am using this library to make calendar app similar to google calendar week view calendar. I read all documentation and added implementation 'com.jonaswanke.calendar:calendar:0.1.1' in build.gradle, but when I add calendar view to activity_mail.xml
<com.jonaswanke.calendar.CalendarView
android:id="#+id/calendar"
android:layout_width="match_parent"
android:layout_height="match_parent"
app:range="week" />
following exception crashed my app on app start.
E/AndroidRuntime: FATAL EXCEPTION: main
Process: com.example.myapplication, PID: 13101
java.lang.RuntimeException: Unable to start activity ComponentInfo{com.example.myapplication/com.example.myapplication.MainActivity}: android.view.InflateException: Binary XML file line #9 in com.example.myapplication:layout/activity_main: Binary XML file line #9 in com.example.myapplication:layout/activity_main: Error inflating class com.jonaswanke.calendar.CalendarView
at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:3895)
at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:4074)
at android.app.servertransaction.LaunchActivityItem.execute(LaunchActivityItem.java:91)
at android.app.servertransaction.TransactionExecutor.executeCallbacks(TransactionExecutor.java:149)
at android.app.servertransaction.TransactionExecutor.execute(TransactionExecutor.java:103)
at android.app.ActivityThread$H.handleMessage(ActivityThread.java:2473)
at android.os.Handler.dispatchMessage(Handler.java:110)
at android.os.Looper.loop(Looper.java:219)
at android.app.ActivityThread.main(ActivityThread.java:8347)
at java.lang.reflect.Method.invoke(Native Method)
at com.android.internal.os.RuntimeInit$MethodAndArgsCaller.run(RuntimeInit.java:513)
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:1055)
Caused by: android.view.InflateException: Binary XML file line #9 in com.example.myapplication:layout/activity_main: Binary XML file line #9 in com.example.myapplication:layout/activity_main: Error inflating class com.jonaswanke.calendar.CalendarView
Caused by: android.view.InflateException: Binary XML file line #9 in com.example.myapplication:layout/activity_main: Error inflating class com.jonaswanke.calendar.CalendarView
Caused by: java.lang.reflect.InvocationTargetException
at java.lang.reflect.Constructor.newInstance0(Native Method)
at java.lang.reflect.Constructor.newInstance(Constructor.java:343)
at android.view.LayoutInflater.createView(LayoutInflater.java:864)
at android.view.LayoutInflater.createViewFromTag(LayoutInflater.java:1016)
at android.view.LayoutInflater.createViewFromTag(LayoutInflater.java:971)
at android.view.LayoutInflater.rInflate(LayoutInflater.java:1133)
at android.view.LayoutInflater.rInflateChildren(LayoutInflater.java:1094)
at android.view.LayoutInflater.inflate(LayoutInflater.java:692)
at android.view.LayoutInflater.inflate(LayoutInflater.java:536)
at android.view.LayoutInflater.inflate(LayoutInflater.java:483)
at androidx.appcompat.app.AppCompatDelegateImpl.setContentView(AppCompatDelegateImpl.java:696)
at androidx.appcompat.app.AppCompatActivity.setContentView(AppCompatActivity.java:170)
at com.example.myapplication.MainActivity.onCreate(MainActivity.kt:27)
at android.app.Activity.performCreate(Activity.java:8085)
at android.app.Activity.performCreate(Activity.java:8073)
at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1320)
at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:3868)
at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:4074)
at android.app.servertransaction.LaunchActivityItem.execute(LaunchActivityItem.java:91)
at android.app.servertransaction.TransactionExecutor.executeCallbacks(TransactionExecutor.java:149)
at android.app.servertransaction.TransactionExecutor.execute(TransactionExecutor.java:103)
at android.app.ActivityThread$H.handleMessage(ActivityThread.java:2473)
at android.os.Handler.dispatchMessage(Handler.java:110)
at android.os.Looper.loop(Looper.java:219)
at android.app.ActivityThread.main(ActivityThread.java:8347)
at java.lang.reflect.Method.invoke(Native Method)
at com.android.internal.os.RuntimeInit$MethodAndArgsCaller.run(RuntimeInit.java:513)
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:1055)
Caused by: java.lang.NoClassDefFoundError: Failed resolution of: Lkotlinx/coroutines/experimental/CoroutineDispatcher;
at com.jonaswanke.calendar.CalendarView.<init>(CalendarView.kt:495)
E/AndroidRuntime: at com.jonaswanke.calendar.CalendarView.<init>(CalendarView.kt:32)
at com.jonaswanke.calendar.CalendarView.<init>(Unknown Source:6)
... 28 more
Caused by: java.lang.ClassNotFoundException: kotlinx.coroutines.experimental.CoroutineDispatcher
at java.lang.VMClassLoader.findLoadedClass(Native Method)
at java.lang.ClassLoader.findLoadedClass(ClassLoader.java:738)
at java.lang.ClassLoader.loadClass(ClassLoader.java:363)
at java.lang.ClassLoader.loadClass(ClassLoader.java:312)
... 31 more
Caused by: java.lang.NoClassDefFoundError: Failed resolution of: Lkotlin/coroutines/experimental/AbstractCoroutineContextElement;
... 31 more
Caused by: java.lang.ClassNotFoundException: Didn't find class "kotlin.coroutines.experimental.AbstractCoroutineContextElement" on path: DexPathList[[zip file "/data/app/com.example.myapplication-_1QFEnFxaCJxdEfjNQvp0g==/base.apk"],nativeLibraryDirectories=[/data/app/com.example.myapplication-_1QFEnFxaCJxdEfjNQvp0g==/lib/arm64, /system/lib64, /hw_product/lib64, /system/product/lib64, /prets/lib64]]
at dalvik.system.BaseDexClassLoader.findClass(BaseDexClassLoader.java:209)
at java.lang.ClassLoader.loadClass(ClassLoader.java:379)
at java.lang.ClassLoader.loadClass(ClassLoader.java:312)
... 31 more
I/Process: Sending signal. PID: 13101 SIG: 9
In MainActivity.kt file I only added:
override fun onCreate(savedInstanceState: Bundle?) {
super.onCreate(savedInstanceState)
setContentView(R.layout.activity_main)
calendar.eventRequestCallback = {
populate(it)
}
}
#Suppress("MagicNumber")
private fun populate(week: Week, force: Boolean = false) {
if (!force && calendar.cachedEvents.contains(week))
return
val events = mutableListOf<Event>()
for (i in 0..15) {
val id = nextId++.toString()
val start = week.start + abs(random.nextLong()) % DateUtils.WEEK_IN_MILLIS
events.add(BaseEvent(
id,
id,
(random.nextInt() or 0xFF000000.toInt()) and 0x00202020.inv(),
start,
start + abs(random.nextLong()) % (DateUtils.DAY_IN_MILLIS / 8)))
}
for (i in 0..3) {
val id = nextId++.toString()
val start = week.start + abs(random.nextLong()) % DateUtils.WEEK_IN_MILLIS
events.add(BaseEvent(
id,
id,
(random.nextInt() or 0xFF000000.toInt()) and 0x00202020.inv(),
start,
start + abs(random.nextLong()) % (DateUtils.DAY_IN_MILLIS * 7),
true))
}
calendar.setEventsForWeek(week, events)
}
But even without this code app still crashes.
This is an old library that was built using experimental Kotlin features. It hasn't been maintained for two years. I tried for 20 minutes to get it to compile. It's just not worth it. I suggest using one of the several other Android CalendarView libraries out there:
https://github.com/search?q=android+calendarview
This one looks particularly promising:
https://github.com/kizitonwose/CalendarView
Fatal Exception: java.lang.RuntimeException: Unable to start activity ComponentInfo{com.***.***/com.***.***.activities.SearchActivity}: android.view.InflateException: Binary XML file line #29: Binary XML file line #29: Error inflating class android.widget.SearchView
at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2451)
at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2511)
at android.app.ActivityThread.access$900(ActivityThread.java:165)
at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1375)
at android.os.Handler.dispatchMessage(Handler.java:102)
at android.os.Looper.loop(Looper.java:150)
at android.app.ActivityThread.main(ActivityThread.java:5621)
at java.lang.reflect.Method.invoke(Method.java)
at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:794)
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:684)
Caused by java.lang.reflect.InvocationTargetException
at java.lang.reflect.Constructor.newInstance(Constructor.java)
at android.view.LayoutInflater.createView(LayoutInflater.java:638)
at com.mikepenz.iconics.context.InternalLayoutInflater.onCreateView(InternalLayoutInflater.java:54)
at android.view.LayoutInflater.onCreateView(LayoutInflater.java:713)
at com.mikepenz.iconics.context.InternalLayoutInflater.onCreateView(InternalLayoutInflater.java:70)
at android.view.LayoutInflater.createViewFromTag(LayoutInflater.java:781)
at android.view.LayoutInflater.createViewFromTag(LayoutInflater.java:723)
at android.view.LayoutInflater.rInflate(LayoutInflater.java:854)
at android.view.LayoutInflater.rInflateChildren(LayoutInflater.java:817)
at android.view.LayoutInflater.rInflate(LayoutInflater.java:857)
at android.view.LayoutInflater.rInflateChildren(LayoutInflater.java:817)
at android.view.LayoutInflater.inflate(LayoutInflater.java:534)
at com.mikepenz.iconics.context.InternalLayoutInflater.inflate(InternalLayoutInflater.java:81)
at android.view.LayoutInflater.inflate(LayoutInflater.java:434)
at android.view.LayoutInflater.inflate(LayoutInflater.java:377)
at androidx.appcompat.app.AppCompatDelegateImpl.setContentView(AppCompatDelegateImpl.java:469)
at androidx.appcompat.app.AppCompatActivity.setContentView(AppCompatActivity.java:140)
at com.***.***.activities.SearchActivity.onCreate(SearchActivity.java:124)
at android.app.Activity.performCreate(Activity.java:6367)
at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1110)
at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2404)
at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2511)
at android.app.ActivityThread.access$900(ActivityThread.java:165)
at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1375)
at android.os.Handler.dispatchMessage(Handler.java:102)
at android.os.Looper.loop(Looper.java:150)
at android.app.ActivityThread.main(ActivityThread.java:5621)
at java.lang.reflect.Method.invoke(Method.java)
at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:794)
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:684)
Caused by android.view.InflateException: Binary XML file line #79: Binary XML file line #79: Error inflating class android.widget.SearchView$SearchAutoComplete
at android.view.LayoutInflater.inflate(LayoutInflater.java:558)
at com.mikepenz.iconics.context.InternalLayoutInflater.inflate(InternalLayoutInflater.java:81)
at android.view.LayoutInflater.inflate(LayoutInflater.java:434)
at android.widget.SearchView.inflateSearchView(SearchView.java:1847)
at android.widget.SearchView.(SearchView.java:268)
at android.widget.SearchView.(SearchView.java:259)
at android.widget.SearchView.(SearchView.java:255)
at java.lang.reflect.Constructor.newInstance(Constructor.java)
at android.view.LayoutInflater.createView(LayoutInflater.java:638)
at com.mikepenz.iconics.context.InternalLayoutInflater.onCreateView(InternalLayoutInflater.java:54)
at android.view.LayoutInflater.onCreateView(LayoutInflater.java:713)
at com.mikepenz.iconics.context.InternalLayoutInflater.onCreateView(InternalLayoutInflater.java:70)
at android.view.LayoutInflater.createViewFromTag(LayoutInflater.java:781)
at android.view.LayoutInflater.createViewFromTag(LayoutInflater.java:723)
at android.view.LayoutInflater.rInflate(LayoutInflater.java:854)
at android.view.LayoutInflater.rInflateChildren(LayoutInflater.java:817)
at android.view.LayoutInflater.rInflate(LayoutInflater.java:857)
at android.view.LayoutInflater.rInflateChildren(LayoutInflater.java:817)
at android.view.LayoutInflater.inflate(LayoutInflater.java:534)
at com.mikepenz.iconics.context.InternalLayoutInflater.inflate(InternalLayoutInflater.java:81)
at android.view.LayoutInflater.inflate(LayoutInflater.java:434)
at android.view.LayoutInflater.inflate(LayoutInflater.java:377)
at androidx.appcompat.app.AppCompatDelegateImpl.setContentView(AppCompatDelegateImpl.java:469)
at androidx.appcompat.app.AppCompatActivity.setContentView(AppCompatActivity.java:140)
at com.***.***.activities.SearchActivity.onCreate(SearchActivity.java:124)
at android.app.Activity.performCreate(Activity.java:6367)
at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1110)
at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2404)
at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2511)
at android.app.ActivityThread.access$900(ActivityThread.java:165)
at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1375)
at android.os.Handler.dispatchMessage(Handler.java:102)
at android.os.Looper.loop(Looper.java:150)
at android.app.ActivityThread.main(ActivityThread.java:5621)
at java.lang.reflect.Method.invoke(Method.java)
at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:794)
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:684)
Caused by android.view.InflateException: Binary XML file line #79: Error inflating class android.widget.SearchView$SearchAutoComplete
at android.view.LayoutInflater.createViewFromTag(LayoutInflater.java:801)
at android.view.LayoutInflater.createViewFromTag(LayoutInflater.java:723)
at android.view.LayoutInflater.rInflate(LayoutInflater.java:854)
at android.view.LayoutInflater.rInflateChildren(LayoutInflater.java:817)
at android.view.LayoutInflater.rInflate(LayoutInflater.java:857)
at android.view.LayoutInflater.rInflateChildren(LayoutInflater.java:817)
at android.view.LayoutInflater.rInflate(LayoutInflater.java:857)
at android.view.LayoutInflater.rInflateChildren(LayoutInflater.java:817)
at android.view.LayoutInflater.inflate(LayoutInflater.java:534)
at com.mikepenz.iconics.context.InternalLayoutInflater.inflate(InternalLayoutInflater.java:81)
at android.view.LayoutInflater.inflate(LayoutInflater.java:434)
at android.widget.SearchView.inflateSearchView(SearchView.java:1847)
at android.widget.SearchView.(SearchView.java:268)
at android.widget.SearchView.(SearchView.java:259)
at android.widget.SearchView.(SearchView.java:255)
at java.lang.reflect.Constructor.newInstance(Constructor.java)
at android.view.LayoutInflater.createView(LayoutInflater.java:638)
at com.mikepenz.iconics.context.InternalLayoutInflater.onCreateView(InternalLayoutInflater.java:54)
at android.view.LayoutInflater.onCreateView(LayoutInflater.java:713)
at com.mikepenz.iconics.context.InternalLayoutInflater.onCreateView(InternalLayoutInflater.java:70)
at android.view.LayoutInflater.createViewFromTag(LayoutInflater.java:781)
at android.view.LayoutInflater.createViewFromTag(LayoutInflater.java:723)
at android.view.LayoutInflater.rInflate(LayoutInflater.java:854)
at android.view.LayoutInflater.rInflateChildren(LayoutInflater.java:817)
at android.view.LayoutInflater.rInflate(LayoutInflater.java:857)
at android.view.LayoutInflater.rInflateChildren(LayoutInflater.java:817)
at android.view.LayoutInflater.inflate(LayoutInflater.java:534)
at com.mikepenz.iconics.context.InternalLayoutInflater.inflate(InternalLayoutInflater.java:81)
at android.view.LayoutInflater.inflate(LayoutInflater.java:434)
at android.view.LayoutInflater.inflate(LayoutInflater.java:377)
at androidx.appcompat.app.AppCompatDelegateImpl.setContentView(AppCompatDelegateImpl.java:469)
at androidx.appcompat.app.AppCompatActivity.setContentView(AppCompatActivity.java:140)
at com.***.***.activities.SearchActivity.onCreate(SearchActivity.java:124)
at android.app.Activity.performCreate(Activity.java:6367)
at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1110)
at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2404)
at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2511)
at android.app.ActivityThread.access$900(ActivityThread.java:165)
at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1375)
at android.os.Handler.dispatchMessage(Handler.java:102)
at android.os.Looper.loop(Looper.java:150)
at android.app.ActivityThread.main(ActivityThread.java:5621)
at java.lang.reflect.Method.invoke(Method.java)
at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:794)
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:684)
Caused by java.lang.NullPointerException: Attempt to invoke virtual method 'boolean android.widget.SearchView.isInActionMode()' on a null object reference
at android.widget.SearchView$SearchAutoComplete.enoughToFilter(SearchView.java:1801)
at android.widget.AutoCompleteTextView.doAfterTextChanged(AutoCompleteTextView.java:781)
at android.widget.AutoCompleteTextView$MyWatcher.afterTextChanged(AutoCompleteTextView.java:749)
at android.widget.TextView.sendAfterTextChanged(TextView.java:8232)
at android.widget.TextView.setText(TextView.java:4435)
at android.widget.TextView.setText(TextView.java:4283)
at android.widget.EditText.setText(EditText.java:84)
at android.widget.TextView.setText(TextView.java:4258)
at com.mikepenz.iconics.Iconics$IconicsBuilderView.build(Iconics.java:297)
at com.mikepenz.iconics.context.IconicsFactory.onViewCreatedInternal(IconicsFactory.java:50)
at com.mikepenz.iconics.context.IconicsFactory.onViewCreated(IconicsFactory.java:27)
at com.mikepenz.iconics.context.InternalLayoutInflater$PrivateWrapperFactory2.onCreateView(InternalLayoutInflater.java:245)
at android.view.LayoutInflater$FactoryMerger.onCreateView(LayoutInflater.java:189)
at android.view.LayoutInflater.createViewFromTag(LayoutInflater.java:773)
at android.view.LayoutInflater.createViewFromTag(LayoutInflater.java:723)
at android.view.LayoutInflater.rInflate(LayoutInflater.java:854)
at android.view.LayoutInflater.rInflateChildren(LayoutInflater.java:817)
at android.view.LayoutInflater.rInflate(LayoutInflater.java:857)
at android.view.LayoutInflater.rInflateChildren(LayoutInflater.java:817)
at android.view.LayoutInflater.rInflate(LayoutInflater.java:857)
at android.view.LayoutInflater.rInflateChildren(LayoutInflater.java:817)
at android.view.LayoutInflater.inflate(LayoutInflater.java:534)
at com.mikepenz.iconics.context.InternalLayoutInflater.inflate(InternalLayoutInflater.java:81)
at android.view.LayoutInflater.inflate(LayoutInflater.java:434)
at android.widget.SearchView.inflateSearchView(SearchView.java:1847)
at android.widget.SearchView.(SearchView.java:268)
at android.widget.SearchView.(SearchView.java:259)
at android.widget.SearchView.(SearchView.java:255)
at java.lang.reflect.Constructor.newInstance(Constructor.java)
at android.view.LayoutInflater.createView(LayoutInflater.java:638)
at com.mikepenz.iconics.context.InternalLayoutInflater.onCreateView(InternalLayoutInflater.java:54)
at android.view.LayoutInflater.onCreateView(LayoutInflater.java:713)
at com.mikepenz.iconics.context.InternalLayoutInflater.onCreateView(InternalLayoutInflater.java:70)
at android.view.LayoutInflater.createViewFromTag(LayoutInflater.java:781)
at android.view.LayoutInflater.createViewFromTag(LayoutInflater.java:723)
at android.view.LayoutInflater.rInflate(LayoutInflater.java:854)
at android.view.LayoutInflater.rInflateChildren(LayoutInflater.java:817)
at android.view.LayoutInflater.rInflate(LayoutInflater.java:857)
at android.view.LayoutInflater.rInflateChildren(LayoutInflater.java:817)
at android.view.LayoutInflater.inflate(LayoutInflater.java:534)
at com.mikepenz.iconics.context.InternalLayoutInflater.inflate(InternalLayoutInflater.java:81)
at android.view.LayoutInflater.inflate(LayoutInflater.java:434)
at android.view.LayoutInflater.inflate(LayoutInflater.java:377)
at androidx.appcompat.app.AppCompatDelegateImpl.setContentView(AppCompatDelegateImpl.java:469)
at androidx.appcompat.app.AppCompatActivity.setContentView(AppCompatActivity.java:140)
at com.***.***.activities.SearchActivity.onCreate(SearchActivity.java:124)
at android.app.Activity.performCreate(Activity.java:6367)
at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1110)
at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2404)
at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2511)
at android.app.ActivityThread.access$900(ActivityThread.java:165)
at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1375)
at android.os.Handler.dispatchMessage(Handler.java:102)
at android.os.Looper.loop(Looper.java:150)
at android.app.ActivityThread.main(ActivityThread.java:5621)
at java.lang.reflect.Method.invoke(Method.java)
at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:794)
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:684)
According to the log its happening in the oncreate of activity when trying to inflate the searchview. I am using 'android.widget.SearchView' instead of appcompat because I am animating it and can't get the appcompat version to animate properly.
I do not feel this is an animate issue though. I cannot figure out why its only happening on Huawei devices. I released this update last week and its already on 100k plus devices. According to Crashlytics its 100% Huawei and 81% Android 8, 15% Android 7 and 4% Android 6.
I have tried try/catches on the start activity, on create and few other places but nothing catches it. I can post code if needed but wanted to see if it was necessary.
Here is the searchview in the layout
<SearchView
android:id="#+id/search_view"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:alpha="0"
android:iconifiedByDefault="false"/>
And this is initializing the searchview in oncreate
SearchManager searchManager = (SearchManager) getSystemService(SEARCH_SERVICE);
if (searchManager != null) {
searchView.setSearchableInfo(searchManager.getSearchableInfo(getComponentName()));
}
Setting setSearchableInfo is calling updateSearchAutoComplete() which is where the crash starts. But i have tried a try/catch there an still crashes.
I want to make an Android project with Kotlin, so I build it with kotlin and Databinding. It's nothing wrong with it. But when I use Kotlin Android Extensions or Butterknife, the app I built from this project can not be used normally.
Here is my fragment class
class MainFragment: Fragment() {
private var stock: StockQuote? = null
override fun onCreateView(inflater: LayoutInflater?, container: ViewGroup?, savedInstanceState: Bundle?): View {
val binding: FragmentMainBinding = DataBindingUtil.inflate(inflater!!, R.layout.fragment_main, container, false)
binding.stock = stock
search_et.setOnFocusChangeListener { v, hasFocus ->
if(!hasFocus){
val newStock = ScrapingUtil.getQuote(search_et?.text.toString())
if(newStock != null){
stock?.name = newStock.name
}
}
}
return binding.root
}
}
I tried to use Butterknife at first but it didn't work, the View I want to bind would be null. Then I tried to use Kotlin Android Extensions, it threw an error at runtime
E/AndroidRuntime: FATAL EXCEPTION: main
Process: cn.mister.stockcalculator, PID: 30010
java.lang.RuntimeException: Unable to start activity ComponentInfo{cn.mister.stockcalculator/cn.mister.stockcalculator.MainActivity}: android.view.InflateException: Binary XML file line #10: Binary XML file line #10: Error inflating class fragment
at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2736)
at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2807)
at android.app.ActivityThread.-wrap11(ActivityThread.java)
at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1604)
at android.os.Handler.dispatchMessage(Handler.java:111)
at android.os.Looper.loop(Looper.java:191)
at android.app.ActivityThread.main(ActivityThread.java:6016)
at java.lang.reflect.Method.invoke(Native Method)
at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:960)
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:821)
Caused by: android.view.InflateException: Binary XML file line #10: Binary XML file line #10: Error inflating class fragment
at android.view.LayoutInflater.inflate(LayoutInflater.java:539)
at android.view.LayoutInflater.inflate(LayoutInflater.java:423)
at android.view.LayoutInflater.inflate(LayoutInflater.java:374)
at android.support.v7.app.AppCompatDelegateImplV9.setContentView(AppCompatDelegateImplV9.java:287)
at android.support.v7.app.AppCompatActivity.setContentView(AppCompatActivity.java:139)
at cn.mister.stockcalculator.MainActivity.onCreate(MainActivity.kt:10)
at android.app.Activity.performCreate(Activity.java:6370)
at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1122)
at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2689)
at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2807)
at android.app.ActivityThread.-wrap11(ActivityThread.java)
at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1604)
at android.os.Handler.dispatchMessage(Handler.java:111)
at android.os.Looper.loop(Looper.java:191)
at android.app.ActivityThread.main(ActivityThread.java:6016)
at java.lang.reflect.Method.invoke(Native Method)
at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:960)
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:821)
Caused by: android.view.InflateException: Binary XML file line #10: Error inflating class fragment
at android.view.LayoutInflater.createViewFromTag(LayoutInflater.java:782)
at android.view.LayoutInflater.createViewFromTag(LayoutInflater.java:704)
at android.view.LayoutInflater.rInflate(LayoutInflater.java:835)
at android.view.LayoutInflater.rInflateChildren(LayoutInflater.java:798)
at android.view.LayoutInflater.inflate(LayoutInflater.java:515)
at android.view.LayoutInflater.inflate(LayoutInflater.java:423)
at android.view.LayoutInflater.inflate(LayoutInflater.java:374)
at android.support.v7.app.AppCompatDelegateImplV9.setContentView(AppCompatDelegateImplV9.java:287)
at android.support.v7.app.AppCompatActivity.setContentView(AppCompatActivity.java:139)
at cn.mister.stockcalculator.MainActivity.onCreate(MainActivity.kt:10)
at android.app.Activity.performCreate(Activity.java:6370)
at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1122)
at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2689)
at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2807)
at android.app.ActivityThread.-wrap11(ActivityThread.java)
at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1604)
at android.os.Handler.dispatchMessage(Handler.java:111)
at android.os.Looper.loop(Looper.java:191)
at android.app.ActivityThread.main(ActivityThread.java:6016)
at java.lang.reflect.Method.invoke(Native Method)
at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:960)
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:821)
Caused by: java.lang.NullPointerException: Attempt to invoke virtual method 'void android.widget.EditText.setOnFocusChangeListener(android.view.View$OnFocusChangeListener)' on a null object reference
at cn.mister.stockcalculator.MainFragment.onCreateView(MainFragment.kt:23)
at android.app.Fragment.performCreateView(Fragment.java:2236)
at android.app.FragmentManagerImpl.moveToState(FragmentManager.java:962)
at android.app.FragmentManagerImpl.moveToState(FragmentManager.java:1139)
at android.app.FragmentManagerImpl.addFragment(FragmentManager.java:1241)
at android.app.FragmentManagerImpl.onCreateView(FragmentManager.java:2236)
at android.app.FragmentController.onCreateView(FragmentController.java:98)
at android.app.Activity.onCreateView(Activity.java:5678)
at android.support.v4.app.BaseFragmentActivityApi14.onCreateView(BaseFragmentActivityApi14.java:41)
at android.support.v4.app.FragmentActivity.onCreateView(FragmentActivity.java:68)
at android.view.LayoutInflater.createViewFromTag(LayoutInflater.java:754)
how can I fix it?
I found the answer at Kotlin Android Extensions and Fragments.
DataBinding doesn't matter, you should directly access search_et in the onViewCreated method
I have a preference fragment with the following files
preference.xml
<?xml version="1.0" encoding="utf-8"?>
<PreferenceScreen xmlns:android="http://schemas.android.com/apk/res/android">
<EditTextPreference
android:defaultValue="0000"
android:key="#string/pin"
android:summary="Set Pin for Notes"
android:title="Password" />
</PreferenceScreen>
Preference Activity
public class PreferenceActivity extends AppCompatActivity {
#BindView(R.id.toolbar_settings)
Toolbar toolbarSettings;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_settings);
ButterKnife.bind(this);
setSupportActionBar(toolbarSettings);
ActionBar actionBar = getSupportActionBar();
if (actionBar != null) {
actionBar.setDisplayHomeAsUpEnabled(true);
}
}
}
and finally
Preference Fragment
public class PreferenceFragment extends PreferenceFragmentCompat {
#Override
public void onCreatePreferences(Bundle savedInstanceState, String rootKey) {
addPreferencesFromResource(R.xml.preference);
}
}
But here when the addPreferencesFromResources is called, I am getting a runtime exception.
Here is the log stack
Exception
02-21 22:23:36.574 8813-8813/in.snotes.snotes E/AndroidRuntime: FATAL EXCEPTION: main
Process: in.snotes.snotes, PID: 8813
java.lang.RuntimeException: Unable to start activity ComponentInfo{in.snotes.snotes/in.snotes.snotes.settings.PreferenceActivity}: android.view.InflateException: Binary XML file line #0: Binary XML file line #0: Error inflating class fragment
at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2659)
at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2724)
at android.app.ActivityThread.-wrap12(ActivityThread.java)
at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1473)
at android.os.Handler.dispatchMessage(Handler.java:102)
at android.os.Looper.loop(Looper.java:154)
at android.app.ActivityThread.main(ActivityThread.java:6123)
at java.lang.reflect.Method.invoke(Native Method)
at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:867)
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:757)
Caused by: android.view.InflateException: Binary XML file line #0: Binary XML file line #0: Error inflating class fragment
Caused by: android.view.InflateException: Binary XML file line #0: Error inflating class fragment
Caused by: java.lang.ClassCastException: java.lang.Integer cannot be cast to java.lang.String
at android.app.SharedPreferencesImpl.getString(SharedPreferencesImpl.java:225)
at android.support.v7.preference.Preference.getPersistedString(Preference.java:1601)
at android.support.v7.preference.EditTextPreference.onSetInitialValue(EditTextPreference.java:92)
at android.support.v7.preference.Preference.dispatchSetInitialValue(Preference.java:1517)
at android.support.v7.preference.Preference.onAttachedToHierarchy(Preference.java:1245)
at android.support.v7.preference.Preference.onAttachedToHierarchy(Preference.java:1257)
at android.support.v7.preference.PreferenceGroup.addPreference(PreferenceGroup.java:194)
at android.support.v7.preference.PreferenceGroup.addItemFromInflater(PreferenceGroup.java:126)
at android.support.v7.preference.PreferenceInflater.rInflate(PreferenceInflater.java:355)
at android.support.v7.preference.PreferenceInflater.inflate(PreferenceInflater.java:162)
at android.support.v7.preference.PreferenceInflater.inflate(PreferenceInflater.java:112)
at android.support.v7.preference.PreferenceManager.inflateFromResource(PreferenceManager.java:138)
at android.support.v7.preference.PreferenceFragmentCompat.addPreferencesFromResource(PreferenceFragmentCompat.java:432)
at in.snotes.snotes.settings.PreferenceFragment.onCreatePreferences(PreferenceFragment.java:12)
at android.support.v7.preference.PreferenceFragmentCompat.onCreate(PreferenceFragmentCompat.java:224)
at android.support.v4.app.Fragment.performCreate(Fragment.java:2246)
at android.support.v4.app.FragmentManagerImpl.moveToState(FragmentManager.java:1377)
at android.support.v4.app.FragmentManagerImpl.moveToState(FragmentManager.java:1650)
at android.support.v4.app.FragmentManagerImpl.addFragment(FragmentManager.java:1906)
at android.support.v4.app.FragmentManagerImpl.onCreateView(FragmentManager.java:3698)
at android.support.v4.app.FragmentController.onCreateView(FragmentController.java:111)
at android.support.v4.app.FragmentActivity.dispatchFragmentsOnCreateView(FragmentActivity.java:350)
at android.support.v4.app.BaseFragmentActivityApi14.onCreateView(BaseFragmentActivityApi14.java:39)
at android.support.v4.app.FragmentActivity.onCreateView(FragmentActivity.java:67)
at android.view.LayoutInflater.createViewFromTag(LayoutInflater.java:777)
at android.view.LayoutInflater.createViewFromTag(LayoutInflater.java:727)
at android.view.LayoutInflater.rInflate(LayoutInflater.java:858)
at android.view.LayoutInflater.rInflateChildren(LayoutInflater.java:821)
at android.view.LayoutInflater.inflate(LayoutInflater.java:518)
at android.view.LayoutInflater.inflate(LayoutInflater.java:426)
at android.view.LayoutInflater.inflate(LayoutInflater.java:377)
02-21 22:23:36.575 8813-8813/in.snotes.snotes E/AndroidRuntime: at android.support.v7.app.AppCompatDelegateImplV9.setContentView(AppCompatDelegateImplV9.java:287)
at android.support.v7.app.AppCompatActivity.setContentView(AppCompatActivity.java:139)
at in.snotes.snotes.settings.PreferenceActivity.onCreate(PreferenceActivity.java:21)
at android.app.Activity.performCreate(Activity.java:6672)
at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1140)
at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2612)
at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2724)
at android.app.ActivityThread.-wrap12(ActivityThread.java)
at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1473)
at android.os.Handler.dispatchMessage(Handler.java:102)
at android.os.Looper.loop(Looper.java:154)
at android.app.ActivityThread.main(ActivityThread.java:6123)
at java.lang.reflect.Method.invoke(Native Method)
at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:867)
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:757)
I had a switch preference before the edit text preference and that worked fine. Once i replaced the switch preference with edit text preference, this error is thrown.
How do I fix this?
Can you try the code below?
strings.xml
<string name="pin_default_value">0000</string>
preference.xml
<EditTextPreference
android:defaultValue="#string/pin_default_value"
android:key="#string/pin"
android:summary="Set Pin for Notes"
android:title="Password" />
Android is trying to set default value but it is reading 0000 as an Integer and not as a String Main reason is that android:defaultValue is not directly handled as String. This way, to avoid this int to Stringconversion error, you must explicit use a String value (by using some string from strings.xml.
Note
You may need to clear your app data since that wrong default value may be stored in cached files already.