I'm following MabBox tutorial at https://docs.mapbox.com/help/tutorials/android-navigation-sdk/ to make a simple navigation fragment that use the basics elements of it.
I already know that MapBox require to instantiate before inflating the View because I made it before on another Fragment to test it out where the fragment is part of a TabLayout component, however, in this one, there's only one Activity that loads the Fragment where I use the SDK and in this case it throws the same error that I fixed previously because I put it afterinflating the layout.
The errore that appears is this one:
E/AndroidRuntime: FATAL EXCEPTION: main
Process: com.example.parkingclientapplication, PID: 14257
java.lang.RuntimeException: Unable to start activity ComponentInfo{com.example.parkingclientapplication/com.example.parkingclientapplication.activities.MapDirectionActivity}: android.view.InflateException: Binary XML file line #8: Binary XML file line #8: Error inflating class com.mapbox.mapboxsdk.maps.MapView
at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2817)
at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2892)
at android.app.ActivityThread.-wrap11(Unknown Source:0)
at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1593)
at android.os.Handler.dispatchMessage(Handler.java:105)
at android.os.Looper.loop(Looper.java:164)
at android.app.ActivityThread.main(ActivityThread.java:6541)
at java.lang.reflect.Method.invoke(Native Method)
at com.android.internal.os.Zygote$MethodAndArgsCaller.run(Zygote.java:240)
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:767)
Caused by: android.view.InflateException: Binary XML file line #8: Binary XML file line #8: Error inflating class com.mapbox.mapboxsdk.maps.MapView
Caused by: android.view.InflateException: Binary XML file line #8: Error inflating class com.mapbox.mapboxsdk.maps.MapView
Caused by: java.lang.reflect.InvocationTargetException
at java.lang.reflect.Constructor.newInstance0(Native Method)
at java.lang.reflect.Constructor.newInstance(Constructor.java:334)
at android.view.LayoutInflater.createView(LayoutInflater.java:647)
at android.view.LayoutInflater.createViewFromTag(LayoutInflater.java:790)
at android.view.LayoutInflater.createViewFromTag(LayoutInflater.java:730)
at android.view.LayoutInflater.rInflate(LayoutInflater.java:863)
at android.view.LayoutInflater.rInflateChildren(LayoutInflater.java:824)
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 com.example.parkingclientapplication.activities.MapDirectionActivity.onCreate(MapDirectionActivity.kt:15)
at android.app.Activity.performCreate(Activity.java:6975)
at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1213)
at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2770)
at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2892)
at android.app.ActivityThread.-wrap11(Unknown Source:0)
at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1593)
at android.os.Handler.dispatchMessage(Handler.java:105)
at android.os.Looper.loop(Looper.java:164)
at android.app.ActivityThread.main(ActivityThread.java:6541)
at java.lang.reflect.Method.invoke(Native Method)
at com.android.internal.os.Zygote$MethodAndArgsCaller.run(Zygote.java:240)
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:767)
Caused by: com.mapbox.mapboxsdk.exceptions.MapboxConfigurationException:
Using MapView requires calling Mapbox.getInstance(Context context, String accessToken) before inflating or creating the view. The access token parameter is required when using a Mapbox service.
Please see https://www.mapbox.com/help/create-api-access-token/ to learn how to create one.
More information in this guide https://www.mapbox.com/help/first-steps-android-sdk/#access-tokens.
at com.mapbox.mapboxsdk.Mapbox.validateMapbox(Mapbox.java:165)
at com.mapbox.mapboxsdk.Mapbox.getAccessToken(Mapbox.java:77)
at com.mapbox.mapboxsdk.storage.FileSource.<init>(FileSource.java:234)
at com.mapbox.mapboxsdk.storage.FileSource.getInstance(FileSource.java:68)
at com.mapbox.mapboxsdk.maps.renderer.MapRenderer.<init>(MapRenderer.java:34)
E/AndroidRuntime: at com.mapbox.mapboxsdk.maps.renderer.glsurfaceview.GLSurfaceViewMapRenderer.<init>(GLSurfaceViewMapRenderer.java:33)
at com.mapbox.mapboxsdk.maps.MapView$5.<init>(MapView.java:294)
at com.mapbox.mapboxsdk.maps.MapView.initialiseDrawingSurface(MapView.java:294)
at com.mapbox.mapboxsdk.maps.MapView.initialize(MapView.java:137)
at com.mapbox.mapboxsdk.maps.MapView.<init>(MapView.java:100)
... 25 more
The code for the Activity is:
class MapDirectionActivity : AppCompatActivity() {
override fun onCreate(savedInstanceState: Bundle?) {
super.onCreate(savedInstanceState)
setContentView(R.layout.activity_map_direction)
supportFragmentManager.inTransaction {
add(R.id.form_clientNavMenu, MapDirectionFragment())
}
//Logout action on custom toolbar layout
logout.setOnClickListener {
val intent = Intent(this, MainActivity::class.java)
startActivity(intent)
}
}
}
The code for the onCreateView method of the Fragment is:
override fun onCreateView(
inflater: LayoutInflater, container: ViewGroup?,
savedInstanceState: Bundle?
): View? {
Mapbox.getInstance(context!!, getString(R.string.access_token))
val view = inflater.inflate(R.layout.fragment_map_direction, container, false)
mapView = view.findViewById(R.id.mapView)
mapView.onCreate(savedInstanceState)
mapView.getMapAsync(this)
// Inflate the layout for this fragment
return view
}
I only wrote onCreateView method because I think the error could be in there instead on the rest of the code which is the same as in the link, but I could be wrong.
Set the method:
Mapbox.getInstance(this,getString(R.string.access_token));
Before the:
setContentView(R.layout.activity_map);
If you having this exception change code structure as follows.
i.e:
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
Mapbox.getInstance(this,getString(R.string.access_token));
setContentView(R.layout.activity_map);
mapView = (MapView) findViewById(R.id.mapView);
mapView.onCreate(savedInstanceState);
mapView.getMapAsync(mapboxMap -> mapboxMap.setStyle(
new Style.Builder().fromUrl(Style.MAPBOX_STREETS)
));
}
Thanks #Tobrun for the answer, the problem was not that I missed the accessToken but I mixed activity and fragment layout so the components in one of them should be in the other. However, looking for the accessToken made me realize about other possible ones.
Related
I have a problem in Fragment Transaction.
I want to change the fragment that is showing when the button is clicked
but when I try to click it, the application shuts down.
It keeps showing the error message,
Binary XML file line #6: Error inflating, and
Unable to instantiate fragment make sure class name exists, is public, and has an empty constructor that is public
I tried to add the newInstace() code but it is still not working
Below is the error code and my Activity, Fragment code. I have no idea what is wrong...
Help me please.
E/AndroidRuntime: FATAL EXCEPTION: main
Process: com.example.illo, PID: 10709
android.view.InflateException: Binary XML file line #6: Binary XML file line #6: Error inflating class fragment
Caused by: android.view.InflateException: Binary XML file line #6: Error inflating class fragment
Caused by: android.app.Fragment$InstantiationException: Unable to instantiate fragment com.example.illo.WritePost1_Fragment: make sure class name exists, is public, and has an empty constructor that is public
at android.app.Fragment.instantiate(Fragment.java:538)
at android.app.FragmentContainer.instantiate(FragmentContainer.java:53)
at android.app.FragmentManagerImpl.onCreateView(FragmentManager.java:3547)
at android.app.FragmentController.onCreateView(FragmentController.java:102)
at android.app.Activity.onCreateView(Activity.java:6323)
at androidx.fragment.app.FragmentActivity.onCreateView(FragmentActivity.java:300)
at android.view.LayoutInflater.createViewFromTag(LayoutInflater.java:780)
at android.view.LayoutInflater.createViewFromTag(LayoutInflater.java:730)
at android.view.LayoutInflater.rInflate(LayoutInflater.java:863)
at android.view.LayoutInflater.rInflateChildren(LayoutInflater.java:824)
at android.view.LayoutInflater.inflate(LayoutInflater.java:515)
at android.view.LayoutInflater.inflate(LayoutInflater.java:423)
at com.example.illo.WritePost_Step2_Fragment.onCreateView(WritePost_Step2_Fragment.kt:27)
at androidx.fragment.app.Fragment.performCreateView(Fragment.java:2963)
at androidx.fragment.app.FragmentStateManager.createView(FragmentStateManager.java:518)
at androidx.fragment.app.FragmentStateManager.moveToExpectedState(FragmentStateManager.java:282)
at androidx.fragment.app.FragmentManager.executeOpsTogether(FragmentManager.java:2189)
at androidx.fragment.app.FragmentManager.removeRedundantOperationsAndExecute(FragmentManager.java:2100)
at androidx.fragment.app.FragmentManager.execPendingActions(FragmentManager.java:2002)
at androidx.fragment.app.FragmentManager$5.run(FragmentManager.java:524)
at android.os.Handler.handleCallback(Handler.java:873)
at android.os.Handler.dispatchMessage(Handler.java:99)
at android.os.Looper.loop(Looper.java:193)
at android.app.ActivityThread.main(ActivityThread.java:6669)
at java.lang.reflect.Method.invoke(Native Method)
at com.android.internal.os.RuntimeInit$MethodAndArgsCaller.run(RuntimeInit.java:493)
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:858)
Caused by: java.lang.ClassNotFoundException: Didn't find class "com.example.illo.WritePost1_Fragment" on path: DexPathList[[zip file "/data/app/com.example.illo-8XrFiUkhowzrFMYYQ7X5qw==/base.apk"],nativeLibraryDirectories=[/data/app/com.example.illo-8XrFiUkhowzrFMYYQ7X5qw==/lib/x86, /system/lib]]
at dalvik.system.BaseDexClassLoader.findClass(BaseDexClassLoader.java:134)
at java.lang.ClassLoader.loadClass(ClassLoader.java:379)
at java.lang.ClassLoader.loadClass(ClassLoader.java:312)
at android.app.Fragment.instantiate(Fragment.java:524)
at android.app.FragmentContainer.instantiate(FragmentContainer.java:53)
at android.app.FragmentManagerImpl.onCreateView(FragmentManager.java:3547)
at android.app.FragmentController.onCreateView(FragmentController.java:102)
at android.app.Activity.onCreateView(Activity.java:6323)
at androidx.fragment.app.FragmentActivity.onCreateView(FragmentActivity.java:300)
at android.view.LayoutInflater.createViewFromTag(LayoutInflater.java:780)
at android.view.LayoutInflater.createViewFromTag(LayoutInflater.java:730)
at android.view.LayoutInflater.rInflate(LayoutInflater.java:863)
at android.view.LayoutInflater.rInflateChildren(LayoutInflater.java:824)
at android.view.LayoutInflater.inflate(LayoutInflater.java:515)
at android.view.LayoutInflater.inflate(LayoutInflater.java:423)
at com.example.illo.WritePost_Step2_Fragment.onCreateView(WritePost_Step2_Fragment.kt:27)
at androidx.fragment.app.Fragment.performCreateView(Fragment.java:2963)
at androidx.fragment.app.FragmentStateManager.createView(FragmentStateManager.java:518)
at androidx.fragment.app.FragmentStateManager.moveToExpectedState(FragmentStateManager.java:282)
at androidx.fragment.app.FragmentManager.executeOpsTogether(FragmentManager.java:2189)
at androidx.fragment.app.FragmentManager.removeRedundantOperationsAndExecute(FragmentManager.java:2100)
at androidx.fragment.app.FragmentManager.execPendingActions(FragmentManager.java:2002)
at androidx.fragment.app.FragmentManager$5.run(FragmentManager.java:524)
at android.os.Handler.handleCallback(Handler.java:873)
at android.os.Handler.dispatchMessage(Handler.java:99)
at android.os.Looper.loop(Looper.java:193)
at android.app.ActivityThread.main(ActivityThread.java:6669)
at java.lang.reflect.Method.invoke(Native Method)
at com.android.internal.os.RuntimeInit$MethodAndArgsCaller.run(RuntimeInit.java:493)
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:858)
class WritePostActivity : AppCompatActivity() {
// step 프래그먼트 가져오기
lateinit var step1Fragment : WritePost_Step1_Fragment
lateinit var step2Fragment : WritePost_Step2_Fragment
override fun onCreate(savedInstanceState: Bundle?) {
super.onCreate(savedInstanceState)
setContentView(R.layout.activity_write_post)
// 툴바 설정
toolbarSetting()
// 처음에는 step1로 설정
ShowStep1()
// step 이동 버튼
btn_step1.setOnClickListener {
ShowStep1()
Toast.makeText(this#WritePostActivity, "Write Post Step1", Toast.LENGTH_SHORT).show()
}
btn_step2.setOnClickListener {
ShowStep2()
Toast.makeText(this#WritePostActivity, "Write Post Step2", Toast.LENGTH_SHORT).show()
}
// 글 쓰기 완료 버튼! (누르면 게시글이 등록됨)
btn_write_post_ok.setOnClickListener {
// *----서버/DB 필요----*
// 포스트로 등록
}
}
// 툴바 세팅
fun toolbarSetting() {
// 툴바로 세팅
setSupportActionBar(toolbar_write_post)
supportActionBar?.setDisplayShowCustomEnabled(true)
supportActionBar?.setDisplayShowTitleEnabled(false)
supportActionBar?.setHomeAsUpIndicator(R.drawable.icon_to_left); // 뒤로 가기 버튼 커스텀
supportActionBar?.setDisplayHomeAsUpEnabled(true) // 뒤로 가기 버튼 자동 생성
// 뒤로가기 버튼 설정
toolbar_write_post.setNavigationOnClickListener {
finish() // 액티비티 끝내기
overridePendingTransition(R.anim.no_animation, R.anim.slide_out_left) // 애니메이션
}
}
fun ShowStep1(){
step1Fragment = WritePost_Step1_Fragment()
supportFragmentManager.beginTransaction()
.replace(R.id.fragment_write_post, step1Fragment)
.setTransition(FragmentTransaction.TRANSIT_FRAGMENT_OPEN)
.commit()
}
fun ShowStep2(){
step2Fragment = WritePost_Step2_Fragment()
supportFragmentManager.beginTransaction()
.replace(R.id.fragment_write_post, step2Fragment)
.setTransition(FragmentTransaction.TRANSIT_FRAGMENT_OPEN)
.commit()
}
}
class WritePost_Step2_Fragment : Fragment() {
companion object {
fun newInstance(foo: Int): WritePost_Step2_Fragment {
val args= Bundle()
args.putInt("foo", foo)
val fragment= WritePost_Step2_Fragment()
fragment.arguments= args
return fragment
}
}
override fun onCreateView(
inflater: LayoutInflater,
container: ViewGroup?,
savedInstanceState: Bundle?
): View? {
val view: View = inflater.inflate(R.layout.fragment_write_post_step2, container, false)
return view
}
// Activity의 OnCreate와 같은 역할을 하는 함수
override fun onViewCreated(view: View, savedInstanceState: Bundle?) {
Log.d("life_cycle", "F onViewCreated")
super.onViewCreated(view, savedInstanceState)
}
}
you have error in you xml file (line no. 6 specifically), check your xml file first it is located in res/layout directory
My application consists of a ViewPager that contains 9 Fragments of the same type. This Fragment takes a parameter in its constructor upon declaration as shown below.
NewsFragment.kt
class NewsFragment(section: NewsSection) : Fragment() {
//...
}
CategoryFragmentPagerAdapter.kt
class CategoryFragmentPagerAdapter(fm: FragmentManager)
: FragmentPagerAdapter(fm, BEHAVIOR_RESUME_ONLY_CURRENT_FRAGMENT) {
override fun getItem(position: Int): Fragment =
NewsFragment(NewsSection.getItem(position))
override fun getCount(): Int = NewsSection.values().size
override fun getPageTitle(position: Int): CharSequence =
NewsSection.getItem(position).sectionName
}
And this fragment contains some actions which should navigate the user to some other fragments.
I tried to implement the navigation using the Jetpack Navigation Component method but looks it gives an error because the navigation graph can't accept a fragment with a parameter.
Any ideas on how to implement the Navigation in such a situation?
Edit
So this is my FragmentContainerView in the activity's XML layout
<androidx.fragment.app.FragmentContainerView
android:id="#+id/fragment_container"
android:name="androidx.navigation.fragment.NavHostFragment"
android:layout_width="match_parent"
android:layout_height="match_parent"
app:defaultNavHost="true"
app:navGraph="#navigation/navigation" />
and this is how I implement the navController in the activity
override fun onCreate(savedInstanceState: Bundle?) {
super.onCreate(savedInstanceState)
setupDataBinding()
val navHostFragment = supportFragmentManager.findFragmentById(R.id.fragment_container) as NavHostFragment
val navController = navHostFragment.navController
}
I didn't add any more configuration, and this is the error that pops up when I run the app
java.lang.RuntimeException: Unable to start activity ComponentInfo{com.domain.news/com.domain.news.ui.activities.MainActivity}: android.view.InflateException: Binary XML file line #18 in com.domain.news:layout/activity_main: Binary XML file line #22 in com.domain.news:layout/content_main: Error inflating class androidx.fragment.app.FragmentContainerView
at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:3307)
at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:3446)
at android.app.servertransaction.LaunchActivityItem.execute(LaunchActivityItem.java:83)
at android.app.servertransaction.TransactionExecutor.executeCallbacks(TransactionExecutor.java:135)
at android.app.servertransaction.TransactionExecutor.execute(TransactionExecutor.java:95)
at android.app.ActivityThread$H.handleMessage(ActivityThread.java:2043)
at android.os.Handler.dispatchMessage(Handler.java:107)
at android.os.Looper.loop(Looper.java:224)
at android.app.ActivityThread.main(ActivityThread.java:7548)
at java.lang.reflect.Method.invoke(Native Method)
at com.android.internal.os.RuntimeInit$MethodAndArgsCaller.run(RuntimeInit.java:539)
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:950)
Caused by: android.view.InflateException: Binary XML file line #18 in com.domain.news:layout/activity_main: Binary XML file line #22 in com.domain.news:layout/content_main: Error inflating class androidx.fragment.app.FragmentContainerView
Caused by: android.view.InflateException: Binary XML file line #22 in com.domain.news:layout/content_main: Error inflating class androidx.fragment.app.FragmentContainerView
Caused by: androidx.fragment.app.Fragment$InstantiationException: Unable to instantiate fragment com.domain.news.ui.fragments.news.NewsFragment: could not find Fragment constructor
at androidx.fragment.app.Fragment.instantiate(Fragment.java:563)
at androidx.fragment.app.FragmentContainer.instantiate(FragmentContainer.java:57)
at androidx.fragment.app.FragmentManager$3.instantiate(FragmentManager.java:390)
at androidx.navigation.fragment.FragmentNavigator.instantiateFragment(FragmentNavigator.java:132)
at androidx.navigation.fragment.FragmentNavigator.navigate(FragmentNavigator.java:162)
at androidx.navigation.fragment.FragmentNavigator.navigate(FragmentNavigator.java:58)
at androidx.navigation.NavGraphNavigator.navigate(NavGraphNavigator.java:71)
at androidx.navigation.NavGraphNavigator.navigate(NavGraphNavigator.java:28)
at androidx.navigation.NavController.navigate(NavController.java:1049)
at androidx.navigation.NavController.onGraphCreated(NavController.java:633)
at androidx.navigation.NavController.setGraph(NavController.java:586)
at androidx.navigation.NavController.setGraph(NavController.java:551)
at androidx.navigation.NavController.setGraph(NavController.java:533)
at androidx.navigation.fragment.NavHostFragment.onCreate(NavHostFragment.java:238)
at androidx.fragment.app.Fragment.performCreate(Fragment.java:2684)
at androidx.fragment.app.FragmentStateManager.create(FragmentStateManager.java:280)
at androidx.fragment.app.FragmentManager.moveToState(FragmentManager.java:1175)
at androidx.fragment.app.FragmentManager.addAddedFragments(FragmentManager.java:2224)
at androidx.fragment.app.FragmentManager.executeOpsTogether(FragmentManager.java:1997)
at androidx.fragment.app.FragmentManager.removeRedundantOperationsAndExecute(FragmentManager.java:1953)
at androidx.fragment.app.FragmentManager.execSingleAction(FragmentManager.java:1818)
at androidx.fragment.app.BackStackRecord.commitNowAllowingStateLoss(BackStackRecord.java:303)
at androidx.fragment.app.FragmentContainerView.<init>(FragmentContainerView.java:166)
at androidx.fragment.app.FragmentLayoutInflaterFactory.onCreateView(FragmentLayoutInflaterFactory.java:51)
2020-09-19 20:39:22.012 16179-16179/com.domain.news E/AndroidRuntime: at androidx.fragment.app.FragmentController.onCreateView(FragmentController.java:135)
at androidx.fragment.app.FragmentActivity.dispatchFragmentsOnCreateView(FragmentActivity.java:356)
at androidx.fragment.app.FragmentActivity.onCreateView(FragmentActivity.java:335)
at android.view.LayoutInflater.tryCreateView(LayoutInflater.java:1073)
at android.view.LayoutInflater.createViewFromTag(LayoutInflater.java:1001)
at android.view.LayoutInflater.createViewFromTag(LayoutInflater.java:965)
at android.view.LayoutInflater.rInflate(LayoutInflater.java:1127)
at android.view.LayoutInflater.rInflateChildren(LayoutInflater.java:1088)
at android.view.LayoutInflater.rInflate(LayoutInflater.java:1130)
at android.view.LayoutInflater.rInflateChildren(LayoutInflater.java:1088)
at android.view.LayoutInflater.parseInclude(LayoutInflater.java:1267)
at android.view.LayoutInflater.rInflate(LayoutInflater.java:1123)
at android.view.LayoutInflater.rInflateChildren(LayoutInflater.java:1088)
at android.view.LayoutInflater.parseInclude(LayoutInflater.java:1267)
at android.view.LayoutInflater.rInflate(LayoutInflater.java:1123)
at android.view.LayoutInflater.rInflateChildren(LayoutInflater.java:1088)
at android.view.LayoutInflater.inflate(LayoutInflater.java:686)
at android.view.LayoutInflater.inflate(LayoutInflater.java:538)
at android.view.LayoutInflater.inflate(LayoutInflater.java:481)
at androidx.appcompat.app.AppCompatDelegateImpl.setContentView(AppCompatDelegateImpl.java:696)
at androidx.appcompat.app.AppCompatActivity.setContentView(AppCompatActivity.java:170)
at androidx.databinding.DataBindingUtil.setContentView(DataBindingUtil.java:303)
at androidx.databinding.DataBindingUtil.setContentView(DataBindingUtil.java:284)
at com.domain.news.utils.delegates.ActivityBindingProperty.createActivityBinding(Databinding.kt:20)
at com.domain.news.utils.delegates.ActivityBindingProperty.getValue(Databinding.kt:16)
at com.domain.news.ui.activities.MainActivity.getBinding(Unknown Source:10)
at com.domain.news.ui.activities.MainActivity.setupBinding(MainActivity.kt:42)
at com.domain.news.ui.activities.MainActivity.onCreate(MainActivity.kt:34)
at android.app.Activity.performCreate(Activity.java:7893)
at android.app.Activity.performCreate(Activity.java:7880)
at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1307)
at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:3282)
at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:3446)
at android.app.servertransaction.LaunchActivityItem.execute(LaunchActivityItem.java:83)
at android.app.servertransaction.TransactionExecutor.executeCallbacks(TransactionExecutor.java:135)
at android.app.servertransaction.TransactionExecutor.execute(TransactionExecutor.java:95)
at android.app.ActivityThread$H.handleMessage(ActivityThread.java:2043)
at android.os.Handler.dispatchMessage(Handler.java:107)
at android.os.Looper.loop(Looper.java:224)
at android.app.ActivityThread.main(ActivityThread.java:7548)
at java.lang.reflect.Method.invoke(Native Method)
at com.android.internal.os.RuntimeInit$MethodAndArgsCaller.run(RuntimeInit.java:539)
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:950)
Caused by: java.lang.NoSuchMethodException: com.domain.news.ui.fragments.news.NewsFragment.<init> []
at java.lang.Class.getConstructor0(Class.java:2332)
at java.lang.Class.getConstructor(Class.java:1728)
at androidx.fragment.app.Fragment.instantiate(Fragment.java:548)
... 66 more
And I can see from the last 3 lines that the reason is because of the fragment constructor
First, open your
app:navGraph="#navigation/navigation
The Error Clearly states that it cant inflate your XMl, (Maybe somewhere in line 18 of your #navigation/navigation XML file)
You need to pass the data in the fragment's argument bundle instead of in the constructor.
To do this NewsSection needs to implement Parcelable
In your navigation xml you can do something like:
<fragment
android:id="#+id/newsFragment"
android:name="com.example.NewsFragment"
android:label="ProductListerFragment"
tools:layout="#layout/fragment_news_secion">
<argument
android:name="newsSection"
app:argType="com.example.NewsSection" />
And in the fragment you're navigating from:
val newsSection = NewsSection(....)
val bundle = bundleOf("newsSection" to newsSection)
view.findNavController().navigate(R.id.newsAction, bundle)
And in NewsFragment:
val newsSection = arguments?.getParcelable("newsSection") as? NewsSection
See https://developer.android.com/guide/navigation/navigation-pass-data for more details.
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 am using a preference fragment, and I get a exception on my XML. It used to work, but I've tweaked something and now it consistently crashes when I press the 3 dots in the top right hand for the menu.
The actual error is:
java.lang.RuntimeException: Unable to start activity ComponentInfo{org.sherman.android.babynme/org.sherman.android.babynme.Activites.SettingsActivity}: android.view.InflateException: Binary XML file line #0: Binary XML file line #0: Error inflating class fragment
Here is my activity_settings.xml:
<?xml version="1.0" encoding="utf-8"?>
<fragment xmlns:android='http://schemas.android.com/apk/res/android'
android:layout_width="match_parent"
android:layout_height="match_parent"
android:id="#+id/activity_settings"
android:name="org.sherman.android.babynme.Fragments.SettingsFragment" />
my preferencesXML:
<?xml version="1.0" encoding="utf-8"?>
<PreferenceScreen xmlns:android="http://schemas.android.com/apk/res/android">
<EditTextPreference
android:defaultValue="Name"
android:key="baby_name"
android:title="Baby's Name" />
<EditTextPreference
android:defaultValue="01/01/01"
android:key="baby_dob"
android:title="Baby's Birthday" />
<EditTextPreference
android:numeric="integer"
android:defaultValue="21"
android:key="mum_age"
android:singleLine="true"
android:title="Your age" />
</PreferenceScreen>
My SettingsActivity.kt:
class SettingsActivity : AppCompatActivity(){
override fun onCreate(savedInstanceState: Bundle?) {
super.onCreate(savedInstanceState)
setContentView(R.layout.activity_settings)
val sharedPreferences: SharedPreferences = PreferenceManager.getDefaultSharedPreferences(this)
val babyName:String = sharedPreferences.getString("baby_name", "Baby")
title = babyName+" 'n Me"
val actionBar = this.supportActionBar
if(actionBar != null){
actionBar.setDisplayHomeAsUpEnabled(true)
}
}
override fun onOptionsItemSelected(item: MenuItem?): Boolean {
val id = item?.itemId
if(id == android.R.id.home){
NavUtils.navigateUpFromSameTask(this)
}
return super.onOptionsItemSelected(item)
}
}
And the code for the SettingsFragment:
class SettingsFragment: PreferenceFragmentCompat(), SharedPreferences.OnSharedPreferenceChangeListener {
val mContext = context
override fun onCreatePreferences(savedInstanceState: Bundle?, rootKey: String?) {
addPreferencesFromResource(R.xml.pref_baby)
val sharedPreferences: SharedPreferences = android.preference.PreferenceManager.getDefaultSharedPreferences(activity.applicationContext)
onSharedPreferenceChanged(sharedPreferences, "baby_name")
onSharedPreferenceChanged(sharedPreferences, "baby_dob")
onSharedPreferenceChanged(sharedPreferences, "mum_age")
}
override fun onResume() {
super.onResume()
preferenceScreen.sharedPreferences.registerOnSharedPreferenceChangeListener(this)
//activity.getSharedPreferences(context.packageName, Context.MODE_PRIVATE).registerOnSharedPreferenceChangeListener(this)
}
override fun onPause() {
super.onPause()
preferenceScreen.sharedPreferences.unregisterOnSharedPreferenceChangeListener(this)
//activity.getSharedPreferences(context.packageName,Context.MODE_PRIVATE).unregisterOnSharedPreferenceChangeListener(this)
}
override fun onSharedPreferenceChanged(p0: SharedPreferences?, key: String?) {
Log.d(DEBUG, "Ospc called")
val pref: Preference = findPreference(key)
val etp: EditTextPreference = pref as EditTextPreference
pref.setSummary(etp.text)
}
Here is the full error log:
--------- beginning of crash
02-12 19:44:06.931 4518-4518/org.sherman.android.babynme E/AndroidRuntime: FATAL EXCEPTION: main
Process: org.sherman.android.babynme, PID: 4518
java.lang.RuntimeException: Unable to start activity ComponentInfo{org.sherman.android.babynme/org.sherman.android.babynme.Activites.SettingsActivity}: android.view.InflateException: Binary XML file line #0: Binary XML file line #0: Error inflating class fragment
at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2817)
at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2892)
at android.app.ActivityThread.-wrap11(Unknown Source:0)
at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1593)
at android.os.Handler.dispatchMessage(Handler.java:105)
at android.os.Looper.loop(Looper.java:164)
at android.app.ActivityThread.main(ActivityThread.java:6541)
at java.lang.reflect.Method.invoke(Native Method)
at com.android.internal.os.Zygote$MethodAndArgsCaller.run(Zygote.java:240)
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:767)
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:255)
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 org.sherman.android.babynme.Fragments.SettingsFragment.onCreatePreferences(SettingsFragment.kt:20)
at android.support.v7.preference.PreferenceFragmentCompat.onCreate(PreferenceFragmentCompat.java:224)
at android.support.v4.app.Fragment.performCreate(Fragment.java:2339)
at android.support.v4.app.FragmentManagerImpl.moveToState(FragmentManager.java:1377)
at android.support.v4.app.FragmentManagerImpl.moveToState(FragmentManager.java:1640)
at android.support.v4.app.FragmentManagerImpl.addFragment(FragmentManager.java:1896)
at android.support.v4.app.FragmentManagerImpl.onCreateView(FragmentManager.java:3673)
at android.support.v4.app.FragmentController.onCreateView(FragmentController.java:111)
at android.support.v4.app.FragmentActivity.dispatchFragmentsOnCreateView(FragmentActivity.java:338)
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:780)
at android.view.LayoutInflater.createViewFromTag(LayoutInflater.java:730)
at android.view.LayoutInflater.inflate(LayoutInflater.java:492)
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)
02-12 19:44:06.933 4518-4518/org.sherman.android.babynme E/AndroidRuntime: at android.support.v7.app.AppCompatActivity.setContentView(AppCompatActivity.java:139)
at org.sherman.android.babynme.Activites.SettingsActivity.onCreate(SettingsActivity.kt:15)
at android.app.Activity.performCreate(Activity.java:6975)
at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1213)
at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2770)
at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2892)
at android.app.ActivityThread.-wrap11(Unknown Source:0)
at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1593)
at android.os.Handler.dispatchMessage(Handler.java:105)
at android.os.Looper.loop(Looper.java:164)
at android.app.ActivityThread.main(ActivityThread.java:6541)
at java.lang.reflect.Method.invoke(Native Method)
at com.android.internal.os.Zygote$MethodAndArgsCaller.run(Zygote.java:240)
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:767)
My Question is what did I change that caused the menu selection to crash now? (Thank You).
The problem is Caused by: java.lang.ClassCastException: java.lang.Integer cannot be cast to java.lang.String
You must have changed the data types somewhere.
Whenever I assign an android:onClick attribute to an AutoCompleteTextView, my application immediately crashes upon loading the activity. I've tested AutoCompleteTextView without assigning an onClick method and it seems to work fine, as does assigning onClick to other views (including EditText, Spinners, TextViews, Buttons and ImageButtons). I've managed to narrow down the error to specifically to line 24 (setContentView(R.layout.activity_main)). Can anyone shed some light on this?
MainActivity.java
package com.example.matt.testapp;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.view.View;
import android.widget.ArrayAdapter;
import android.widget.AutoCompleteTextView;
public class MainActivity extends AppCompatActivity {
public AutoCompleteTextView actv;
public String[] continents = {"North America",
"South America",
"Antarctica",
"Europe",
"Africa",
"Asia",
"Australia"
};
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
actv = (AutoCompleteTextView) findViewById(R.id.actv);
ArrayAdapter<String> adapter = new ArrayAdapter<>(this, android.R.layout.simple_list_item_1, continents);
actv.setAdapter(adapter);
}
public void actvClick (View view) {}
}
activity_main.xml
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout
xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_height="match_parent"
android:layout_width="match_parent" >
<AutoCompleteTextView
android:id="#+id/actv"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:onClick="actvClick" >
</AutoCompleteTextView>
</LinearLayout>
Logcat
java.lang.RuntimeException: Unable to start activity ComponentInfo{com.example.matt.testapp/com.example.matt.testapp.MainActivity}: android.view.InflateException: Binary XML file line #7: Binary XML file line #7: Error inflating class AutoCompleteTextView
at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2817)
at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2892)
at android.app.ActivityThread.-wrap11(Unknown Source:0)
at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1593)
at android.os.Handler.dispatchMessage(Handler.java:105)
at android.os.Looper.loop(Looper.java:164)
at android.app.ActivityThread.main(ActivityThread.java:6541)
at java.lang.reflect.Method.invoke(Native Method)
at com.android.internal.os.Zygote$MethodAndArgsCaller.run(Zygote.java:240)
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:767)
Caused by: android.view.InflateException: Binary XML file line #7: Binary XML file line #7: Error inflating class AutoCompleteTextView
Caused by: android.view.InflateException: Binary XML file line #7: Error inflating class AutoCompleteTextView
Caused by: java.lang.NullPointerException: Attempt to write to field 'android.view.View$OnClickListener android.widget.AutoCompleteTextView$PassThroughClickListener.mWrapped' on a null object reference
at android.widget.AutoCompleteTextView$PassThroughClickListener.-set0(Unknown Source:0)
at android.widget.AutoCompleteTextView.setOnClickListener(AutoCompleteTextView.java:307)
at android.view.View.<init>(View.java:5008)
at android.widget.TextView.<init>(TextView.java:824)
at android.widget.EditText.<init>(EditText.java:88)
at android.widget.AutoCompleteTextView.<init>(AutoCompleteTextView.java:220)
at android.widget.AutoCompleteTextView.<init>(AutoCompleteTextView.java:193)
at android.widget.AutoCompleteTextView.<init>(AutoCompleteTextView.java:172)
at android.support.v7.widget.AppCompatAutoCompleteTextView.<init>(AppCompatAutoCompleteTextView.java:68)
at android.support.v7.widget.AppCompatAutoCompleteTextView.<init>(AppCompatAutoCompleteTextView.java:64)
at android.support.v7.app.AppCompatViewInflater.createView(AppCompatViewInflater.java:130)
at android.support.v7.app.AppCompatDelegateImplV9.createView(AppCompatDelegateImplV9.java:1026)
at android.support.v7.app.AppCompatDelegateImplV9.onCreateView(AppCompatDelegateImplV9.java:1083)
at android.view.LayoutInflater.createViewFromTag(LayoutInflater.java:772)
at android.view.LayoutInflater.createViewFromTag(LayoutInflater.java:730)
at android.view.LayoutInflater.rInflate(LayoutInflater.java:863)
at android.view.LayoutInflater.rInflateChildren(LayoutInflater.java:824)
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:289)
at android.support.v7.app.AppCompatActivity.setContentView(AppCompatActivity.java:139)
at com.example.matt.testapp.MainActivity.onCreate(MainActivity.java:24)
at android.app.Activity.performCreate(Activity.java:6975)
at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1213)
at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2770)
at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2892)
at android.app.ActivityThread.-wrap11(Unknown Source:0)
at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1593)
at android.os.Handler.dispatchMessage(Handler.java:105)
at android.os.Looper.loop(Looper.java:164)
at android.app.ActivityThread.main(ActivityThread.java:6541)
at java.lang.reflect.Method.invoke(Native Method)
at com.android.internal.os.Zygote$MethodAndArgsCaller.run(Zygote.java:240)
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:767)
This looks like to be a bug in AutoCompleteTextView.
What happens:
Inflating the XML invokes AutoCompleteTextView constructor, and it invokes View constructor.
View constructor sees the onClick attribute and calls setOnClickListener().
AutoCompleteTextView has overridden setOnClickListener() which writes to an internal mPassThroughClickListener field. It is only initialized at the end of AutoCompleteTextView constructor, and this init code has not been run yet.
Reference
To work around it, remove the onClick attrtibute from your XML and call setOnClickListener() programmatically in your code.
I filed a bug about this in Android issue tracker.