inflating class fragment android kotlin的错误
The error of inflating class fragment android kotlin
那么,我先回答你的问题"Are you really? There are lots of answers on it."。是的,我知道,但我真的像第三天一样尝试解决这个问题:在所有 SOF 中,我的错误都没有问题。我有 2 个类似的项目:在 Java 和 Kotlin 中。但是这个 f@#^%!@ing 错误只在 Kotlin 中。但是 XML 是身份 100%。
所以,错误是:android.view.InflateException: Binary XML file line #8: Error inflating class fragment
.
我的activity_main.xml:
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:id="@+id/layoutMain"
android:orientation="vertical">
<fragment
android:id="@+id/top_fragment"
android:name="com.example.weatherapp_kotlin.fragments.TopFragment"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:layout_weight="1">
</fragment>
<fragment
android:id="@+id/bottom_fragment"
android:name = "com.example.weatherapp_kotlin.fragments.BottomFragment"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:layout_weight="4">
</fragment>
</LinearLayout>
我尝试做的事情:
- 将 XML 中的
android:name =""
更改为 class = ""
- 要设置
android:name
或 class
在第一个位置
- 将
LinearLayout
更改为 RelativeLayout
和 ConstraintLayout
- 要将 class
extends
更改为 FragmentActivity
(现在是 Fragment
并且在
Java 工作正常)
- 清理项目
我的 LinearLayout
有一个 id
,一个 orientation
。 android:name
中 class 的路径为真。
我试着用这个 class:
class StartActivity: Activity() {
override fun onCreate(savedInstanceState: Bundle?) {
super.onCreate(savedInstanceState)
setContentView(R.layout.activity_main)
}}
也许这是一个硬编码,但在 Java 中它工作正常。
我想我不需要显示 TopFragment.kt
和 BottomFragment.kt
因为我试图告诉你我在 SOF 主题中发现的一切。但如果你需要 - 我会展示。但是,XML 文件中有一个错误,所以可能是 XML 的原因。
完整的堆栈跟踪:
06-14 15:24:10.161 25861-25861/com.example.weatherapp_kotlin E/AndroidRuntime: FATAL EXCEPTION: main
Process: com.example.weatherapp_kotlin, PID: 25861
java.lang.RuntimeException: Unable to start activity ComponentInfo{com.example.weatherapp_kotlin/com.example.weatherapp_kotlin.StartActivity}: android.view.InflateException: Binary XML file line #8: Error inflating class fragment
at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2472)
at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2534)
at android.app.ActivityThread.access0(ActivityThread.java:174)
at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1424)
at android.os.Handler.dispatchMessage(Handler.java:111)
at android.os.Looper.loop(Looper.java:194)
at android.app.ActivityThread.main(ActivityThread.java:5550)
at java.lang.reflect.Method.invoke(Native Method)
at java.lang.reflect.Method.invoke(Method.java:372)
at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:955)
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:750)
Caused by: android.view.InflateException: Binary XML file line #8: Error inflating class fragment
at android.view.LayoutInflater.createViewFromTag(LayoutInflater.java:763)
at android.view.LayoutInflater.rInflate(LayoutInflater.java:806)
at android.view.LayoutInflater.inflate(LayoutInflater.java:504)
at android.view.LayoutInflater.inflate(LayoutInflater.java:414)
at android.view.LayoutInflater.inflate(LayoutInflater.java:365)
at com.android.internal.policy.impl.PhoneWindow.setContentView(PhoneWindow.java:418)
at android.app.Activity.setContentView(Activity.java:2172)
at com.example.weatherapp_kotlin.StartActivity.onCreate(StartActivity.kt:9)
at android.app.Activity.performCreate(Activity.java:6003)
at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1129)
at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2425)
at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2534)
at android.app.ActivityThread.access0(ActivityThread.java:174)
at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1424)
at android.os.Handler.dispatchMessage(Handler.java:111)
at android.os.Looper.loop(Looper.java:194)
at android.app.ActivityThread.main(ActivityThread.java:5550)
at java.lang.reflect.Method.invoke(Native Method)
at java.lang.reflect.Method.invoke(Method.java:372)
at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:955)
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:750)
Caused by: java.lang.IllegalArgumentException: Parameter specified as non-null is null: method kotlin.jvm.internal.Intrinsics.checkParameterIsNotNull, parameter savedInstanceState
at com.example.weatherapp_kotlin.fragments.TopFragment.onCreateView(TopFragment.kt:0)
at android.app.Fragment.performCreateView(Fragment.java:2069)
at android.app.FragmentManagerImpl.moveToState(FragmentManager.java:875)
at android.app.FragmentManagerImpl.moveToState(FragmentManager.java:1050)
at android.app.FragmentManagerImpl.addFragment(FragmentManager.java:1152)
at android.app.FragmentManagerImpl.onCreateView(FragmentManager.java:2142)
at android.app.Activity.onCreateView(Activity.java:5347)
at android.view.LayoutInflater.createViewFromTag(LayoutInflater.java:733)
at android.view.LayoutInflater.rInflate(LayoutInflater.java:806)
at android.view.LayoutInflater.inflate(LayoutInflater.java:504)
at android.view.LayoutInflater.inflate(LayoutInflater.java:414)
at android.view.LayoutInflater.inflate(LayoutInflater.java:365)
at com.android.internal.policy.impl.PhoneWindow.setContentView(PhoneWindow.java:418)
at android.app.Activity.setContentView(Activity.java:2172)
at com.example.weatherapp_kotlin.StartActivity.onCreate(StartActivity.kt:9)
at android.app.Activity.performCreate(Activity.java:6003)
at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1129)
at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2425)
at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2534)
at android.app.ActivityThread.access0(ActivityThread.java:174)
at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1424)
at android.os.Handler.dispatchMessage(Handler.java:111)
at android.os.Looper.loop(Looper.java:194)
at android.app.ActivityThread.main(ActivityThread.java:5550)
at java.lang.reflect.Method.invoke(Native Method)
at java.lang.reflect.Method.invoke(Method.java:372)
at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:955)
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:750)
TopFragment.class:
class TopFragment : Fragment(), SwipeRefreshLayout.OnRefreshListener {
private var outputCity: String = getString(R.string.output_city)
private var outputClouds: String = getString(R.string.output_clouds)
private var outputHumidity: String = getString(R.string.output_humidity)
private var outputPressure: String = getString(R.string.output_pressure)
private var outputWindspeed: String = getString(R.string.output_windspeed)
private var outputMmhg: String = getString(R.string.output_mmHg)
private var outputMs: String = getString(R.string.output_ms)
private var date: Date = Date()
private var dateFormat: SimpleDateFormat = SimpleDateFormat("E, dd.MM")
override fun onCreateView(inflater: LayoutInflater, container: ViewGroup?, savedInstanceState: Bundle): View? {
return inflater.inflate(R.layout.fragment_top, container, false)
}
override fun onStart() {
super.onStart()
swipeToRefresh.setOnRefreshListener(this)
swipeToRefresh.setColorSchemeColors(Color.GREEN, Color.RED, Color.BLUE, Color.CYAN)
checkInternetConnection(activity)
}
override fun onRefresh() {
Handler().postDelayed({
getData(activity)
swipeToRefresh.setRefreshing(false)
}, 2500)
}
fun checkInternetConnection(c: Context) {
if (InternetConnection().isNetworkAvailable(activity)) {
getData(activity)
} else {
Toast.makeText(activity, "Internet Connection Error", Toast.LENGTH_LONG).show()
}
}
fun getData(c: Context) {
val apiServiceWeather = RetrofitClient().getApiServiceWeather()
val call: Call<WeatherModel> = apiServiceWeather.getMyJSON();
call.enqueue(object: Callback<WeatherModel> {
override fun onResponse(call: Call<WeatherModel>?, response: Response<WeatherModel>?) {
val humidity: Int? = response!!.body().getMainWeather().humidity
val pressure: Float? = response.body().getMainWeather().pressure
val temperature: Float? = response.body().getMainWeather().temp
val windspeed: Float? = response.body().getWind().windspeed
val clouds: Int? = response.body().getClouds().clouds
textViewDate.setText((dateFormat!!.format(date).toString()))
textViewCity.setText(outputCity)
textViewTemp.setText(((Math.rint(temperature!! - 273.15) * 10.0) / 10.0).toString()
+ "°C")
textViewCondition.setText(response.body().getWeather().get(0).description)
textViewClouds.setText(outputClouds + " " + clouds.toString() + "%")
textViewPressure.setText(outputPressure + " " + (Math.round(pressure!! / 1.3332239))
.toString() + " " + outputMmhg)
textViewHumidity.setText(outputHumidity + " " + humidity.toString() + "%")
textViewWindspeed.setText(outputWindspeed + " " + windspeed.toString() + outputMs)
}
override fun onFailure(call: Call<WeatherModel>?, t: Throwable?) {
Toast.makeText(activity, "onResponse Error", Toast.LENGTH_LONG).show()
}
})
}
}
Caused by: java.lang.IllegalStateException: Fragment TopFragment{37d52abb} not attached to Activity
...
at com.example.weatherapp_kotlin.fragments.TopFragment.(TopFragment.kt:24)
我希望那将是私有变量的第一行,您可以在其中调用 getString()
从资源中获取字符串。
这些字段是在对象构造期间初始化的,但此时片段未附加到任何上下文。 getString()
需要一个来自上下文的资源对象。
如果您自己创建一个实例(即调用它的构造函数),也会发生这种崩溃。
它包裹膨胀异常的原因是因为这些片段是在解析 xml 布局时创建的。它找到了创建和包含片段的指令,它这样做了但失败了。
可能的解决方案
1) 在 lazy
委托中包装对 getString 的调用。这会延迟
首次访问 属性 时的调用,因此您只需要
确保只有在确定
附上片段。例如,在 onCreateView() 内部。
private var outputCity by lazy { getString(R.string.output_city) }
2) 用lateinit
标记变量,第一次初始化
附加到上下文。现在你必须确保你永远不会访问
之前的属性,或者你会看到一个
PropertyNotInitialisedException
。
private lateinit var outputCity: String
override fun onAttach(context: Activity) {
super.onAttach(context)
outputCity = getString(R.string.output_city)
...
}
那么,我先回答你的问题"Are you really? There are lots of answers on it."。是的,我知道,但我真的像第三天一样尝试解决这个问题:在所有 SOF 中,我的错误都没有问题。我有 2 个类似的项目:在 Java 和 Kotlin 中。但是这个 f@#^%!@ing 错误只在 Kotlin 中。但是 XML 是身份 100%。
所以,错误是:android.view.InflateException: Binary XML file line #8: Error inflating class fragment
.
我的activity_main.xml:
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:id="@+id/layoutMain"
android:orientation="vertical">
<fragment
android:id="@+id/top_fragment"
android:name="com.example.weatherapp_kotlin.fragments.TopFragment"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:layout_weight="1">
</fragment>
<fragment
android:id="@+id/bottom_fragment"
android:name = "com.example.weatherapp_kotlin.fragments.BottomFragment"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:layout_weight="4">
</fragment>
</LinearLayout>
我尝试做的事情:
- 将 XML 中的
android:name =""
更改为class = ""
- 要设置
android:name
或class
在第一个位置 - 将
LinearLayout
更改为RelativeLayout
和ConstraintLayout
- 要将 class
extends
更改为FragmentActivity
(现在是Fragment
并且在 Java 工作正常) - 清理项目
我的 LinearLayout
有一个 id
,一个 orientation
。 android:name
中 class 的路径为真。
我试着用这个 class:
class StartActivity: Activity() {
override fun onCreate(savedInstanceState: Bundle?) {
super.onCreate(savedInstanceState)
setContentView(R.layout.activity_main)
}}
也许这是一个硬编码,但在 Java 中它工作正常。
我想我不需要显示 TopFragment.kt
和 BottomFragment.kt
因为我试图告诉你我在 SOF 主题中发现的一切。但如果你需要 - 我会展示。但是,XML 文件中有一个错误,所以可能是 XML 的原因。
完整的堆栈跟踪:
06-14 15:24:10.161 25861-25861/com.example.weatherapp_kotlin E/AndroidRuntime: FATAL EXCEPTION: main
Process: com.example.weatherapp_kotlin, PID: 25861
java.lang.RuntimeException: Unable to start activity ComponentInfo{com.example.weatherapp_kotlin/com.example.weatherapp_kotlin.StartActivity}: android.view.InflateException: Binary XML file line #8: Error inflating class fragment
at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2472)
at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2534)
at android.app.ActivityThread.access0(ActivityThread.java:174)
at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1424)
at android.os.Handler.dispatchMessage(Handler.java:111)
at android.os.Looper.loop(Looper.java:194)
at android.app.ActivityThread.main(ActivityThread.java:5550)
at java.lang.reflect.Method.invoke(Native Method)
at java.lang.reflect.Method.invoke(Method.java:372)
at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:955)
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:750)
Caused by: android.view.InflateException: Binary XML file line #8: Error inflating class fragment
at android.view.LayoutInflater.createViewFromTag(LayoutInflater.java:763)
at android.view.LayoutInflater.rInflate(LayoutInflater.java:806)
at android.view.LayoutInflater.inflate(LayoutInflater.java:504)
at android.view.LayoutInflater.inflate(LayoutInflater.java:414)
at android.view.LayoutInflater.inflate(LayoutInflater.java:365)
at com.android.internal.policy.impl.PhoneWindow.setContentView(PhoneWindow.java:418)
at android.app.Activity.setContentView(Activity.java:2172)
at com.example.weatherapp_kotlin.StartActivity.onCreate(StartActivity.kt:9)
at android.app.Activity.performCreate(Activity.java:6003)
at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1129)
at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2425)
at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2534)
at android.app.ActivityThread.access0(ActivityThread.java:174)
at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1424)
at android.os.Handler.dispatchMessage(Handler.java:111)
at android.os.Looper.loop(Looper.java:194)
at android.app.ActivityThread.main(ActivityThread.java:5550)
at java.lang.reflect.Method.invoke(Native Method)
at java.lang.reflect.Method.invoke(Method.java:372)
at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:955)
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:750)
Caused by: java.lang.IllegalArgumentException: Parameter specified as non-null is null: method kotlin.jvm.internal.Intrinsics.checkParameterIsNotNull, parameter savedInstanceState
at com.example.weatherapp_kotlin.fragments.TopFragment.onCreateView(TopFragment.kt:0)
at android.app.Fragment.performCreateView(Fragment.java:2069)
at android.app.FragmentManagerImpl.moveToState(FragmentManager.java:875)
at android.app.FragmentManagerImpl.moveToState(FragmentManager.java:1050)
at android.app.FragmentManagerImpl.addFragment(FragmentManager.java:1152)
at android.app.FragmentManagerImpl.onCreateView(FragmentManager.java:2142)
at android.app.Activity.onCreateView(Activity.java:5347)
at android.view.LayoutInflater.createViewFromTag(LayoutInflater.java:733)
at android.view.LayoutInflater.rInflate(LayoutInflater.java:806)
at android.view.LayoutInflater.inflate(LayoutInflater.java:504)
at android.view.LayoutInflater.inflate(LayoutInflater.java:414)
at android.view.LayoutInflater.inflate(LayoutInflater.java:365)
at com.android.internal.policy.impl.PhoneWindow.setContentView(PhoneWindow.java:418)
at android.app.Activity.setContentView(Activity.java:2172)
at com.example.weatherapp_kotlin.StartActivity.onCreate(StartActivity.kt:9)
at android.app.Activity.performCreate(Activity.java:6003)
at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1129)
at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2425)
at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2534)
at android.app.ActivityThread.access0(ActivityThread.java:174)
at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1424)
at android.os.Handler.dispatchMessage(Handler.java:111)
at android.os.Looper.loop(Looper.java:194)
at android.app.ActivityThread.main(ActivityThread.java:5550)
at java.lang.reflect.Method.invoke(Native Method)
at java.lang.reflect.Method.invoke(Method.java:372)
at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:955)
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:750)
TopFragment.class:
class TopFragment : Fragment(), SwipeRefreshLayout.OnRefreshListener {
private var outputCity: String = getString(R.string.output_city)
private var outputClouds: String = getString(R.string.output_clouds)
private var outputHumidity: String = getString(R.string.output_humidity)
private var outputPressure: String = getString(R.string.output_pressure)
private var outputWindspeed: String = getString(R.string.output_windspeed)
private var outputMmhg: String = getString(R.string.output_mmHg)
private var outputMs: String = getString(R.string.output_ms)
private var date: Date = Date()
private var dateFormat: SimpleDateFormat = SimpleDateFormat("E, dd.MM")
override fun onCreateView(inflater: LayoutInflater, container: ViewGroup?, savedInstanceState: Bundle): View? {
return inflater.inflate(R.layout.fragment_top, container, false)
}
override fun onStart() {
super.onStart()
swipeToRefresh.setOnRefreshListener(this)
swipeToRefresh.setColorSchemeColors(Color.GREEN, Color.RED, Color.BLUE, Color.CYAN)
checkInternetConnection(activity)
}
override fun onRefresh() {
Handler().postDelayed({
getData(activity)
swipeToRefresh.setRefreshing(false)
}, 2500)
}
fun checkInternetConnection(c: Context) {
if (InternetConnection().isNetworkAvailable(activity)) {
getData(activity)
} else {
Toast.makeText(activity, "Internet Connection Error", Toast.LENGTH_LONG).show()
}
}
fun getData(c: Context) {
val apiServiceWeather = RetrofitClient().getApiServiceWeather()
val call: Call<WeatherModel> = apiServiceWeather.getMyJSON();
call.enqueue(object: Callback<WeatherModel> {
override fun onResponse(call: Call<WeatherModel>?, response: Response<WeatherModel>?) {
val humidity: Int? = response!!.body().getMainWeather().humidity
val pressure: Float? = response.body().getMainWeather().pressure
val temperature: Float? = response.body().getMainWeather().temp
val windspeed: Float? = response.body().getWind().windspeed
val clouds: Int? = response.body().getClouds().clouds
textViewDate.setText((dateFormat!!.format(date).toString()))
textViewCity.setText(outputCity)
textViewTemp.setText(((Math.rint(temperature!! - 273.15) * 10.0) / 10.0).toString()
+ "°C")
textViewCondition.setText(response.body().getWeather().get(0).description)
textViewClouds.setText(outputClouds + " " + clouds.toString() + "%")
textViewPressure.setText(outputPressure + " " + (Math.round(pressure!! / 1.3332239))
.toString() + " " + outputMmhg)
textViewHumidity.setText(outputHumidity + " " + humidity.toString() + "%")
textViewWindspeed.setText(outputWindspeed + " " + windspeed.toString() + outputMs)
}
override fun onFailure(call: Call<WeatherModel>?, t: Throwable?) {
Toast.makeText(activity, "onResponse Error", Toast.LENGTH_LONG).show()
}
})
}
}
Caused by: java.lang.IllegalStateException: Fragment TopFragment{37d52abb} not attached to Activity
...
at com.example.weatherapp_kotlin.fragments.TopFragment.(TopFragment.kt:24)
我希望那将是私有变量的第一行,您可以在其中调用 getString()
从资源中获取字符串。
这些字段是在对象构造期间初始化的,但此时片段未附加到任何上下文。 getString()
需要一个来自上下文的资源对象。
如果您自己创建一个实例(即调用它的构造函数),也会发生这种崩溃。
它包裹膨胀异常的原因是因为这些片段是在解析 xml 布局时创建的。它找到了创建和包含片段的指令,它这样做了但失败了。
可能的解决方案
1) 在 lazy
委托中包装对 getString 的调用。这会延迟
首次访问 属性 时的调用,因此您只需要
确保只有在确定
附上片段。例如,在 onCreateView() 内部。
private var outputCity by lazy { getString(R.string.output_city) }
2) 用lateinit
标记变量,第一次初始化
附加到上下文。现在你必须确保你永远不会访问
之前的属性,或者你会看到一个
PropertyNotInitialisedException
。
private lateinit var outputCity: String
override fun onAttach(context: Activity) {
super.onAttach(context)
outputCity = getString(R.string.output_city)
...
}