Возникновение исключений в представлении навигации с использованием RecyclerView, Fragment и Activity с Kotlin
У меня есть программа, которая будет показывать некоторые данные во фрагмент с представлением реселлера и использовать для данных файл 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>
я ищу другую проблему, которая похожа на меня, и их ответ - поставить андроид: имя под идентификатором, и я уже пробовал это, и все еще ничего не исправлено
это мой фрагмент класса
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>
Я пытался найти эту проблему, но нет вопроса, как моя проблема. Я надеюсь, что все могут мне помочь.
Благодарю вас