MainActivity.kt---------------package com.example.stsearchimport android.content.Intentimport androidx.appcompat.app.AppCompatActivityimport android.os.Bundleimport android.view.Viewimport android.widget.AdapterViewimport android.widget.ArrayAdapterimport android.widget.Spinnerimport android.widget.Toastimport kotlinx.android.synthetic.main.activity_main.*class MainActivity : AppCompatActivity() { override fun onCreate(savedInstanceState: Bundle?) { super.onCreate(savedInstanceState) setContentView(R.layout.activity_main) var fossVsFossID = HashMap<String, Int>() fossVsFossID.put("Java",10) fossVsFossID.put("Cpp",57) fossVsFossID.put("Python",26) fossVsFossID.put("RDBMS",92) var languageVsLanguageID = HashMap<String, Int>() languageVsLanguageID.put("English", 22) languageVsLanguageID.put("Hindi", 6) languageVsLanguageID.put("Gujarati", 5) languageVsLanguageID.put("Tamil", 18) languageVsLanguageID.put("Marathi", 12) languageVsLanguageID.put("Kannada", 7) var fossVsLanguage = HashMap<String, ArrayList<String>>() var availableLanguagesForFoss: ArrayList<String> availableLanguagesForFoss = arrayListOf("English", "Gujarati","Hindi", "Kannada") fossVsLanguage.put("Java", availableLanguagesForFoss) availableLanguagesForFoss = arrayListOf("English", "Gujarati", "Hindi","Kannada", "Marathi", "Tamil") fossVsLanguage.put("Cpp", availableLanguagesForFoss) availableLanguagesForFoss = arrayListOf("English", "Hindi") fossVsLanguage.put("Python", availableLanguagesForFoss) availableLanguagesForFoss = arrayListOf("English") fossVsLanguage.put("RDBMS", availableLanguagesForFoss) var fossSpinner = findViewById<Spinner>(R.id.Spinner1) var languageSpinner = findViewById<Spinner>(R.id.Spinner2) var fossOptions = fossVsFossID.keys.toList() var languageOptions = languageVsLanguageID.keys.toMutableList() var selectedFoss = "none" var selectedLanguage = "none" var fossAdapter = ArrayAdapter<String>(applicationContext, android.R.layout.simple_list_item_activated_1, fossOptions) fossSpinner.adapter = fossAdapter var languageAdapter = ArrayAdapter<String>(applicationContext, android.R.layout.simple_list_item_activated_1, languageOptions) languageSpinner.adapter = languageAdapter fossSpinner.onItemSelectedListener = object : AdapterView.OnItemSelectedListener { override fun onNothingSelected(parent: AdapterView<*>?) { } override fun onItemSelected(parent: AdapterView<*>?, view: View?, position: Int, id: Long) { languageOptions.clear() selectedFoss = fossOptions.get(position) languageOptions.addAll(ArrayList(fossVsLanguage.get(selectedFoss)!!)) languageAdapter.notifyDataSetChanged() } } languageSpinner.onItemSelectedListener = object : AdapterView.OnItemSelectedListener { override fun onNothingSelected(parent: AdapterView<*>?) { } override fun onItemSelected(parent: AdapterView<*>?, view: View?, position: Int, id: Long) { selectedLanguage = languageOptions.get(position) } } searchButton.setOnClickListener { if (selectedFoss == "none" || selectedLanguage == "none") { Toast.makeText(this, "Please select FOSS and Language both", Toast.LENGTH_SHORT).show() } else { var fossID = fossVsFossID.get(selectedFoss) var languageID = languageVsLanguageID.get(selectedLanguage) var searchIntent = Intent(this, Main2Activity::class.java).apply { putExtra("fossID", "" + fossID) putExtra("languageID", "" + languageID) } startActivity(searchIntent) } } }}-------------------------------------------------------------------------------Main2Activity.kt----------------package com.example.stsearchimport androidx.appcompat.app.AppCompatActivityimport android.os.Bundleimport android.widget.Toastimport androidx.recyclerview.widget.LinearLayoutManagerimport androidx.recyclerview.widget.RecyclerViewimport com.android.volley.Requestimport com.android.volley.Request.*import com.android.volley.Responseimport com.android.volley.toolbox.StringRequestimport com.android.volley.toolbox.Volleyimport kotlinx.android.synthetic.main.activity_main2.*import org.json.JSONArrayimport org.json.JSONObjectclass Main2Activity : AppCompatActivity() { var finalResultsArrayList = ArrayList<VideoItem>() lateinit var myAdapter: MyAdapter override fun onCreate(savedInstanceState: Bundle?) { super.onCreate(savedInstanceState) setContentView(R.layout.activity_main2) var foss = intent.getStringExtra("fossID") var language = intent.getStringExtra("languageID") selectedOption.text = "Foss ID : " + foss + "\nLanguage ID : " + language var myRecyclerView = findViewById<RecyclerView>(R.id.videos_recyclerview) myAdapter = MyAdapter(applicationContext, finalResultsArrayList) myRecyclerView.layoutManager = LinearLayoutManager(this) myRecyclerView.setHasFixedSize(true) myRecyclerView.adapter = myAdapter val queue = Volley.newRequestQueue(this) val url = "https://spoken-tutorial.org/api/get_tutorials/" + foss+ "/" + language val stringRequest = StringRequest(Request.Method.GET, url, Response.Listener<String> { response -> Toast.makeText(applicationContext,"Received server response!", Toast.LENGTH_SHORT).show() extractJsonData(response) }, Response.ErrorListener { Toast.makeText(applicationContext, "Unable to connect to the server", Toast.LENGTH_SHORT).show() }) Toast.makeText(applicationContext, "Contacting Server...", Toast.LENGTH_SHORT).show() queue.add(stringRequest) } private fun extractJsonData(jsonResponse: String) { var videosDataArray = JSONArray(jsonResponse) var singleVideoJsonObject: JSONObject var singleVideoItem: VideoItem var i = 0 var size = videosDataArray.length() while (i < size) { singleVideoJsonObject = videosDataArray.getJSONObject(i) singleVideoItem = VideoItem(singleVideoJsonObject.getString("video_id"), singleVideoJsonObject.getString("tutorial_name"), singleVideoJsonObject.getString("tutorial_level")) finalResultsArrayList.add(singleVideoItem) i++ } println("The parsed videoItems are :") finalResultsArrayList.forEach { println(it) } myAdapter.notifyDataSetChanged() }}------------------------------------------------------------------------------------------ThirdActivity.kt----------------package com.example.stsearchimport androidx.appcompat.app.AppCompatActivityimport android.os.Bundleimport android.widget.Toastclass ThirdActivity : AppCompatActivity() { override fun onCreate(savedInstanceState: Bundle?) { super.onCreate(savedInstanceState) setContentView(R.layout.activity_third) var selectedVideoID = intent.getStringExtra("VIDEO_ID") var selectedVideoTitle = intent.getStringExtra("VIDEO_TITLE") Toast.makeText(applicationContext,""+ selectedVideoTitle, Toast.LENGTH_SHORT).show() }}------------------------------------------------------------------------------------------------MyAdapter.kt-------------package com.example.stsearchimport android.content.Contextimport android.content.Intentimport android.view.LayoutInflaterimport android.view.Viewimport android.view.ViewGroupimport androidx.recyclerview.widget.RecyclerViewimport com.squareup.picasso.Picassoimport kotlinx.android.synthetic.main.my_customlayout.view.*class MyAdapter (val mContext : Context, val videoList : ArrayList<VideoItem>) : RecyclerView.Adapter<MyAdapter.MyViewHolder>(){ class MyViewHolder(itemView : View) : RecyclerView.ViewHolder(itemView) override fun onCreateViewHolder(parent: ViewGroup, viewType: Int): MyViewHolder { val layoutInflator = LayoutInflater.from(parent.context) val videoItem = layoutInflator.inflate(R.layout.my_customlayout, parent, false) return MyViewHolder(videoItem) } override fun onBindViewHolder(holder: MyViewHolder, position: Int) { var singleVideoItem = videoList.get(position) holder.itemView.video_title.text = singleVideoItem.videoTitle holder.itemView.video_level.text = "Level : " + singleVideoItem.videoLevel Picasso.with(mContext) .load("https://i.ytimg.com/vi/"+singleVideoItem.videoId+"/sddefault.jpg") .resize(640,360)//640x360 *initially 320x180 .centerCrop() .into(holder.itemView.video_thumbnail) holder.itemView.itemLinearLayout.setOnClickListener{ var playVideoIntent = Intent(mContext, ThirdActivity::class.java).apply { putExtra("VIDEO_ID",""+singleVideoItem.videoId) putExtra("VIDEO_TITLE",""+singleVideoItem.videoTitle) addFlags(Intent.FLAG_ACTIVITY_NEW_TASK) } mContext.startActivity(playVideoIntent) } } override fun getItemCount(): Int { return videoList.size }}--------------------------------------------------------------------------------------------Run Log file------------Run logfile-----------06/23 06:33:33: Launching app$ adb push /home/rsenthilkumar/STsearch/app/build/outputs/apk/debug/app-debug.apk /data/local/tmp/com.example.stsearch$ adb shell pm install -t -r "/data/local/tmp/com.example.stsearch"SuccessAPK installed in 18 s 61 ms$ adb shell am start -n "com.example.stsearch/com.example.stsearch.MainActivity" -a android.intent.action.MAIN -c android.intent.category.LAUNCHERClient not ready yet..Waiting for process to come onlineConnected to process 26355 on device lge-lm_x210-LMX210bcbfde07Capturing and displaying logcat messages from application. This behavior can be disabled in the "Logcat output" section of the "Debugger" settings page.W/art: Before Android 4.1, method android.graphics.PorterDuffColorFilter androidx.vectordrawable.graphics.drawable.VectorDrawableCompat.updateTintFilter(android.graphics.PorterDuffColorFilter, android.content.res.ColorStateList, android.graphics.PorterDuff$Mode) would have incorrectly overridden the package-private method in android.graphics.drawable.DrawableW/art: Before Android 4.1, method int androidx.appcompat.widget.DropDownListView.lookForSelectablePosition(int, boolean) would have incorrectly overridden the package-private method in android.widget.ListViewI/Adreno-EGL: <qeglDrvAPI_eglInitialize:379>: EGL 1.4 QUALCOMM build: Nondeterministic_AU_msm8909_LA.BR.1.2.9_RB1__release_AU (I09d312ff84) OpenGL ES Shader Compiler Version: E031.31.00.01 Build Date: 09/25/17 Mon Local Branch: Remote Branch: quic/LA.BR.1.2.9_rb1.26 Local Patches: NONE Reconstruct Branch: NOTHINGI/OpenGLRenderer: Initialized EGL, version 1.4D/OpenGLRenderer: Swap behavior 1I/Timeline: Timeline: Activity_idle id: android.os.BinderProxy@d0a84c0 time:2076235D/ViewRootImpl: Need to measure again cause of requestLayout called between measure and layoutI/Timeline: Timeline: Activity_idle id: android.os.BinderProxy@d0a84c0 time:2112300I/ViewRootImpl: ViewRoot's Touch Event : ACTION_DOWNI/ViewRootImpl: ViewRoot's Touch Event : ACTION_UPI/ViewRootImpl: ViewRoot's Touch Event : ACTION_DOWNI/ViewRootImpl: ViewRoot's Touch Event : ACTION_UPI/AudioManagerEx: AudioManagerEx createdD/OpenGLRenderer: endAllActiveAnimators on 0x95ed6000 (DropDownListView) with handle 0xaad99c70I/ViewRootImpl: ViewRoot's Touch Event : ACTION_DOWNI/ViewRootImpl: ViewRoot's Touch Event : ACTION_UPI/ViewRootImpl: ViewRoot's Touch Event : ACTION_DOWNI/ViewRootImpl: ViewRoot's Touch Event : ACTION_UPD/OpenGLRenderer: endAllActiveAnimators on 0x95ed9800 (DropDownListView) with handle 0xab629cc0I/ViewRootImpl: ViewRoot's Touch Event : ACTION_DOWNI/ViewRootImpl: ViewRoot's Touch Event : ACTION_UPI/AudioManagerEx: AudioManagerEx createdI/Timeline: Timeline: Activity_launch_request id:com.example.stsearch time:2117599D/AndroidRuntime: Shutting down VME/AndroidRuntime: FATAL EXCEPTION: main Process: com.example.stsearch, PID: 26355 java.lang.RuntimeException: Unable to start activity ComponentInfo{com.example.stsearch/com.example.stsearch.Main2Activity}: java.lang.IllegalStateException: myRecyclerView must not be null at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2763) at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2824) at android.app.ActivityThread.-wrap12(ActivityThread.java) at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1546) at android.os.Handler.dispatchMessage(Handler.java:102) at android.os.Looper.loop(Looper.java:154) at android.app.ActivityThread.main(ActivityThread.java:6351) at java.lang.reflect.Method.invoke(Native Method) at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:896) at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:786) Caused by: java.lang.IllegalStateException: myRecyclerView must not be null at com.example.stsearch.Main2Activity.onCreate(Main2Activity.kt:33) at android.app.Activity.performCreate(Activity.java:6775) at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1119) at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2716) at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2824) at android.app.ActivityThread.-wrap12(ActivityThread.java) at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1546) at android.os.Handler.dispatchMessage(Handler.java:102) at android.os.Looper.loop(Looper.java:154) at android.app.ActivityThread.main(ActivityThread.java:6351) at java.lang.reflect.Method.invoke(Native Method) at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:896) at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:786) Application terminated.
I'm getting an error while importing the package (android.support.v7.widget.RecyclerView).What do I do?
3436 visits
Outline:About RecyclerView Add RecyclerView and set its attributes Create a custom layout Add ImageView to display the thumbnail of videos Add TextView to display the video title and video level Create an adapter to populate the custom layout in RecyclerView Source code to get the thumbnail from a specific spoken tutorial YouTube URL Implement the required methods Pass the Foss and Language ids to the URL of Spoken Tutorial Run the Kotlin App and display the thumbnails
About RecyclerView Add RecyclerView and set its attributes Create a custom layout Add ImageView to display the thumbnail of videos Add TextView to display the video title and video level Create an adapter to populate the custom layout in RecyclerView Source code to get the thumbnail from a specific spoken tutorial YouTube URL Implement the required methods Pass the Foss and Language ids to the URL of Spoken Tutorial Run the Kotlin App and display the thumbnails
Show video info
Pre-requisite