-1

I created an ImageView with the hopes of it taking me from one activity to the next when clicked on.

I entered the following code but the compiler is saying:"error: cannot find symbol method findViewById(int)". If you could help me resolve this, I'd be very grateful.

My code:

package com.androidcodefinder.loginscreendemo.Catalysts;

import android.content.Intent;
import android.os.Bundle;
import android.support.annotation.Nullable;
import android.support.v7.widget.LinearLayoutManager;
import android.support.v7.widget.RecyclerView;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ImageView;

import com.androidcodefinder.loginscreendemo.MainActivity;
import com.androidcodefinder.loginscreendemo.R;
import com.androidcodefinder.loginscreendemo.SignUpActivity;
import com.androidcodefinder.loginscreendemo.postpg;

public class AIFragment extends android.support.v4.app.Fragment {
    private static final String TAG = "AIFragments";
    private ImageView imageView;

    @Nullable
    @Override
    public View onCreateView(LayoutInflater inflater, @Nullable ViewGroup container, Bundle savedInstanceState) {
        View view = inflater.inflate(R.layout.fragment_ai, container, false);
        return view;
       imageView=(ImageView)findViewById(R.id.savage);
        imageView.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                Intent intent = new Intent(getActivity(),postpg.class);
                startActivity(intent);
            }
        });
    }
}
Dacre Denny
  • 29,664
  • 5
  • 45
  • 65
Mike Ike
  • 77
  • 1
  • 7

1 Answers1

0
    @Nullable
    @Override
    public View onCreateView(LayoutInflater inflater, @Nullable ViewGroup container, Bundle savedInstanceState) {
       View view = inflater.inflate(R.layout.fragment_ai, container, false);

       imageView=(ImageView)view.findViewById(R.id.savage);
       imageView.setOnClickListener((view) -> {
                Intent intent = new Intent(getActivity(),postpg.class);
                startActivity(intent);
            }
        });

       return view;
    }

The Function findViewById is defined on a View. Use your view you are inflating. I also change the click-Listener to a lambda function, because it is easier to read.

Or with the old syntax:

    @Nullable
    @Override
    public View onCreateView(LayoutInflater inflater, @Nullable ViewGroup container, Bundle savedInstanceState) {
       View view = inflater.inflate(R.layout.fragment_ai, container, false);

       imageView=(ImageView)view.findViewById(R.id.savage);
       imageView.setOnClickListener(new View.OnClickListener() {
         @Override
         public void onClick(View view) {
             Intent intent = new Intent(getActivity(),postpg.class);
             startActivity(intent);
         }
        });

       return view;
    }
Charlie
  • 1,169
  • 2
  • 16
  • 31
  • Thanks for responding. my version of android studio doesn't support Lambada apparently so how could I express that code without it? – Mike Ike Nov 25 '18 at 23:09
  • added the old syntax as well. please mark as resolved if you are happy with the solution. – Charlie Nov 25 '18 at 23:18
  • Here you find the manual to enable lambdas: https://developer.android.com/studio/write/java8-support. It is supported since october 2017 (android studio 3.0) – Charlie Nov 25 '18 at 23:22