Warning: file_get_contents(/data/phpspider/zhask/data//catemap/3/android/221.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
将类片段android kotlin膨胀的错误_Android_Android Layout_Android Fragments_Kotlin_Inflate Exception - Fatal编程技术网

将类片段android kotlin膨胀的错误

将类片段android kotlin膨胀的错误,android,android-layout,android-fragments,kotlin,inflate-exception,Android,Android Layout,Android Fragments,Kotlin,Inflate Exception,所以,首先我回答你的问题“你真的是吗?有很多答案。”。是的,我知道,但我真的试着像第三天一样解决这个问题:在所有的软件中,我的错误是毫无疑问的。我有两个类似的项目:Java和Kotlin。但这是百分之五十@仅在Kotlin中显示错误。但是XML是100%的 所以,错误是:android.view.InflateException:Binary-XML文件行#8:error-inflating class fragment 我的活动\u main.xml: <LinearLayout xml

所以,首先我回答你的问题“你真的是吗?有很多答案。”。是的,我知道,但我真的试着像第三天一样解决这个问题:在所有的软件中,我的错误是毫无疑问的。我有两个类似的项目:Java和Kotlin。但这是百分之五十@仅在Kotlin中显示错误。但是XML是100%的

所以,错误是:
android.view.InflateException:Binary-XML文件行#8:error-inflating class fragment

我的活动\u 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>
也许这是一个硬代码,但在Java中它工作得很好。 我想我不需要显示
TopFragment.kt
BottomFragment.kt
,因为我试图告诉你我在SOFs主题中找到的一切。但是如果你需要的话,我会给你看的。然而,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.access$800(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.access$800(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.access$800(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()
        }
    })
}
class TopFragment:Fragment(),SwipeRefreshLayout.OnRefreshListener{
私有变量outputCity:String=getString(R.String.output\u city)
私有变量outputClouds:String=getString(R.String.output\u clouds)
私有变量outputHumidity:String=getString(R.String.output\u)
私有变量输出压力:String=getString(R.String.output\u pressure)
私有变量outputWindspeed:String=getString(R.String.output\u windspeed)
私有变量outputMmhg:String=getString(R.String.output\u mmHg)
私有变量outputMs:String=getString(R.String.output\u ms)
私有变量日期:日期=日期()
私有变量日期格式:SimpleDataFormat=SimpleDataFormat(“E,dd.MM”)
override fun onCreateView(充气机:布局充气机,容器:ViewGroup?,savedInstanceState:Bundle):视图{
返回充气机。充气(右布局。顶部碎片,容器,假)
}
覆盖有趣的onStart(){
super.onStart()
swipeToRefresh.setOnRefreshListener(此)
swipeToRefresh.setColorSchemeColors(颜色.绿色,颜色.红色,颜色.蓝色,颜色.青色)
检查Internet连接(活动)
}
重写onRefresh(){
Handler().postDelayed({
getData(活动)
swipeToRefresh.setRefreshing(假)
}, 2500)
}
趣味检查Internet连接(c:上下文){
if(InternetConnection().isNetworkAvailable(活动)){
getData(活动)
}否则{
Toast.makeText(活动,“Internet连接错误”,Toast.LENGTH\u LONG.show())
}
}
有趣的getData(c:上下文){
val apiServiceWeather=inversionclient().getApiServiceWeather()
val call:call=apiServiceWeather.getMyJSON();
排队(对象:Callback{
覆盖fun onResponse(调用:调用?,响应:响应?){
val湿度:Int?=响应!!.body().getMainWeather().湿度
val压力:浮动?=response.body().getMainWeather()压力
val温度:浮动?=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((数学打印温度!!-273.15)*10.0)/10.0.toString()
+“°C”)
textViewCondition.setText(response.body().getWeather().get(0.description)
textViewClouds.setText(outputClouds+“”+clouds.toString()+“%”)
textViewPressure.setText(输出压力+“”+(数学圆整(压力!!/1.3332239))
.toString()+“”+outputMmhg)
textView湿度.setText(输出湿度+“”+湿度.toString()+“%”)
textViewWindspeed.setText(输出windspeed+“”+windspeed.toString()+outputMs)
}
覆盖失效时的乐趣(调用:调用?、t:可丢弃?){
Toast.makeText(活动“onResponse Error”,Toast.LENGTH\u LONG.show())
}
})
}
}

原因:java.lang.IllegalStateException:片段TopFragment{37d52abb}未附加到活动

在com.example.weatherapp_kotlin.fragments.TopFragment.(TopFragment.kt:24)

我希望这是私有变量的第一行,在这里调用
getString()
从参考资料中获取字符串

这些字段在对象构造期间初始化,但此时片段未附加到任何上下文
getString()
需要一个来自上下文的资源对象。
如果您自己创建一个实例(即调用它的构造函数),也会发生这种崩溃

将其包装为充气异常的原因是,这些片段是在解析xml布局时创建的。它找到创建并包含片段的指令,它这样做了,但失败了

可能的解决方案 1) 将对getString的调用包装在一个
lazy
委托中。这将推迟 首次访问属性时对的调用,因此您只需 确保只有在确定 碎片附在上面。例如,在onCreateView()中

2) 用
lateinit
标记变量,并在第一次初始化时初始化 依附于上下文。现在你必须确保你永远不会访问 否则,您将收到一封电子邮件
属性初始化异常

private lateinit var outputCity: String

override fun onAttach(context: Activity) {
    super.onAttach(context)
    outputCity = getString(R.string.output_city)
    ...
}

堆栈跟踪中的信息比第一行上的异常消息更多。请进一步查看。TopFragment类是否存在?是否启用了ProGuard?您使用的是android.app.Fragment还是v4 Fragment?@Miha_x64是的,ofc,TopFragment存在。令我羞愧的是,我对ProGuard一无所知。我使用的是
android.app.Fragment
@MikeM。我大约在第二天到第三天去找它。真的,所有堆栈都由meOK检查,那么根本原因是什么?每当充气过程中出现任何问题时,都会抛出
InflateException
。这本身并不能告诉你多少。
private var outputCity by lazy { getString(R.string.output_city) }
private lateinit var outputCity: String

override fun onAttach(context: Activity) {
    super.onAttach(context)
    outputCity = getString(R.string.output_city)
    ...
}