我有一个程序,该程序将使用recyclerview将一些数据显示到片段中,并使用json文件作为数据,我之前曾尝试过此方法,并且可以正常工作,但是当我使用底部导航时,遇到此异常>
java.lang.RuntimeException: Unable to start activity ComponentInfo{smktelkom.student.profil/smktelkom.student.profil.MainActivity}: android.view.InflateException: Binary XML file line #10: Error inflating class fragment
at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2325)
at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2387)
at android.app.ActivityThread.access$800(ActivityThread.java:151)
at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1303)
at android.os.Handler.dispatchMessage(Handler.java:102)
at android.os.Looper.loop(Looper.java:135)
at android.app.ActivityThread.main(ActivityThread.java:5254)
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:905)
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:700)
Caused by: android.view.InflateException: Binary XML file line #10: 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 android.support.v7.app.AppCompatDelegateImplV9.setContentView(AppCompatDelegateImplV9.java:287)
at android.support.v7.app.AppCompatActivity.setContentView(AppCompatActivity.java:139)
at smktelkom.student.profil.MainActivity.onCreate(MainActivity.kt:13)
at android.app.Activity.performCreate(Activity.java:5990)
at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1106)
at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2278)
at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2387)
at android.app.ActivityThread.access$800(ActivityThread.java:151)
at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1303)
at android.os.Handler.dispatchMessage(Handler.java:102)
at android.os.Looper.loop(Looper.java:135)
at android.app.ActivityThread.main(ActivityThread.java:5254)
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:905)
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:700)
Caused by: java.lang.RuntimeException: Exception inflating smktelkom.student.profil:navigation/navigasinya line 41
at androidx.navigation.NavInflater.inflate(NavInflater.java:131)
at androidx.navigation.NavController.setGraph(NavController.java:419)
at androidx.navigation.fragment.NavHostFragment.onCreate(NavHostFragment.java:221)
at android.support.v4.app.Fragment.performCreate(Fragment.java:2331)
at android.support.v4.app.FragmentManagerImpl.moveToState(FragmentManager.java:1386)
at android.support.v4.app.FragmentManagerImpl.moveToState(FragmentManager.java:1659)
at android.support.v4.app.FragmentManagerImpl.addFragment(FragmentManager.java:1905)
at android.support.v4.app.FragmentManagerImpl.onCreateView(FragmentManager.java:3715)
at android.support.v4.app.FragmentController.onCreateView(FragmentController.java:114)
at android.support.v4.app.FragmentActivity.dispatchFragmentsOnCreateView(FragmentActivity.java:374)
at android.support.v4.app.BaseFragmentActivityApi14.onCreateView(BaseFragmentActivityApi14.java:39)
at android.support.v4.app.FragmentActivity.onCreateView(FragmentActivity.java:68)
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 android.support.v7.app.AppCompatDelegateImplV9.setContentView(AppCompatDelegateImplV9.java:287)
at android.support.v7.app.AppCompatActivity.setContentView(AppCompatActivity.java:139)
at smktelkom.student.profil.MainActivity.onCreate(MainActivity.kt:13)
at android.app.Activity.performCreate(Activity.java:5990)
at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1106)
at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2278)
at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2387)
at android.app.ActivityThread.access$800(ActivityThread.java:151)
at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1303)
at android.os.Handler.dispatchMessage(Handler.java:102)
at android.os.Looper.loop(Looper.java:135)
at android.app.ActivityThread.main(ActivityThread.java:5254)
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:905)
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:700)
Caused by: java.lang.IllegalArgumentException: smktelkom.student.profil.More.MoreFragment must be a subclass of class android.support.v4.app.Fragment
at androidx.navigation.NavDestination.parseClassFromName(NavDestination.java:111)
at androidx.navigation.fragment.FragmentNavigator$Destination.onInflate(FragmentNavigator.java:310)
at androidx.navigation.NavInflater.inflate(NavInflater.java:143)
at androidx.navigation.NavInflater.inflate(NavInflater.java:172)
at androidx.navigation.NavInflater.inflate(NavInflater.java:122)
at androidx.navigation.NavController.setGraph(NavController.java:419)
at androidx.navigation.fragment.NavHostFragment.onCreate(NavHostFragment.java:221)
at android.support.v4.app.Fragment.performCreate(Fragment.java:2331)
at android.support.v4.app.FragmentManagerImpl.moveToState(FragmentManager.java:1386)
at android.support.v4.app.FragmentManagerImpl.moveToState(FragmentManager.java:1659)
at android.support.v4.app.FragmentManagerImpl.addFragment(FragmentManager.java:1905)
at android.support.v4.app.FragmentManagerImpl.onCreateView(FragmentManager.java:3715)
at android.support.v4.app.FragmentController.onCreateView(FragmentController.java:114)
at android.support.v4.app.FragmentActivity.dispatchFragmentsOnCreateView(FragmentActivity.java:374)
at android.support.v4.app.BaseFragmentActivityApi14.onCreateView(BaseFragmentActivityApi14.java:39)
at android.support.v4.app.FragmentActivity.onCreateView(FragmentActivity.java:68)
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 android.support.v7.app.AppCompatDelegateImplV9.setContentView(AppCompatDelegateImplV9.java:287)
at android.support.v7.app.AppCompatActivity.setContentView(AppCompatActivity.java:139)
at smktelkom.student.profil.MainActivity.onCreate(MainActivity.kt:13)
at android.app.Activity.performCreate(Activity.java:5990)
at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1106)
at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2278)
at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2387)
at android.app.ActivityThread.access$800(ActivityThread.java:151)
at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1303)
at android.os.Handler.dispatchMessage(Handler.java:102)
at android.os.Looper.loop(Looper.java:135)
at android.app.ActivityThread.main(ActivityThread.java:5254)
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:905)
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:700)
这是我的班级文件和导航文件,如果需要的话
package smktelkom.student.profil
import android.os.Bundle
import android.support.v7.app.AppCompatActivity
import androidx.navigation.Navigation
import androidx.navigation.ui.NavigationUI.onNavDestinationSelected
import kotlinx.android.synthetic.main.activity_main.*
class MainActivity : AppCompatActivity() {
override fun onCreate(savedInstanceState: Bundle?) {
super.onCreate(savedInstanceState)
setContentView(R.layout.activity_main)
// navigation_bottom.setOnNavigationItemSelectedListener(mOnNavigationItemSelectedListener)
navigation_bottom.setOnNavigationItemSelectedListener { item ->
onNavDestinationSelected(item, Navigation.findNavController(this, R.id.my_nav_host_fragment))
}
}
}
是的,MainActivity就是这样,异常链接到该类,我找不到问题
这是导航文件
<?xml version="1.0" encoding="utf-8"?>
<navigation xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
xmlns:tools="http://schemas.android.com/tools"
android:id="@+id/navigasinya"
app:startDestination="@id/navigation_profil">
<fragment
android:id="@+id/navigation_profil"
android:name="smktelkom.student.profil.Profil.ProfilFragment"
android:label="@string/title_profil"
tools:layout="@layout/fragment_profil">
<action
android:id="@+id/action_navigation_profil_to_main2Activity"
app:destination="@id/main2Activity"
app:enterAnim="@anim/nav_default_pop_enter_anim"
app:exitAnim="@anim/nav_default_exit_anim"
app:popEnterAnim="@anim/nav_default_pop_enter_anim"
app:popExitAnim="@anim/nav_default_pop_exit_anim" />
</fragment>
<fragment
android:id="@+id/navigation_hobi"
android:name="smktelkom.student.profil.Hobi.HobiFragment"
android:label="@string/title_hobi"
tools:layout="@layout/fragment_hobi">
<action
android:id="@+id/action_navigation_hobi_to_main3Activity"
app:destination="@id/main3Activity"
app:enterAnim="@anim/nav_default_pop_enter_anim" />
</fragment>
<fragment
android:id="@+id/navigation_makanan"
android:name="smktelkom.student.profil.Makanan.MakananFragment"
android:label="@string/title_makanan"
tools:layout="@layout/fragment_makanan">
<action
android:id="@+id/action_navigation_makanan_to_main4Activity"
app:destination="@id/main4Activity"
app:enterAnim="@anim/nav_default_pop_enter_anim" />
</fragment>
<fragment
android:id="@+id/navigation_more"
android:name="smktelkom.student.profil.More.MoreFragment"
android:label="@string/title_more"
tools:layout="@layout/fragment_more">
<action
android:id="@+id/action_navigation_more_to_main5Activity"
app:destination="@id/main5Activity"
app:enterAnim="@anim/nav_default_pop_enter_anim" />
</fragment>
<activity
android:id="@+id/main2Activity"
android:name="smktelkom.student.profil.Main2Activity"
android:label="activity_main2"
tools:layout="@layout/activity_main2" />
<activity
android:id="@+id/main5Activity"
android:name="smktelkom.student.profil.Main5Activity"
android:label="activity_main5"
tools:layout="@layout/activity_main5" />
<activity
android:id="@+id/main3Activity"
android:name="smktelkom.student.profil.Main3Activity"
android:label="activity_main3"
tools:layout="@layout/activity_main3" />
<activity
android:id="@+id/main4Activity"
android:name="smktelkom.student.profil.Main4Activity"
android:label="activity_main4"
tools:layout="@layout/activity_main4" />
</navigation>
我正在搜索另一个与我相同的问题,他们的答案是将android:name放在ID下方,而我已经尝试过了,但仍未解决
这是我的片段类
package smktelkom.student.profil.More
import android.annotation.TargetApi
import android.app.Fragment
import android.os.Build
import android.support.v7.app.AppCompatActivity
import android.os.Bundle
import android.support.v7.widget.LinearLayoutManager
import android.util.Log
import android.view.LayoutInflater
import android.view.View
import android.view.ViewGroup
import kotlinx.android.synthetic.main.activity_main.*
import kotlinx.android.synthetic.main.fragment_more.*
import org.json.JSONArray
import org.json.JSONObject
import smktelkom.student.profil.News
import smktelkom.student.profil.NewsAdapter
import smktelkom.student.profil.R
import java.io.InputStream
class MoreFragment : Fragment() {
private val TAG : String = "News"
private lateinit var linearLayoutManager:LinearLayoutManager
private lateinit var mAdapter: NewsAdapter
@TargetApi(Build.VERSION_CODES.M)
override fun onCreateView(inflater: LayoutInflater, container: ViewGroup?,
savedInstanceState: Bundle?): View? {
// Inflate the layout for this fragment
val view = inflater.inflate(R.layout.fragment_more, container, false)
recyclerView.layoutManager = LinearLayoutManager(context)
val jsonString:String = readJsonFromKotlinFile()
val newsList : ArrayList<News> = parseJsonStringToNewsList(jsonString)
mAdapter = NewsAdapter(newsList)
recyclerView.adapter = mAdapter
return view
}
private fun parseJsonStringToNewsList(jsonString: String): ArrayList<News> {
val newsList :ArrayList<News> = ArrayList<News>(0)
val newsArray = JSONArray(jsonString)
var i = 0
var numIterations = newsArray.length()
while(i < numIterations){
val newsObject:JSONObject = newsArray.getJSONObject(i)
val news = News()
news.news_title = newsObject.getString("news_title")
news.news_image_url = newsObject.getString("news_image_url")
news.news_source = newsObject.getString("news_source")
news.news_detail = newsObject.getString("news_detail")
news.news_url = newsObject.getString("news_url")
news.id = newsObject.getInt("id")
newsList.add(news)
i++
}
return newsList
}
private fun readJsonFromKotlinFile(): String {
var inputString = ""
try {
val inputStream:InputStream = activity.assets.open("news_data_file.json")
inputString = inputStream.bufferedReader().use{it.readText()}
Log.d(TAG,inputString)
} catch (e:Exception){
Log.d(TAG, e.toString())
}
return inputString
}
}
好吧,我认为此类没有问题,因为异常未提及它。万一您需要它
这是我的片段XML文件
<?xml version="1.0" encoding="utf-8"?>
<android.support.constraint.ConstraintLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:background="@color/colorKuning"
tools:context="smktelkom.student.profil.More.MoreFragment">
<android.support.v7.widget.RecyclerView
android:id="@+id/recyclerView"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:scrollbars="vertical"/>
</android.support.constraint.ConstraintLayout>
我已经尝试搜索此问题,但是没有像我的问题那样的问题。 我希望每个人都能帮助我。
谢谢