Android Studio Beginner here - kotlin

I am taking the coursera course for mobile application development and I am doing my first project, my build launches successfully then immediately crashes. I am not sure why it is doing this so any insight into the matter would be greatly appreciated. Below is my build...
package com.example.video_player
import android.R
import android.net.Uri
import android.net.Uri.*
import android.os.Bundle
import android.widget.MediaController
import android.widget.VideoView
import androidx.appcompat.app.AppCompatActivity
class MainActivity : AppCompatActivity() {
override fun onCreate(savedInstanceState: Bundle?) {
super.onCreate(savedInstanceState)
setContentView(R.layout.activity_main)
val videoView = findViewById(R.id.testView)
val mediaController = MediaController(this)
mediaController.setAnchorView(videoView)
val uri:Uri = parse(
"android.resource://" + packageName
+ "/raw/test"
)
videoView.setMediaController(mediaController)
videoView.setVideoURI(uri)
videoView.requestFocus()
videoView.start()
}
}
<?xml version="1.0" encoding="utf-8"?>
<androidx.constraintlayout.widget.ConstraintLayout 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:layout_width="match_parent"
android:layout_height="match_parent"
tools:context="com.example.video_player.MainActivity">
<VideoView
android:id="#+id/testView"
android:layout_width="wrap_content"
android:layout_height="308dp"
app:layout_constraintBottom_toBottomOf="parent"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toTopOf="parent" />
</androidx.constraintlayout.widget.ConstraintLayout>
This is my first build and i am following a tutorial and am just getting error after error, when i get the errors to clear the app crashes directly after launch.

Related

Kotlin - Navigation Components - Navigation action/destination cannot be found from the current destination

ISSUE
java.lang.IllegalArgumentException: Navigation action/destination xxxx/action_scanFragment_to_addVehicleFragment cannot be found from the current destination xxxx/addVehicleFragment
The error occurs when I do
findNavController().navigate(R.id.action_scanFragment_to_addVehicleFragment)
in scanFragment. Which means that current destination is addVehicleFragment, but it should be scanFragment.
I am clueless on how to approach this. See my earlier question for some troubleshooting and what really goes on in scanFragment:
Kotlin - fragment lifecycle navigation issues; why does child fragment become current destination?
I suspect my navigation setup is wrong, but I can't find the solution anywhere.
I'm posting my entire navigation implementation/code below.
My nav_graph design:
My nav_graph XML:
<?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/nav_graph"
app:startDestination="#id/mainFragment">
<fragment
android:id="#+id/scanFragment"
android:name="xxxx.ui.scan.ScanFragment"
android:label="#string/tab_bar_first_item_title"
tools:layout="#layout/fragment_scan" >
<action
android:id="#+id/action_scanFragment_to_addVehicleFragment"
app:destination="#id/addVehicleFragment"
app:enterAnim="#anim/from_left"
app:exitAnim="#anim/to_left" />
</fragment>
<fragment
android:id="#+id/addVehicleFragment"
android:name="xxxx.ui.scan.AddVehicleFragment"
android:label="#string/add_vehicle_fragment_title_string"
tools:layout="#layout/fragment_add_vehicle">
<action
android:id="#+id/action_addVehicleFragment_to_scanFragment"
app:destination="#id/scanFragment" />
</fragment>
<fragment
android:id="#+id/mainFragment"
android:name="xxxx.ui.main.MainFragment"
android:label="#string/tab_bar_second_item_title"
tools:layout="#layout/fragment_main" />
<fragment
android:id="#+id/profileFragment"
android:name="xxxx.ui.profile.ProfileFragment"
android:label="#string/tab_bar_third_item_title"
tools:layout="#layout/fragment_profile">
<action
android:id="#+id/actionMyVehicles"
app:destination="#id/myVehiclesFragment"
app:enterAnim="#anim/from_right"
app:exitAnim="#anim/to_left" />
<action
android:id="#+id/actionMyRooms"
app:destination="#+id/myRoomsFragment"
app:enterAnim="#anim/from_right"
app:exitAnim="#anim/to_left" />
</fragment>
<fragment
android:id="#+id/myVehiclesFragment"
android:name="xxxx.ui.profile.MyVehiclesFragment"
android:label="fragment_my_vehicles"
tools:layout="#layout/fragment_my_vehicles" >
<action
android:id="#+id/action_myVehiclesFragment_to_profileFragment"
app:destination="#id/profileFragment"
app:enterAnim="#anim/from_left"
app:exitAnim="#anim/to_right" />
</fragment>
<fragment
android:id="#+id/myRoomsFragment"
android:name="xxxx.ui.profile.MyRoomsFragment"
android:label="fragment_my_rooms"
tools:layout="#layout/fragment_my_rooms" >
<action
android:id="#+id/action_myRoomsFragment_to_profileFragment"
app:destination="#id/profileFragment"
app:enterAnim="#anim/from_left"
app:exitAnim="#anim/to_right" />
</fragment>
</navigation>
My Main Activity XML:
<?xml version="1.0" encoding="utf-8"?>
<androidx.constraintlayout.widget.ConstraintLayout 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/main_activity_layout"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:background="#color/light_gray"
tools:context=".MainActivity">
<androidx.fragment.app.FragmentContainerView
android:id="#+id/navHostFragment"
android:name="androidx.navigation.fragment.NavHostFragment"
android:layout_width="0dp"
android:layout_height="0dp"
app:defaultNavHost="true"
app:layout_constraintBottom_toTopOf="#+id/tabBar"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toTopOf="parent"
app:navGraph="#navigation/nav_graph">
</androidx.fragment.app.FragmentContainerView>
<com.google.android.material.bottomnavigation.BottomNavigationView
android:id="#+id/tabBar"
android:layout_width="match_parent"
android:layout_height="58dp"
android:layout_alignParentBottom="true"
app:elevation="2dp"
app:itemBackground="#color/light_gray"
app:itemIconSize="30dp"
app:itemIconTint="#color/tab_bar_icon_tint_color"
app:labelVisibilityMode="unlabeled"
app:layout_constraintBottom_toBottomOf="parent"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintStart_toStartOf="parent"
app:menu="#menu/tab_bar" />
</androidx.constraintlayout.widget.ConstraintLayout>
MainActivity:
class MainActivity : AppCompatActivity() {
private lateinit var viewBinding: ActivityMainBinding
private lateinit var tabBar: BottomNavigationView
private lateinit var navHostFragment: NavHostFragment
override fun onCreate(savedInstanceState: Bundle?) {
super.onCreate(savedInstanceState)
println("MainActivity || onCreate ||")
// Initialize viewBinding object
viewBinding = ActivityMainBinding.inflate(layoutInflater)
// Hide the default navigation bar; we implement our own
supportActionBar!!.hide()
viewBinding.loadingPageLayout.visibility = View.GONE
setContentView(viewBinding.root)
setupBottomBarNavigation()
}
private fun setupBottomBarNavigation() {
tabBar = viewBinding.tabBar
navHostFragment = supportFragmentManager.findFragmentById(R.id.navHostFragment) as NavHostFragment
val navigationController = navHostFragment.navController
val appBarConfiguration = AppBarConfiguration(setOf(R.id.scanFragment, R.id.mainFragment, R.id.profileFragment))
setupActionBarWithNavController(navigationController, appBarConfiguration)
tabBar.setupWithNavController(navigationController)
val navHost = supportFragmentManager.currentNavigationFragment
println("Current navigation fragment: $navHost")
}
override fun onSupportNavigateUp() =
Navigation.findNavController(this, R.id.navHostFragment).navigateUp()
Please help a desperate fellow out!
If someone had the same issues due to multiple clicks on the screen. It can be resolved by checking the current destination first before navigating
For example
Fragments A, B, and C
navigating from A to B while clicking on a button in fragment A that navigates to C might lead to crashes in some cases
for that you should check the current destination first as follows:
if(findNavController().currentDestination?.id==R.id.AFragment)
findNavController().navigate(
AFragmentDirections.actionAFragmentToCFragment()
)
The problem was not in Navigation Components but instead - I think - in my ImageAnalyzer. Turns out I accidentally allowed multiple calls to findNavController.navigate(). Now that I fixed that, my navigation issue is gone.

emre1512 Noty RelativeLayout creation in Kotlin

I have a simple MainActivity with a simple main layout like this:
<?xml version="1.0" encoding="utf-8"?>
<androidx.constraintlayout.widget.ConstraintLayout
xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
android:layout_width="match_parent"
android:layout_height="match_parent">
<ImageView
... />
</androidx.constraintlayout.widget.ConstraintLayout>
I need to activate Noty on this activity. What I need to know is how to create RelativeLayout "yourLayout" in its simple exmple in Kotlin.
Noty.init(YourActivity.this, "Your warning message", yourLayout, Noty.WarningStyle.SIMPLE).show();
Thank you!
Add an android:id attribute to your ConstraintLayout tag:
android:id="#+id/root"
Now you can fetch a reference to that ConstraintLayout in code:
val root: ConstraintLayout = findViewById(R.id.root)
Noty.init(this#MainActivity, "Your warning message", root, Noty.WarningStyle.SIMPLE).show()

why RecyclerView doesn't work in android 9

Although everything works well in android 8 and lower, but my RecyclerView doesn't load anything in android 9.
because I used Picasso in my recyclerView Adapter, I added code bellow to my AndroidManifest but nothing changed:
android:usesCleartextTraffic="true"
this is my RecyclerView XML code which is located in a MotionLayout:
<LinearLayout
android:id="#+id/linearlayout"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:background="#F6F6F6"
android:elevation="10dp"
android:orientation="horizontal">
<androidx.recyclerview.widget.RecyclerView
android:id="#+id/recycler_challenge"
android:layout_width="match_parent"
android:layout_height="wrap_content" />
</LinearLayout>
note that I have two recyclerView in my layout, the first one, works well
but the second one doesn't work!
Define a xml file, then add to your manifest
->> android:networkSecurityConfig="#xml/network_security_config"
<?xml version="1.0" encoding="utf-8"?>
<network-security-config>
<domain-config cleartextTrafficPermitted="true">
<domain includeSubdomains="true">secure.example.com</domain>
</domain-config>
</network-security-config>

Recyclerview not displaying list item but gray background and text "android...RecyclerView"

i am a new to Kotlin Android development,i added recycler view to my project and then move the icon to the layout according to the tutorial i was watching but it not displaying anything except gray background and this black text "andriod...recycler" instead of displaying the list items samples as in the tutorial
i suspect it the api level, the android studio version is 3.1
it on Api28, here is the activity_hobbies.xml
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical">
<android.support.v7.widget.RecyclerView
android:layout_width="match_parent"
android:layout_height="match_parent" />
</LinearLayout>
this is HobbiesActivity.tk
package org.blessedmedia.www.kotlinapp
import android.os.Bundle
import android.support.v7.app.AppCompatActivity
class HobbiesActivity : AppCompatActivity(){
override fun onCreate(savedInstanceState: Bundle?) {
super.onCreate(savedInstanceState)
setContentView(R.layout.activity_hobbies)
}
}
if you need any more file to provide solution.kindly comment

Attempt to invoke virtual method crash in AndroidPdfViewer

Using the AndroidPdfViewer package.
My build.gradle code:
dependencies {
implementation fileTree(dir: 'libs', include: ['*.jar'])
implementation 'com.android.support:appcompat-v7:26.1.0'
implementation 'com.android.support.constraint:constraint-layout:1.0.2'
testImplementation 'junit:junit:4.12'
androidTestImplementation 'com.android.support.test:runner:1.0.1'
androidTestImplementation 'com.android.support.test.espresso:espresso-core:3.0.1'
implementation("com.github.bumptech.glide:glide:4.5.0") {
exclude group: "com.android.support"
}
implementation "com.android.support:support-fragment:26.1.0"
compile 'com.github.barteksc:android-pdf-viewer:2.8.2'
}
Activity XML file:
<?xml version="1.0" encoding="utf-8"?>
<android.support.constraint.ConstraintLayout 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/linearLayout"
android:layout_width="match_parent"
android:layout_height="match_parent">
<com.github.barteksc.pdfviewer.PDFView
android:id="#+id/pdfView"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:layout_centerInParent="true"
app:layout_constraintBottom_toBottomOf="parent"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toTopOf="parent" />
</android.support.constraint.ConstraintLayout>
Activity code:
package eu.myapp.myapp;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import com.github.barteksc.pdfviewer.PDFView;
public class activity_fact_sheet extends AppCompatActivity {
PDFView pdfView;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
PDFView pdfView = (PDFView) findViewById(R.id.pdfView);
pdfView.fromAsset("file2.pdf").load();
}
}
file2.pdf located in app > src > main > assets
Error on run:
01-18 11:59:25.475 29674-29674/eu.myapp.myapp E/AndroidRuntime: FATAL EXCEPTION: main
Process: eu.myapp.myapp, PID: 29674
java.lang.RuntimeException: Unable to start activity ComponentInfo{eu.myapp.myapp/eu.myapp.myapp.activity_fact_sheet}: java.lang.NullPointerException: Attempt to invoke virtual method 'com.github.barteksc.pdfviewer.PDFView$Configurator com.github.barteksc.pdfviewer.PDFView.fromAsset(java.lang.String)' on a null object reference
at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2924)
at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2985)
at android.app.ActivityThread.-wrap14(ActivityThread.java)
at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1635)
at android.os.Handler.dispatchMessage(Handler.java:102)
at android.os.Looper.loop(Looper.java:154)
at android.app.ActivityThread.main(ActivityThread.java:6692)
at java.lang.reflect.Method.invoke(Native Method)
at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:1468)
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:1358)
Caused by: java.lang.NullPointerException: Attempt to invoke virtual method 'com.github.barteksc.pdfviewer.PDFView$Configurator com.github.barteksc.pdfviewer.PDFView.fromAsset(java.lang.String)' on a null object reference
at eu.myapp.myapp.activity_fact_sheet.onCreate(activity_fact_sheet.java:17)
at android.app.Activity.performCreate(Activity.java:6912)
at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1126)
at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2877)
at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2985) 
at android.app.ActivityThread.-wrap14(ActivityThread.java) 
at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1635) 
at android.os.Handler.dispatchMessage(Handler.java:102) 
at android.os.Looper.loop(Looper.java:154) 
at android.app.ActivityThread.main(ActivityThread.java:6692) 
at java.lang.reflect.Method.invoke(Native Method) 
at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:1468) 
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:1358) 
Steps taken to resolve it:
added PDFView pdfView; to activity code above #Override and under public class activity_fact_sheet extends AppCompatActivity {
Also checked to see if it was an issue with my activity opening method (it wasn't).
It seems the error is thrown on pdfView.fromAsset("file2.pdf").load(); in the activity code. That makes it seem to me that it cannot find file2.pdf in the assets, but I'm probably wrong.
The app crashes when you try to open the activity.
Tried to implement it using this video.
OK, I found another topic on stackexchange (besides the countless ones on nullpointer exceptions - which didn't help me). This has the code that works. Just import
compile 'com.github.barteksc:android-pdf-viewer:2.8.2'
compile 'org.apache.commons:commons-collections4:4.1'
in your gradle build file.
Then make sure you have the <uses-permission android:name="android.permission.WRITE_SETTINGS"/> permission in AndroidManifest.xml. (You should already have had this anyway.)
Then make sure to copy his code in your activity where you want to show the PDF. Change public static final string SAMPLE_FILE to the actual pdf you want to show (in your assets folder).
If you get an error on the TAG in Log.e(TAG, String.format("%s %s, p %d", sep, b.getTitle(), b.getPageIdx())); you must add private static final String TAG = "youractivitynamehere"; just under the public static final String SAMPLE_FILE = "yourfilehere.pdf";
That's all! Hope anyone else searching for this issue will find this.
Add this code in your XML file
<com.github.barteksc.pdfviewer.PDFView
android:layout_width="match_parent"
android:layout_height="match_parent"
android:id="#+id/pdfView"
></com.github.barteksc.pdfviewer.PDFView>