-1

I want to click on the image and get textview text but im getting this error Attempt to invoke virtual method 'void android.widget.ImageView.setOnClickListener(android.view.View$OnClickListener)' on a null object reference

XML

          <ImageView
            android:id="@+id/imgClick"
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            app:srcCompat="@mipmap/ic_launcher"
            />
        <TextView
            android:id="@+id/Message"
            android:layout_width="50dp"
            android:layout_height="50dp"
            android:text="text yeah"
            android:visibility="invisible"/>

Here i want to acess to the above XML items!!

Fragment

 class HomeFragment : Fragment() {

 var newList: MutableList<News> = mutableListOf()

 companion object {
     fun newInstance() =HomeFragment()
}

private lateinit var viewModel: HomeViewModel

override fun onCreateView(
    inflater: LayoutInflater,
    container: ViewGroup?,
    savedInstanceState: Bundle?
): View? {
    return inflater.inflate(R.layout.fragment_gallery, container, false)
}

override fun onActivityCreated(savedInstanceState: Bundle?) {
    super.onActivityCreated(savedInstanceState)
    viewModel = ViewModelProviders.of(this).get(HomeViewModel::class.java)


    recyclerView.setHasFixedSize(true)
    recyclerView.layoutManager = LinearLayoutManager(this.context)


    // TODO: Use the ViewModel

    val lastNewsObserver = Observer<List<News>>
    {
        // Access the RecyclerView Adapter and load the data into it
            newList -> recyclerView.adapter = NewsAdapter(newList,this.context!!)

    }

    viewModel.getNews().observe(this, lastNewsObserver)

    val img: ImageView = imageView.findViewById(R.id.imgClick)
    val msg: TextView = textView.findViewById(R.id.Message)
    img.setOnClickListener{
        val intent = Intent()
        intent.action = Intent.ACTION_SEND
        intent.putExtra(Intent.EXTRA_TEXT, msg.text)
        intent.type = "text/plain"

        startActivity(intent)
    }
}

 }
  • 1
    Does this answer your question? [What is a NullPointerException, and how do I fix it?](https://stackoverflow.com/questions/218384/what-is-a-nullpointerexception-and-how-do-i-fix-it) – a_local_nobody Jan 09 '20 at 05:33

1 Answers1

0

imgClick is null. It's impossible to tell why without seeing the definition and initialization code, but if it's a normal variable, you need to initialize it after the view is created. My guess would be that if you are initializing it via findViewById, you are doing so before onCreateView, at which point the view hasn't been created yet, so it returns null.

Ryan M
  • 11,512
  • 21
  • 38
  • 50
  • Im doing after as you can see on Fragment end of file! How imgClick is null if when i write it it detect it and suggest the exact file i want to get it ... Not understanding – Francisco Marques Jan 09 '20 at 01:34
  • The code in your question does not initialize `imgClick` - please show the code that does – Ryan M Jan 09 '20 at 01:36
  • Im very begginer... I think im not initializing how should i do this? – Francisco Marques Jan 09 '20 at 01:44
  • Here's some information on how to declare and initialize variables: https://kotlinlang.org/docs/tutorials/kotlin-for-py/declaring-variables.html – Ryan M Jan 09 '20 at 01:53
  • val msg: TextView = textView.findViewById(R.id.Message) val img: ImageView = imageView.findViewById(R.id.imgClick) img.setOnClickListener{ val intent = Intent() intent.action = Intent.ACTION_SEND intent.putExtra(Intent.EXTRA_TEXT, msg.text) intent.type = "text/plain" startActivity(intent) } – Francisco Marques Jan 09 '20 at 01:54
  • Attempt to invoke virtual method 'android.view.View android.widget.TextView.findViewById(int)' now gives this error – Francisco Marques Jan 09 '20 at 01:55
  • Okay, you really need to give the entire code (not as a comment, please) or I'm just guessing blindly...where are `textView` and `imageView` set up? – Ryan M Jan 09 '20 at 01:57
  • can you come discord i share my screen with you! This is project for school and i really need to passs this error – Francisco Marques Jan 09 '20 at 02:00
  • Sorry, I'd rather not provide help outside of SO. – Ryan M Jan 09 '20 at 02:02
  • What entire code you need? from the xml file? I updated the fragment one – Francisco Marques Jan 09 '20 at 02:07